两个配置类建两个多数据源配置
## master 数据源配置
aaa.datasource.url=jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=utf8
aaa.datasource.username=root
aaa.datasource.password=
aaa.datasource.driverClassName=com.mysql.jdbc.Driver
## slave 数据源配置
bbb.datasource.url=jdbc:mysql://localhost:3308/learn?useUnicode=true&characterEncoding=utf8
bbb.datasource.username=root
bbb.datasource.password=
`
主要配置两个配置类,两个SqlSessionFactory,读取指定包下面的接口
```java
@Configuration
//指定哪个包下面的接口使用xxxSqlSessionFactory
@MapperScan(basePackages = {"aaa.mapper"},
sqlSessionFactoryRef = "aaaSqlSessionFactory")
public class InquiryDataSourceConfig {
@Primary //主数据源必须
@Bean(name = "aaaDataSource")//数据源
@ConfigurationProperties(prefix = "aaa")
public DataSource inquiryDataSource() {
DataSource dataSource = DruidDataSourceBuilder.create().build();
return dataSource;
}
@Primary
@Bean(name = "aaaSqlSessionFactory")
public SqlSessionFactory inquirySqlSessionFactory(@Qualifier("aaaDataSource") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/aaa/*.xml"));//扫描这个目录下面的xml文件
// 配置包别名
bean.setTypeAliasesPackage("aaa.database.dataobject");
SqlSessionFactory factory = bean.getObject();
return factory;
}
@Primary
@Bean("aaaSqlSessionTemplate")
public SqlSessionTemplate
inquirySqlSessionTemplate(@Qualifier("aaaSqlSessionFactory") SqlSessionFactory sessionFactory) {
return new SqlSessionTemplate(sessionFactory);
}
@Primary
@Bean(name = "aaaTransactionManager")
public DataSourceTransactionManager inquiryTransactionManager() {
return new DataSourceTransactionManager(inquiryDataSource());
}
}
第二个数据源
@Configuration
//指定哪个包下面的接口使用xxxSqlSessionFactory
@MapperScan(basePackages = {"bbb.mapper"},
sqlSessionFactoryRef = "bbbSqlSessionFactory")
public class InquiryDataSourceConfig {
@Bean(name = "bbbDataSource")//数据源
@ConfigurationProperties(prefix = "bbb")
public DataSource inquiryDataSource() {
DataSource dataSource = DruidDataSourceBuilder.create().build();
return dataSource;
}
@Bean(name = "bbbSqlSessionFactory")
public SqlSessionFactory inquirySqlSessionFactory(@Qualifier("bbbDataSource") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/bbb/*.xml"));//扫描这个目录下面的xml文件
// 配置包别名
bean.setTypeAliasesPackage("bbb.database.dataobject");
SqlSessionFactory factory = bean.getObject();
return factory;
}
@Bean("bbbSqlSessionTemplate")
public SqlSessionTemplate
inquirySqlSessionTemplate(@Qualifier("bbbSqlSessionFactory") SqlSessionFactory sessionFactory) {
return new SqlSessionTemplate(sessionFactory);
}
@Bean(name = "bbbTransactionManager")
public DataSourceTransactionManager inquiryTransactionManager() {
return new DataSourceTransactionManager(inquiryDataSource());
}
}