一个最简单的SpringBoot+Mybatis的多数据源解决方案,基于Mysql数据库。
1.第一步先配置多个数据源信息,在application.properties文件里面。配置文件里含有数据库链接等待时候等配置,防止断开链接后报错:No operations allowed after connection closed ,也可以根据实际情况修改。
#多数据源配置–Mysql shop库
spring.datasource.shop.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.shop.url=jdbc:mysql://xxx?useUnicode=true&characterEncoding=UTF-8
spring.datasource.shop.username=xxxspring.datasource.shop.password= xxx
spring.datasource.shop.max-idle=10
spring.datasource.shop.max-wait=10000
spring.datasource.shop.min-idle=5
spring.datasource.shop.initial-size=5
spring.datasource.shop.validation-query=SELECT 1
spring.datasource.shop.test-on-borrow=false
spring.datasource.shop.test-while-idle=true
spring.datasource.shop.time-between-eviction-runs-millis=18800
#多数据源配置-Mysql 对账数据库–bill库
spring.datasource.bill.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.bill.url= xxxxxxxx
spring.datasource.bill.username=xxxxxxxx
spring.datasource.bill.password= xxxxxxxx
#最大的空闲连接数量.
spring.datasource.bill.max-idle=10
#等待连接返回的最大等待时间,毫秒单位.
spring.datasource.bill.max-wait=10000
#最小的空闲连接数量.
spring.datasource.bill.min-idle=5
#初始数量
spring.datasource.bill.initial-size=5
#指定获取连接时连接校验的sql查询语句
spring.datasource.bill.validation-query=SELECT 1
#当从连接池借用连接时,是否测试该连接.
spring.datasource.bill.test-on-borrow=false
#创建时,是否测试连接
spring.datasource.bill.test-while-idle=true
#指定空闲连接检查、废弃连接清理、空闲连接池大小调整之间的操作时间间隔
spring.datasource.bill.time-between-eviction-runs-millis=18800
注意:一个shop库和一个bill库,其中bill为主库,在使用的过程中必须指定主库,不然会报错。
2.主库数据源 配置
/**
* Created by wll on 2017/10/18.
*/
@Configuration
@MapperScan(basePackages = “com.wll.mapper.bill”, sqlSessionTemplateRef = “billSqlSessionTemplate”)
public class BillDataSourceConfig {@Bean(name = “billDataSource”)
@ConfigurationProperties(prefix = “spring.datasource.bill”)
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}@Bean(name = “billSqlSessionFactory”)
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier(“billDataSource”) DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources
(“classpath:mapping/bill/*.xml”));
return bean.getObject();
}
@Bean(name = “billTransactionManager”)
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier(“billDataSource”) DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = “billSqlSessionTemplate”)
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier(“billSqlSessionFactory”) SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
3.另一个数据源信息配置(其实和上面的一样的,只是改了一些东西,复制粘贴代码切忘修改) 注意红色部分,都要修改红色部分意思的为com.wll.mapper.bill目录下的mapper文件指定数据源信息是billSqlSessionTemplate,也指定了这个mapper类对应的xml路径是classpath:mapping/bill/*.xml
@Configuration
@MapperScan(basePackages = ” com.wll.mapper.shop”, sqlSessionTemplateRef = “shopSqlSessionTemplate”)
public class ShopDataSourceConfig {@Bean(name = “shopDataSource”)
@ConfigurationProperties(prefix = “spring.datasource.shop”)
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}@Bean(name = “shopSqlSessionFactory”)
public SqlSessionFactory testSqlSessionFactory(@Qualifier(“shopDataSource”) DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources
(“classpath:mapping/shop/*.xml”));
return bean.getObject();
}@Bean(name = “shopTransactionManager”)
public DataSourceTransactionManager testTransactionManager(@Qualifier(“shopDataSource”) DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = “shopSqlSessionTemplate”)
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier(“shopSqlSessionFactory”) SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
4.Mapper里面和平时一样,xml也一样,这里不再演示。