当前位置: 首页 > news >正文

【第6章】SpringBoot整合Mybatis

文章目录

  • 前言
  • 一、准备
    • 1. 版本要求
    • 2.安装
    • 3. 建表语句
  • 二、案例
    • 1. mapper
    • 2.实体类
    • 3.测试类
    • 4.扫描
    • 5. 配置
    • 6. mapper.xml
    • 7.输出
  • 总结


前言

MyBatis-Spring-Boot-Starter 可以帮助你更快地在 Spring Boot 之上构建 MyBatis 应用。


一、准备

1. 版本要求

MyBatis-Spring-Boot-StarterMyBatis-SpringSpring BootJava
3.03.03.0 - 3.117 或更高
2.32.12.5 - 2.78 或更高

2.安装

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version>
</dependency>

3. 建表语句

CREATE TABLE user  (`id` int NOT NULL,`name` varchar(255) NULL,`age` int NULL,`brith_day` date NULL,PRIMARY KEY (`id`)
);
INSERT INTO `springboot`.`user`(`id`, `name`, `age`, `brith_day`) VALUES (1, '张三', 11, '2014-05-19');
INSERT INTO `springboot`.`user`(`id`, `name`, `age`, `brith_day`) VALUES (2, '李四', 10, '2015-05-19');

二、案例

正如你已经知道的, 要与 Spring 一起使用 MyBatis,你至少需要一个 SqlSessionFactory 和一个 mapper 接口。
MyBatis-Spring-Boot-Starter 将会:

  • 自动探测存在的 DataSource
  • 将使用 SqlSessionFactoryBean 创建并注册一个 SqlSessionFactory 的实例,并将探测到的 DataSource 作为数据源
  • 将创建并注册一个从 SqlSessionFactory 中得到的 SqlSessionTemplate 的实例
  • 自动扫描你的 mapper,将它们与 SqlSessionTemplate 相关联,并将它们注册到Spring 的环境(context)中去,这样它们就可以被注入到你的 bean 中

1. mapper

package org.example.springboot3.mybatis.mappers;import org.apache.ibatis.annotations.Mapper;
import org.example.springboot3.mybatis.model.User;import java.util.List;/*** Create by zjg on 2024/5/19*/
@Mapper
public interface UserMapper {List<User> selectList();
}

2.实体类

package org.example.springboot3.mybatis.model;import lombok.Getter;
import lombok.Setter;
import lombok.ToString;import java.util.Date;/*** Create by zjg on 2024/5/19*/
@Getter
@Setter
@ToString
public class User {private int id;private String name;private int age;private Date brithDay;
}

3.测试类

package org.example.springboot3.mybatis.controller;import lombok.extern.log4j.Log4j2;
import org.example.springboot3.mybatis.mappers.UserMapper;
import org.example.springboot3.mybatis.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;/*** Create by zjg on 2024/5/19*/
@RequestMapping("/mybatis/")
@RestController
@Log4j2
public class UserController {@AutowiredUserMapper userMapper;@RequestMapping("001")public List mybatis001(){List<User> users = userMapper.selectList();log.info(users);return users;}
}

4.扫描

package org.example.springboot3;import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@MapperScan("org.example.springboot3.mybatis.mappers")
@RestController
@SpringBootApplication
public class SpringBoot3Application {private static final Logger log = LoggerFactory.getLogger(SpringBoot3Application.class);public static void main(String[] args) {ConfigurableApplicationContext run = SpringApplication.run(SpringBoot3Application.class, args);String appName = run.getEnvironment().getProperty("spring.application.name");log.info("{}启动完成",appName);}@RequestMapping("/")String home() {return "Hello SpringBoot!";}
}

5. 配置

#mybatis
mybatis:mapper-locations: mappers/**/*.xmltype-aliases-package: org.example.springboot3.mybatis.modeltype-handlers-package: org.example.springboot3.mybatis.typehandlerconfiguration:map-underscore-to-camel-case: truedefault-fetch-size: 100default-statement-timeout: 30

更多配置请查看mybatis配置参数

6. mapper.xml

<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="org.example.springboot3.mybatis.mappers.UserMapper"><select id="selectList" resultType="user">select * from user</select>
</mapper>

7.输出

[2024-05-19 16:40:38.402][http-nio-8080-exec-1][INFO]- org.example.springboot3.mybatis.controller.UserController.mybatis001(UserController.java:23) - [User(id=1, name=张三, age=11, brithDay=Mon May 19 00:00:00 CST 2014), User(id=2, name=李四, age=10, brithDay=Tue May 19 00:00:00 CST 2015)]

总结

回到顶部

架子这就搭好喽,比上一章顺利多了。

更多内容请查看《Mybatis》系列文章目录

http://www.lryc.cn/news/356121.html

相关文章:

  • vim常用指令——001
  • java 对接农行支付相关业务(二)
  • 超频是什么意思?超频的好处和坏处
  • 【cocos creator】进度条控制脚本,支持节点进度条,图片进度条,进度条组件,和进度文字展示
  • Bean的一些属性信息总结
  • CentOS 7 安装 Minio
  • vue3和vite实现vue-router4版本路由的配置以及自动生成路由配置
  • Flutter 中的 CupertinoDatePicker 小部件:全面指南
  • 用 Python 编写自动发送每日电子邮件报告的脚本
  • IT人的拖延——渴望成功与害怕成功的矛盾
  • 【全开源】场馆预定系统源码(ThinkPHP+FastAdmin+UniApp)
  • 音乐系统java在线音乐网站基于springboot+vue的音乐系统带万字文档
  • Python—面向对象小解(1)
  • 2024最新TikTok抖音国际版,tiktok正版免拔卡安装来了!
  • 【Python-OS】os.path.splitext()
  • 安卓开发--安卓使用Echatrs绘制折线图
  • 每日5题Day9 - LeetCode 41 - 45
  • 进程间通信的方式中,socket和消息队列的区别
  • 10. C++异步IO处理库和使用libevent实现高性能服务器
  • React里面useMemo和useCallBack的区别
  • css 渐变色边框
  • prompt提示词:如何让AI帮你提一个好问题
  • 若依ruoyi-vue element-ui 横向滚动条 动态横向滚动条
  • CET-4 听力高频词
  • ARM鲲鹏920-oe2309-caffe
  • 这款网站测试工具,炫酷且强大!【送源码】
  • 成功案例(IF=7.4)| 代谢组+16s联合分析助力房颤代谢重构的潜在机制研究
  • 【LeetCode:496. 下一个更大元素 I + 单调栈】
  • 软考案例题总结
  • 第二证券炒股知识:股票破发后怎么办?