一個最簡單的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.主庫數據源 配置
@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);
}
}
更多Java技術乾貨分享,可以關注我,我會繼續發,謝謝。也可以直接去我技術博客[ vsalw.com]
謝謝各位!
閱讀更多 程序猿老王 的文章