mybatis和spring集成原理 springboot集成mybatis原理



文章插图
mybatis和spring集成原理 springboot集成mybatis原理

文章插图
在《第一个MyBatis应用:Spring Boot整合MyBatis框架》文章中,SqlSessionFactory仍然是由MyBatis来构建的,通过SqlSessionFactory创建SqlSession,然后通过SqlSession来进行增删查改操作,并没有由Spring容器托管 。
实际上,在Spring Boot中使用MyBatis本质就是在Spring框架中集成MyBatis,并没有其他任何高级的东西 。只不过在Spring Boot中使用时因为插件封装的关系使得相关的配置可以更简洁一些,但是这种封装对于不熟悉MyBatis的人来讲反而增加了理解的难度 。
因此,本文将把如何在Spring Boot中使用MyBatis进行详细介绍,希望对你有帮助 。
Spring Cloud Alibaba微服务实战技术专栏,从项目实践出发,包括Spring Cloud Alibaba、Nacos、Gateway、Sentinel、Log日志、分布式全局唯一ID、DDD领域驱动设计等等技术内容,可帮助你对Spring Cloud 微服务技术栈有更加全面和直观的了解 。相信你通过本专栏的练习和实践,能够学以致用,提升微服务应用的开发能力 。
一、创建Maven示例项目
创建Maven示例项目mybatis-second,如图所示:
然后,在pom.xml文件中添加依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
二、配置数据源
通过与 Spring Boot的整合,SqlSessionFactory 交由 Spring 来构建 。构建时需要在Spring的配置文件中添加数据库连接的四大属性来配置数据源 。
在 Spring Boot的application.yml配置文件中添加如下配置 。
# 配置数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: xxx
# 整合mybatis
mybatis:
mapper-locations: classpath:mapper/*.xml
从上述 MyBatis的运行时参数中可以看到,可以通过参数mybatis.mapper-locations指定XML映射器所在位置 。
XML映射器文件UserMapper.xml和上一章中的UserMapper.xml是一样的,可以从上一篇文章中获取 。
三、集成MyBatis-Spring-Boot-Starter插件
通过插件MyBatis-Spring-Boot-Starter在Spring Boot中集成MyBatis时,可以不用再去关心原生配置方式里的细节,直接使用默认配置就能实现最基本的功能 。当然,同样可以针对MyBatis的核心组件进行定制 。
前面已经在项目的pom文件中添加了插件MyBatis-Spring-Boot-Starter的依赖配置 。
默认情况下,插件MyBatis-Spring-Boot-Starter将进行如下配置:
自动检查Spring Boot的数据源配置并构建DataSource对象通过SqlSessionFactoryBean使用数据源构建并注册SqlSessionFactory对象从SqlSessionFactory中创建并注册一个SqlSessionTemplate实例,其实就是构建一个SqlSession对象自动扫描接口映射器,并将这些映射器与SqlSessionTemplate实例进行关联,同时将它们注册到Spring容器中
这些在Spring Boot中通过插件MyBatis-Spring-Boot-Starter自动完成了 。
既然MyBatis的配置已经完成了,那么下一步的工作就是如何编写和使用接口映射器 。
四、定义接口映射器
在示例项目中,定义接口映射器UserMapper,代码如下所示:
public interface UserMapper {
MyUser selectUserById(int id);
List<MyUser> selectAllUser();
void addUser(MyUser myUser);
void updateUser(MyUser myUser);
void deleteUser(int id);
}
[email protected]apper的接口映射器并将其注册到Spring容器中,也可以在Spring [email protected],示例项目采用这种方式 。
五、使用接口映射器
我们在示例项目中创建了一个测试类UserMapperTest,在该类中直接注入接口映射器Bean进行使用 。
测试类UserMapperTest 代码如下所示:
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
void selectUserById() {
MyUser myUser = userMapper.selectUserById(5);
System.out.println(myUser);
}
@Test
void selectAllUser() {
List<MyUser> listUser = userMapper.selectAllUser();
for (MyUser myUser:listUser) {
System.out.println(myUser);
}
}
@Test
void addUser() {
MyUser myUser = new MyUser();
myUser.setUname(“Spring Cloud”);
myUser.setUsex(“男”);
userMapper.addUser(myUser);
}
@Test
void updateUser() {
MyUser myUser = new MyUser();
myUser.setUid(5);
myUser.setUname(“MyBatis”);
myUser.setUsex(“男”);
userMapper.updateUser(myUser);
}
@Test
void deleteUser() {
userMapper.deleteUser(6);
}
}
运行单元测试,验证输出结果,如图所示:
至此可以看到,在Spring Boot中通过插件MyBatis-Spring-Boot-Starter集成MyBatis时非常方便,只需要添加基本的数据源配置就可以使用了 。
六、定制MyBatis运行时参数
在Spring Boot中对MyBatis进行定制主要是指在Spring Boot的配置文件中(如:application.yaml)对MyBatis运行参数进行自定义配置(使用mybatis作为配置参数前缀):
mybatis:
check-config-location: true # 是否检测MyBatis运行参数配置文件
config-location:classpath:/mybatis-config.xml # 指定MyBatis运行参数配置文件位置
mapper-locations: classpath:/mapper/**/*.xml # 注册XML映射器
type-aliases-package: test.springboot.model # 配置Java类型包名
type-handlers-package: test.springboot.handlers # 配置类型处理器包名
executor-type: SIMPLE # 指定执行器类型
configuration:
default-fetch-size: 20
default-statement-timeout: 30
上述配置参数最终是通过mybatis-spring-boot-autoconfigure.jar加载和配置的 。
另外,上述配置参数只是一个配置示例,详细的配置参数列表请参考MyBatis配置官网:http://www.mybatis.org/mybatis-3/zh/configuration.html 。
总结起来,在Spring Boot中使用MyBatis可以使用2种方式:
(1)使用在Spring框架中集成MyBatis的原生集成方式
(2)使用插件MyBatis-Spring-Boot-Starter集成MyBatis
【mybatis和spring集成原理 springboot集成mybatis原理】上述两种方式都可以实现对MyBatis的定制化配置,可以根据个人喜好进行选择 。无论如何,要想在Spring Boot中灵活使用好MyBatis,最基础的还是MyBatis和Spring框架本身 。