SpringBoot+Mybatis+Mysql最簡單的多數據源解決方案

一個最簡單的SpringBoot+Mybatis的多數據源解決方案,基於Mysql數據庫。

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. @Configuration

  2. @MapperScan(basePackages = "com.wll.mapper.bill", sqlSessionTemplateRef = "billSqlSessionTemplate")

  3. public class BillDataSourceConfig {@Bean(name = "billDataSource")

  4. @ConfigurationProperties(prefix = "spring.datasource.bill")

  5. @Primary

  6. public DataSource testDataSource() {

  7. return DataSourceBuilder.create().build();

  8. }@Bean(name = "billSqlSessionFactory")

  9. @Primary

  10. public SqlSessionFactory testSqlSessionFactory(@Qualifier("billDataSource") DataSource dataSource) throws Exception {

  11. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

  12. bean.setDataSource(dataSource);

  13. bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources

  14. ("classpath:mapping/bill/*.xml"));

  15. return bean.getObject();

  16. }

  17. @Bean(name = "billTransactionManager")

  18. @Primary

  19. public DataSourceTransactionManager testTransactionManager(@Qualifier("billDataSource") DataSource dataSource) {

  20. return new DataSourceTransactionManager(dataSource);

  21. }

  22. @Bean(name = "billSqlSessionTemplate")

  23. @Primary

  24. public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("billSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {

  25. return new SqlSessionTemplate(sqlSessionFactory);

  26. }

  27. }

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);

  • }

  • }

更多Java技術乾貨分享,可以關注我,我會繼續發,謝謝。也可以直接去我技術博客[ vsalw.com]

謝謝各位!


分享到:


相關文章: