springboot配置多数据源
- 配置yml文件多数据源
spring:
datasource:
## A数据源配置
A:
jdbc-url: jdbc:XXX://XXXXXX:XXXX/XXX
username: AAA
password: AAA
driverClassName:com.mysql.cj.jdbc.Driver
## B数据源配置
B:
jdbc-url: jdbc:XXX://XXXXXX:XXXX/XXX
username: BBB
password: BBB
driverClassName: com.mysql.cj.jdbc.Driver
mybatis:
mapperLocations: classpath:mapper/A/*.xml,classpath:mapper/B/*.xml,
type-aliases-package: com.xxx.model
- 一共要配置2种文件
- DataSourceConfig.java——罗列n个数据源
- XDBConfig.java——x数据源具体配置
- DataSourceConfig文件代码
罗列设置n个数据源
@Configuration
public class DataSourceConfig {
//只有一个主数据源
@Bean(name = "ADataSource")
@Qualifier("ADataSource")
@Primary // 定义主数据源
@ConfigurationProperties(prefix = "spring.datasource.A")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
//B数据源
@Bean(name = "BDataSource")
@Qualifier("BDataSource")
@ConfigurationProperties(prefix = "spring.datasource.B")
public DataSource thirdDataSource() {
return DataSourceBuilder.create().build();
}
}
- A数据源的具体配置
@Configuration
@MapperScan(basePackages = "com.xxx.mapper.A", sqlSessionTemplateRef = "ASqlSessionTemplate")
public class LipsDBConfig {
@Bean(name = "ASqlSessionFactory")
@Primary
public SqlSessionFactory lipsSqlSessionFactory(@Qualifier("ADataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
try {
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/A/*.xml"));
} catch (Exception e) {
bean.setMapperLocations(null);
}
return bean.getObject();
}
//配置声明式事务管理器
@Bean(name = "ATransactionManager")
@Primary
public PlatformTransactionManager lipsTransactionManager(@Qualifier("ADataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "ASqlSessionTemplate")
@Primary
public SqlSessionTemplate lipsSqlSessionTemplate(
@Qualifier("ASqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
- B数据源的具体配置
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.xxx.mapper.B", sqlSessionTemplateRef = "BSqlSessionTemplate")
public class OipsDBConfig {
@Bean(name = "BSqlSessionFactory")
public SqlSessionFactory oipsSqlSessionFactory(@Qualifier("BDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
try{
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:mapper/B/*.xml"));
}catch (Exception e){
bean.setMapperLocations(null);
}
return bean.getObject();
}
@Bean(name = "BTransactionManager")
public PlatformTransactionManager oipsTransactionManager(
@Qualifier("BDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "BSqlSessionTemplate")
public SqlSessionTemplate oipsSqlSessionTemplate(
@Qualifier("BSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
文件层级是
com
=xxx
==controller
==config->多数据源配置文件
==mapper
===A->A数据源的mapper
===B->B数据源的mapper
resource
=mapper
==A->A数据源的xml
==B->B数据源的xml
.yml->配置文件
来源:CSDN
作者:梁可爱
链接:https://blog.csdn.net/ALiangXiu/article/details/103808381