一、背景
在日常的需求開發(fā)中,考慮到數(shù)據(jù)庫的備份,減緩單一數(shù)據(jù)庫的壓力因素,常常會使用多個(gè)數(shù)據(jù)源,不同的業(yè)務(wù)場景需要從不同的數(shù)據(jù)庫中查詢,常見的數(shù)據(jù)庫有MySQL、TiDB、Oracle等等。
二、代碼實(shí)現(xiàn)
- xml文件配置
salesman.jar --spring.profiles.active=prod
spring:
profiles:
active: dev
application:
name: 項(xiàng)目名
datasource:
mysql:
driver-class-name: com.mysql.jdbc.Driver
maxLifetime: 1765000 #一個(gè)連接的生命時(shí)長(毫秒),超時(shí)而且沒被使用則被釋放(retired),缺省:30分鐘,建議設(shè)置比數(shù)據(jù)庫超時(shí)時(shí)長少30秒以上
maximumPoolSize: 160 #連接池中允許的最大連接數(shù)。缺省值:10;推薦的公式:((core_count * 2) + effective_spindle_count)
tidb:
driver-class-name: com.mysql.jdbc.Driver
maxLifetime: 1765000 #一個(gè)連接的生命時(shí)長(毫秒),超時(shí)而且沒被使用則被釋放(retired),缺省:30分鐘,建議設(shè)置比數(shù)據(jù)庫超時(shí)時(shí)長少30秒以上
maximumPoolSize: 160
# 開發(fā)環(huán)境配置
spring:
profiles: dev
datasource:
mysql:
jdbc-url: xxx
username: xxx
password: xxx
tidb:
jdbc-url: jdbc:xxx
username: xxx
password: xxx
- xml文件配置
@Configuration
@MapperScan(basePackages = {"com.ddd.dao.mysql"}, sqlSessionFactoryRef = YsbDataSourceConfig.SESSION_FACTORY)
public class MysqlDataSourceConfig {
public static final String SESSION_FACTORY = "mysqlSessionFactory";
@Resource
MybatisPlusInterceptor mybatisPlusInterceptor;
@Bean(name = SESSION_FACTORY)
public SqlSessionFactory sessionFactory() throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
// 給MyBatis-Plus注入數(shù)據(jù)源
bean.setDataSource(mysqlDataSource());
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
Interceptor[] plugins = {mybatisPlusInterceptor};//解決分頁失效問題
bean.setPlugins(plugins);
return bean.getObject();
}
@Bean
@Primary
public PlatformTransactionManager mysqlTransactionManager() {
return new DataSourceTransactionManager(mysqlDataSource());
}
@Bean
public DataSource mysqlDataSource() {
return new HikariDataSource(mysqlHikariConfig());
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.mysql")
public HikariConfig mysqlHikariConfig() {
return new HikariConfig();
}
}
@Configuration
@MapperScan(basePackages = {"com.ddd.dao.tidb"}, sqlSessionFactoryRef = OrderTidbDataSourceConfig.SESSION_FACTORY)
public class OrderTidbDataSourceConfig {
public static final String SESSION_FACTORY = "tidbSessionFactory";
@Resource
MybatisPlusInterceptor mybatisPlusInterceptor;
@Bean(name = SESSION_FACTORY)
public SqlSessionFactory sessionFactory() throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
// 給MyBatis-Plus注入數(shù)據(jù)源
bean.setDataSource(tidbDataSource());
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
//解決分頁失效問題
Interceptor[] plugins = {mybatisPlusInterceptor};
bean.setPlugins(plugins);
return bean.getObject();
}
@Bean
public PlatformTransactionManager tidbTransactionManager() {
return new DataSourceTransactionManager(tidbDataSource());
}
@Bean
public MybatisPlusProperties mybatisPlusProperties() {
return new MybatisPlusProperties();
}
@Bean
public DataSource tidbDataSource() {
return new HikariDataSource(tidbHikariConfig());
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.tidb")
public HikariConfig tidbHikariConfig() {
return new HikariConfig();
}
}
-
包目錄
需要注入mysql數(shù)據(jù)源的將dao文件放入mysql目錄下,需要注入tidb數(shù)據(jù)源的將dao文件放到tidb目錄下即可
