一个最简单的SpringBoot+Mybatis的多数据源解决方案,基于Mysql数据库。

 

1.第一步先配置多个数据源信息,在application.properties文件里面。配置文件里含有数据库链接等待时候等配置,防止断开链接后报错:No operations allowed after connection closed  ,也可以根据实际情况修改。

  1. #多数据源配置--Mysql shop库
  2. spring.datasource.shop.driver-class-name=com.mysql.jdbc.Driver
  3. spring.datasource.shop.url=jdbc:mysql://xxx?useUnicode=true&characterEncoding=UTF-8
  4. spring.datasource.shop.username=xxxspring.datasource.shop.password= xxx
  5. spring.datasource.shop.max-idle=10
  6. spring.datasource.shop.max-wait=10000
  7. spring.datasource.shop.min-idle=5
  8. spring.datasource.shop.initial-size=5
  9. spring.datasource.shop.validation-query=SELECT 1
  10. spring.datasource.shop.test-on-borrow=false
  11. spring.datasource.shop.test-while-idle=true
  12. spring.datasource.shop.time-between-eviction-runs-millis=18800
  13. #多数据源配置-Mysql 对账数据库--bill库
  14. spring.datasource.bill.driver-class-name=com.mysql.jdbc.Driver
  15. spring.datasource.bill.url= xxxxxxxx
  16. spring.datasource.bill.username=xxxxxxxx
  17. spring.datasource.bill.password= xxxxxxxx
  18. #最大的空闲连接数量.
  19. spring.datasource.bill.max-idle=10
  20. #等待连接返回的最大等待时间,毫秒单位.
  21. spring.datasource.bill.max-wait=10000
  22. #最小的空闲连接数量.
  23. spring.datasource.bill.min-idle=5
  24. #初始数量
  25. spring.datasource.bill.initial-size=5
  26. #指定获取连接时连接校验的sql查询语句
  27. spring.datasource.bill.validation-query=SELECT 1
  28. #当从连接池借用连接时,是否测试该连接.
  29. spring.datasource.bill.test-on-borrow=false
  30. #创建时,是否测试连接
  31. spring.datasource.bill.test-while-idle=true
  32. #指定空闲连接检查、废弃连接清理、空闲连接池大小调整之间的操作时间间隔
  33. spring.datasource.bill.time-between-eviction-runs-millis=18800
 

注意:一个shop库和一个bill库,其中bill为主库,在使用的过程中必须指定主库,不然会报错。

2.主库数据源 配置

  1. /**
  2. * Created by wll on 2017/10/18.
  3. */
  4. @Configuration
  5. @MapperScan(basePackages = "com.wll.mapper.bill", sqlSessionTemplateRef  = "billSqlSessionTemplate")
  6. public class BillDataSourceConfig {@Bean(name = "billDataSource")
  7. @ConfigurationProperties(prefix = "spring.datasource.bill")
  8. @Primary
  9. public DataSource testDataSource() {
  10. return DataSourceBuilder.create().build();
  11. }@Bean(name = "billSqlSessionFactory")
  12. @Primary
  13. public SqlSessionFactory testSqlSessionFactory(@Qualifier("billDataSource") DataSource dataSource) throws Exception {
  14. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  15. bean.setDataSource(dataSource);
  16. bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources
  17. ("classpath:mapping/bill/*.xml"));
  18. return bean.getObject();
  19. }
  20. @Bean(name = "billTransactionManager")
  21. @Primary
  22. public DataSourceTransactionManager testTransactionManager(@Qualifier("billDataSource") DataSource dataSource) {
  23. return new DataSourceTransactionManager(dataSource);
  24. }
  25. @Bean(name = "billSqlSessionTemplate")
  26. @Primary
  27. public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("billSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
  28. return new SqlSessionTemplate(sqlSessionFactory);
  29. }
  30. }
3.另一个数据源信息配置(其实和上面的一样的,只是改了一些东西,复制粘贴代码切忘修改) 注意红色部分,都要修改红色部分意思的为com.wll.mapper.bill目录下的mapper文件指定数据源信息是billSqlSessionTemplate,也指定了这个mapper类对应的xml路径是classpath:mapping/bill/*.xml
  1. @Configuration
  2. @MapperScan(basePackages = " com.wll.mapper.shop", sqlSessionTemplateRef  = "shopSqlSessionTemplate")
  3. public class ShopDataSourceConfig {@Bean(name = "shopDataSource")
  4. @ConfigurationProperties(prefix = "spring.datasource.shop")
  5. public DataSource testDataSource() {
  6. return DataSourceBuilder.create().build();
  7. }@Bean(name = "shopSqlSessionFactory")
  8. public SqlSessionFactory testSqlSessionFactory(@Qualifier("shopDataSource") DataSource dataSource) throws Exception {
  9. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  10. bean.setDataSource(dataSource);
  11. bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources
  12. ("classpath:mapping/shop/*.xml"));
  13. return bean.getObject();
  14. }@Bean(name = "shopTransactionManager")
  15. public DataSourceTransactionManager testTransactionManager(@Qualifier("shopDataSource") DataSource dataSource) {
  16. return new DataSourceTransactionManager(dataSource);
  17. }
  18. @Bean(name = "shopSqlSessionTemplate")
  19. public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("shopSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
  20. return new SqlSessionTemplate(sqlSessionFactory);
  21. }
  22. }
4.Mapper里面和平时一样,xml也一样,这里不再演示。