Spring整合mybatis回顾
原生Spring中整合mybatis主要是将SqlSessionFactoryBean交给Spring管理。下面来回顾一下
1. 引入依赖
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.1.0</version> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.10</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency>
2. mybatis-config.xml
<!-- DataSource配置 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.56.102:3306/boot-test"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <!-- MyBatis SqlSessionFactory配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/> </bean> <!-- MyBatis SqlSessionTemplate配置 --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <!-- Mapper接口扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.axj.mybatis.mapper"/> </bean> <!-- 事务管理器配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 开启事务管理器--> <tx:annotation-driven transaction-manager="transactionManager"/>
Spring整合mybatis需要配置数据源,事务管理器,显示的开启事务管理器,sqlSessionFacotryBean。感觉整个配置下来巨麻烦不说,还特别容易忘记。
SpringBoot整合mybatis
想比之下,SpringBoot默认帮我们开启了事务管理器,默认帮我们将sqlSessionFacotryBean注册到Spring容器中。我们只需要配置mapper.xml的文件路径以及mapper接口的路径(接口路径可不配置,但是需要在mapper接口上显示的加上@Mapepr注解)和数据库链接参数等信息。
涉及到SpringBoot的自动配置原理,以后再说。
1.引入依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
mybatis-spring-boot-starter 可以简化在 Spring Boot 项目中集成 MyBatis 的过程,并且事务管理也会自动配置。在 Spring Boot 2.x 及以上版本中,只需添加相应的依赖和配置,即可启用事务管理。
2. 配置数据源和 MyBatis 属性
spring: datasource: druid: username: root password: 123456 url: jdbc:mysql://192.168.56.102:3306/boot-test? driver-class-name: com.mysql.cj.jdbc.Driver initial-size: 5 min-idle: 5 max-active: 20 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 validation-query: select 1 from dual test-while-idle: true test-on-borrow: false test-on-return: false mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: cn.axj.boot.mybatis.model.bean configuration: map-underscore-to-camel-case: true
3. 配置 Mapper 接口
确保你的 Mapper 接口和 XML 映射文件配置正确。例如,创建一个简单的 Mapper 接口
@Mapper public interface SysUserMapper { int insertUser(SysUser sysUser); SysUser getUserById(Integer id); }
可通过在SpringBoot配置类上手动加上@MapperScan(basePackages = “cn.axj.boot.mybatis.mapper”)定义mapper接口路径,在创建mapper接口的时候可不用加上@Mapper注解
4. 配置mapper.xml
按照配置在resources目录下创建mapper文件夹,创建一个简单的mapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.axj.boot.mybatis.mapper.SysUserMapper"> <insert id="insertUser"> insert into sys_user(id, name, age) values(#{id}, #{name}, #{age}) </insert> <select id="getUserById" resultType="cn.axj.boot.mybatis.model.bean.SysUser"> select * from sys_user where id = #{id} </select> </mapper>
5. 使用 @Transactional
注解
@Service public class SysUserService { @Resource private SysUserMapper sysUserMapper; @Transactional(rollbackFor = Exception.class) public void testTrans(){ SysUser sysUser = new SysUser(); sysUser.setName("test"); sysUser.setAge(23); sysUserMapper.insertUser(sysUser); int i = ThreadLocalRandom.current().nextInt(10); SysUser userById = sysUserMapper.getUserById(i); if(Objects.isNull(userById)){ throw new RuntimeException("查询结果为空"); } } }