上一篇简单介绍了SpringBoot简单集成Druid、MyBatis和Redis 传送门,本篇是在上一篇的基础上配置多个数据源的配置以及事物的管理。
-
pom.xml文件没有任何变化,直接看项目结构,因为有多个数据源,mapper和xml文件夹要单独分成多个,一个数据源对应一个,由于默认的mybatis配置不支持多个数据源,所以将mybatis的配置放在了config包中, 每个类对应一个数据源的配置
- application.propertis中的变化,每个数据源的配置要单独配置,另外就是Mybatis的配置移到了DataSource1Config.java和DataSource2Config.java中
Spring Boot 2.X 版本不再支持配置继承,多数据源的话每个数据源的所有配置都需要单独配置,否则配置不会生效(引用Druid官方说明)
debug=false
trace=false
# Druid数据源1
spring.datasource.druid.one.url=jdbc:mysql://10.168.0.24:3306/temp_db1?useUnicode=true&characterEncoding=utf8
spring.datasource.druid.one.username=root
spring.datasource.druid.one.password=123456
spring.datasource.driud.one.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.one.initial-size=5
spring.datasource.druid.one.max-active=20
spring.datasource.druid.one.min-idle=10
spring.datasource.druid.one.max-wait=10
# Druid数据源2
spring.datasource.druid.two.url=jdbc:mysql://10.168.0.24:3306/temp_db2?useUnicode=true&characterEncoding=utf8
spring.datasource.druid.two.username=root
spring.datasource.druid.two.password=123456
spring.datasource.driud.two.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.two.initial-size=5
spring.datasource.druid.two.max-active=20
spring.datasource.druid.two.min-idle=10
spring.datasource.druid.two.max-wait=10
# 数据源通用配置
spring.datasource.druid.filters=stat,wall
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000
# WebStatFilter配置
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.gif,*.png,*.jpg,*.html,*.js,*.css,*.ico,/druid/*
# StatViewServlet配置
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.stat-view-servlet.reset-enable=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.stat-view-servlet.allow=
spring.datasource.druid.stat-view-servlet.deny=
#Mybatis配置移到java代码中进行配置
# Redis配置
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=7779
spring.redis.password=
spring.redis.timeout=2000
- DataSource1Config.java中的代码
@Configuration
@MapperScan(basePackages = "com.example.mapper1", sqlSessionTemplateRef = "sqlSessionTemplateOne")
public class DataSource1Config {
@Primary /** 亲测不是必须的,可以省略 */
@Bean("dataSourceOne")
@ConfigurationProperties("spring.datasource.druid.one")
public DataSource dataSourceOne(){
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "sqlSessionFactoryOne")
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSourceOne") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis1/*.xml"));
return bean.getObject();
}
@Bean(name = "transactionManagerOne")
public DataSourceTransactionManager transactionManager(@Qualifier("dataSourceOne") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionTemplateOne")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactoryOne") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
DataSource2Config.java中的代码
@Configuration
@MapperScan(basePackages = "com.example.mapper2", sqlSessionTemplateRef = "sqlSessionTemplateTwo")
public class DataSource2Config {
@Bean("dataSourceTwo")
@ConfigurationProperties("spring.datasource.druid.two")
public DataSource dataSourceOne(){
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "sqlSessionFactoryTwo")
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSourceTwo") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis2/*.xml"));
return bean.getObject();
}
@Bean(name = "transactionManagerTwo")
public DataSourceTransactionManager transactionManager(@Qualifier("dataSourceTwo") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionTemplateTwo")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactoryTwo") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
这两段代码不同的地方体现在@MapperScan的配置和sqlSessionFactory()方法中对MapperLocations的配置,还有@ConfigurationProperties的配置(用户在application.propertis中使用哪个配置)
Service中控制事物的代码,必须指定使用哪个transactionManager,不然会报无法确定该使用哪个事物管理器
@Transactional(transactionManager = "transactionManagerOne")
public int insertShop(Shop4s model){
int result = mapper.insertShop(model);
int i = 1/0; // 用来测试事物是否生效
return result;
}
- 其他废话
mapper和mybatis的xml文件除了位置发生变化外,内部代码没有任何变化,还是按照正常的mybatis格式写就可以。SpringBoot在微服务方面很有优势,一般不建议一个项目中配置多个数据源,可以另创建一个项目来单独连接另一个数据源,也可以在业务上体现单一原则。