Spring Boot 集成MyBatis
在集成MyBatis前,我們先配置一個druid數據源。
Spring Boot 系列
Spring Boot 集成druid
druid
有非常多個配置選項,使用Spring Boot 的配置文件能夠方便的配置druid
。
在application.yml
配置文件里寫上:
spring:
datasource:
name: test
url: jdbc:mysql://192.168.16.137:3306/test
username: root
password:
# 使用druid數據源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
這里通過type: com.alibaba.druid.pool.DruidDataSource
配置就可以!
Spring Boot 集成MyBatis
Spring Boot 集成MyBatis有兩種方式。一種簡單的方式就是使用MyBatis官方提供的:
第二種方式就是仍然用相似mybatis-spring
的配置方式,這樣的方式須要自己寫一些代碼,可是能夠非常方便的控制MyBatis的各項配置。
一、mybatis-spring-boot-starter方式
在pom.xml
中加入依賴:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
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
中添加配置:
mybatis:
mapperLocations: classpath:mapper/*.xml
typeAliasesPackage: tk.mapper.model
除了上面常見的兩項配置。還有:
- mybatis.config:mybatis-config.xml配置文件的路徑
- mybatis.typeHandlersPackage:掃描typeHandlers的包
- mybatis.checkConfigLocation:檢查配置文件是否存在
- mybatis.executorType:設置運行模式(
SIMPLE, REUSE, BATCH
),默覺得SIMPLE
二、mybatis-spring方式
這樣的方式和尋常的使用方法比較接近。
須要加入mybatis
依賴和mybatis-spring
依賴。
然后創建一個MyBatisConfig
配置類:
/** * MyBatis基礎配置 * * @author liuzh * @since 2015-12-19 10:11 */
@Configuration
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {
@Autowired
DataSource dataSource;
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean() {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setTypeAliasesPackage("tk.mybatis.springboot.model");
//分頁插件
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("reasonable", "true");
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("returnPageInfo", "check");
properties.setProperty("params", "count=countSql");
pageHelper.setProperties(properties);
//加入插件
bean.setPlugins(new Interceptor[]{pageHelper});
//加入XML文件夾
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
}
上面代碼創建了一個SqlSessionFactory
和一個SqlSessionTemplate
。為了支持注解事務,添加了@EnableTransactionManagement
注解。而且反回了一個PlatformTransactionManager
Bean。
另外應該注意到這個配置中沒有MapperScannerConfigurer
。假設我們想要掃描MyBatis的Mapper接口。我們就須要配置這個類,這個配置我們須要單獨放到一個類中。
/** * MyBatis掃描接口 * * @author liuzh * @since 2015-12-19 14:46 */
@Configuration
//TODO 注意,因為MapperScannerConfigurer運行的比較早。所以必須有以下的注解
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("tk.mybatis.springboot.mapper");
return mapperScannerConfigurer;
}
}
這個配置一定要注意@AutoConfigureAfter(MyBatisConfig.class)
,必須有這個配置,否則會有異常。原因就是這個類運行的比較早,因為sqlSessionFactory
還不存在。興許運行出錯。
做好上面配置以后就能夠使用MyBatis了。
關於分頁插件和通用Mapper集成
分頁插件作為插件的樣例在上面代碼中有。
通用Mapper配置實際就是配置MapperScannerConfigurer
的時候使用tk.mybatis.spring.mapper.MapperScannerConfigurer
就可以。配置屬性使用Properties
。
Spring Boot集成MyBatis的基礎項目
我上傳到github一個採用第二種方式的集成項目,而且集成了分頁插件和通用Mapper,項目包括了簡單的配置和操作。僅作為參考。
項目地址:https://github.com/abel533/MyBatis-Spring-Boot
分頁插件和通用Mapper的相關信息能夠通過上面地址找到。