在做项目的过程中难免会遇到这种情况:一个项目需要两个数据库中的数据,希望这篇文章能给遇到这些问题的小伙伴一点帮助
第一步:将两个数据源的mapper接口和xml文件分别放入不同的文件夹下;
第二步:在application.yml文件中加入双数据源,一定要指定主数据源,不然会报错
spring:
datasource:
primary:
driver-class-name: com.mysql.jdbc.Driver
url: url地址
username: 用户名
password: 密码
secondary:
driver-class-name: com.mysql.jdbc.Driver
url: url地址
username: 用户名
password: 密码
第三步:config类:
@Configuration
@MapperScan(basePackages = "数据源1的mapper路径:com.dao.mapper.interface1", sqlSessionTemplateRef = "test1SqlSessionTemplate")
public class DataSource1Config {
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/数据源1/*.xml"));//指定mapper.xml路径
return bean.getObject();
}
@Bean(name = "test1TransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test1SqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(basePackages = "数据源2的mapper路径:com.dao.mapper.interface2", sqlSessionTemplateRef = "test2SqlSessionTemplate")
public class DataSource2Config {
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "test2SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/数据源2/*.xml"));//指定mapper.xml路径
return bean.getObject();
}
@Bean(name = "test2TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
此时,项目是可以运行起来的,但是MySql有一个问题,就是配置双数据源后,当连接池空闲时间超过8小时,数据库连接就会自动断掉,为了避免这种情况,还需要在yml文件下加入如下配置:
datasource:
primary:
max-idle: 10
max-wait: 10000
min-idle: 5
initial-size: 5
validation-query: SELECT 1
test-on-borrow: false
test-while-idle: true
time-between-eviction-runs-millis: 18800 #就是这句话
secondary:
max-idle: 10
max-wait: 10000
min-idle: 5
initial-size: 5
validation-query: SELECT 1
test-on-borrow: false
test-while-idle: true
time-between-eviction-runs-millis: 18800 #就是这句话
目前配置双数据源已经完成了。
当然,上面的配置是针对使用默认数据源的配置,但是还有很多童鞋使用了阿里的druid数据源,两种原理基本相同,只是稍作改动:
spring:
datasource:
druid:
primary:
driver-class-name: com.mysql.jdbc.Driver
url: url地址
username: 用户名
password: 密码
secondary:
driver-class-name: com.mysql.jdbc.Driver
url: url地址
username: 用户名
password: 密码
在config中作如下修改:
将 DataSourceBuilder 改为 DuridDataSourceBuilder
转载于:https://blog.51cto.com/11864647/2067708