一、背景
在日常的需求开发中,考虑到数据库的备份,减缓单一数据库的压力因素,常常会使用多个数据源,不同的业务场景需要从不同的数据库中查询,常见的数据库有MySQL、TiDB、Oracle等等。
二、代码实现
- xml文件配置
salesman.jar --spring.profiles.active=prod
spring:
profiles:
active: dev
application:
name: 项目名
datasource:
mysql:
driver-class-name: com.mysql.jdbc.Driver
maxLifetime: 1765000 #一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒以上
maximumPoolSize: 160 #连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)
tidb:
driver-class-name: com.mysql.jdbc.Driver
maxLifetime: 1765000 #一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒以上
maximumPoolSize: 160
# 开发环境配置
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注入数据源
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注入数据源
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数据源的将dao文件放入mysql目录下,需要注入tidb数据源的将dao文件放到tidb目录下即可
