Spring Boot 集成MyBatis
在集成MyBatis前,我们先配置一个druid数据源。
Spring Boot 系列
Spring Boot 集成druid
druid有很多个配置选项,使用Spring Boot 的配置文件可以方便的配置druid。
在application.yml配置文件中写上:
1spring: 2 datasource: 3 name: test 4 url: jdbc:mysql://192.168.16.137:3306/test 5 username: root 6 password: 7 # 使用druid数据源 8 type: com.alibaba.druid.pool.DruidDataSource 9 driver-class-name: com.mysql.jdbc.Driver 10 filters: stat 11 maxActive: 20 12 initialSize: 1 13 maxWait: 60000 14 minIdle: 1 15 timeBetweenEvictionRunsMillis: 60000 16 minEvictableIdleTimeMillis: 300000 17 validationQuery: select 'x' 18 testWhileIdle: true 19 testOnBorrow: false 20 testOnReturn: false 21 poolPreparedStatements: true 22 maxOpenPreparedStatements: 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
这里通过type: com.alibaba.druid.pool.DruidDataSource配置即可!
Spring Boot 集成MyBatis
Spring Boot 集成MyBatis有两种方式,一种简单的方式就是使用MyBatis官方提供的:
另外一种方式就是仍然用类似mybatis-spring的配置方式,这种方式需要自己写一些代码,但是可以很方便的控制MyBatis的各项配置。
一、mybatis-spring-boot-starter方式
在pom.xml中添加依赖:
1<dependency> 2 <groupId>org.mybatis.spring.boot</groupId> 3 <artifactId>mybatis-spring-boot-starter</artifactId> 4 <version>1.0.0</version> 5</dependency>
- 1
- 2
- 3
- 4
- 5
mybatis-spring-boot-starter依赖树如下:
其中mybatis使用的3.3.0版本,可以通过:
<mybatis.version>3.3.0</mybatis.version>属性修改默认版本。
mybatis-spring使用版本1.2.3,可以通过:
<mybatis-spring.version>1.2.3</mybatis-spring.version>修改默认版本。
在application.yml中增加配置:
1mybatis: 2 mapperLocations: classpath:mapper/*.xml 3 typeAliasesPackage: tk.mapper.model
- 1
- 2
- 3
除了上面常见的两项配置,还有:
- mybatis.config:mybatis-config.xml配置文件的路径
- mybatis.typeHandlersPackage:扫描typeHandlers的包
- mybatis.checkConfigLocation:检查配置文件是否存在
- mybatis.executorType:设置执行模式(
SIMPLE, REUSE, BATCH),默认为SIMPLE
二、mybatis-spring方式
这种方式和平常的用法比较接近。需要添加mybatis依赖和mybatis-spring依赖。
然后创建一个MyBatisConfig配置类:
1/** 2 * MyBatis基础配置 3 * 4 * @author liuzh 5 * @since 2015-12-19 10:11 6 */ 7@Configuration 8@EnableTransactionManagement 9public class MyBatisConfig implements TransactionManagementConfigurer { 10 11 @Autowired 12 DataSource dataSource; 13 14 @Bean(name = "sqlSessionFactory") 15 public SqlSessionFactory sqlSessionFactoryBean() { 16 SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); 17 bean.setDataSource(dataSource); 18 bean.setTypeAliasesPackage("tk.mybatis.springboot.model"); 19 20 //分页插件 21 PageHelper pageHelper = new PageHelper(); 22 Properties properties = new Properties(); 23 properties.setProperty("reasonable", "true"); 24 properties.setProperty("supportMethodsArguments", "true"); 25 properties.setProperty("returnPageInfo", "check"); 26 properties.setProperty("params", "count=countSql"); 27 pageHelper.setProperties(properties); 28 29 //添加插件 30 bean.setPlugins(new Interceptor[]{pageHelper}); 31 32 //添加XML目录 33 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); 34 try { 35 bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); 36 return bean.getObject(); 37 } catch (Exception e) { 38 e.printStackTrace(); 39 throw new RuntimeException(e); 40 } 41 } 42 43 @Bean 44 public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { 45 return new SqlSessionTemplate(sqlSessionFactory); 46 } 47 48 @Bean 49 @Override 50 public PlatformTransactionManager annotationDrivenTransactionManager() { 51 return new DataSourceTransactionManager(dataSource); 52 } 53}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
上面代码创建了一个SqlSessionFactory和一个SqlSessionTemplate,为了支持注解事务,增加了@EnableTransactionManagement注解,并且反回了一个PlatformTransactionManagerBean。
另外应该注意到这个配置中没有MapperScannerConfigurer,如果我们想要扫描MyBatis的Mapper接口,我们就需要配置这个类,这个配置我们需要单独放到一个类中。
1/** 2 * MyBatis扫描接口 3 * 4 * @author liuzh 5 * @since 2015-12-19 14:46 6 */ 7@Configuration 8//TODO 注意,由于MapperScannerConfigurer执行的比较早,所以必须有下面的注解 9@AutoConfigureAfter(MyBatisConfig.class) 10public class MyBatisMapperScannerConfig { 11 12 @Bean 13 public MapperScannerConfigurer mapperScannerConfigurer() { 14 MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); 15 mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory"); 16 mapperScannerConfigurer.setBasePackage("tk.mybatis.springboot.mapper"); 17 return mapperScannerConfigurer; 18 } 19 20}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
这个配置一定要注意@AutoConfigureAfter(MyBatisConfig.class),必须有这个配置,否则会有异常。原因就是这个类执行的比较早,由于sqlSessionFactory还不存在,后续执行出错。
做好上面配置以后就可以使用MyBatis了。
关于分页插件和通用Mapper集成
分页插件作为插件的例子在上面代码中有。
通用Mapper配置实际就是配置MapperScannerConfigurer的时候使用tk.mybatis.spring.mapper.MapperScannerConfigurer即可,配置属性使用Properties。
Spring Boot集成MyBatis的基础项目
我上传到github一个采用第二种方式的集成项目,并且集成了分页插件和通用Mapper,项目包含了简单的配置和操作,仅作为参考。