主数据源:注意一定要指明primary
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = SearchDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "searchSqlSessionFactory")
public class SearchDataSourceConfig {
// 精确到 master 目录,以便跟其他数据源隔离
static final String PACKAGE = "com.ysten.viper.search.manager.repository.mapper";
static final String MAPPER_LOCATION = "classpath*:mapper/*.xml";
@Value("${search.datasource.url}")
private String url;
@Value("${search.datasource.username}")
private String user;
@Value("${search.datasource.password}")
private String password;
@Value("${search.datasource.driverClassName}")
private String driverClass;
@Bean(name = "searchDataSource")
@Primary
public DataSource searchDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
System.out.println(dataSource);
return dataSource;
}
@Bean(name = "searchTransactionManager")
@Primary
public DataSourceTransactionManager searchTransactionManager() {
return new DataSourceTransactionManager(searchDataSource());
}
@Bean(name = "searchSqlSessionFactory")
@Primary
public SqlSessionFactory searchSqlSessionFactory(@Qualifier("searchDataSource") DataSource searchDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(searchDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(SearchDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
另一个数据源
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = AnalysisDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "analysisSqlSessionFactory")
public class AnalysisDataSourceConfig {
// 精确到 master 目录,以便跟其他数据源隔离
static final String PACKAGE = "com.ysten.viper.search.manager.analysis.repository.mapper";
static final String MAPPER_LOCATION = "classpath*:mapper/*.xml";
@Value("${analysis.datasource.url}")
private String url;
@Value("${analysis.datasource.username}")
private String user;
@Value("${analysis.datasource.password}")
private String password;
@Value("${analysis.datasource.driverClassName}")
private String driverClass;
@Bean(name = "analysisDataSource")
public DataSource analysisDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
System.out.println(dataSource);
return dataSource;
}
@Bean(name = "analysisTransactionManager")
public DataSourceTransactionManager analysisTransactionManager() {
return new DataSourceTransactionManager(analysisDataSource());
}
@Bean(name = "analysisSqlSessionFactory")
public SqlSessionFactory analysisSqlSessionFactory(@Qualifier("analysisDataSource") DataSource analysisDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(analysisDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(AnalysisDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
application.properties配置
#mysql 多数据源
search.datasource.url=jdbc:mysql://192.168.50.21:3306/search?useUnicode=true&characterEncoding=utf8
search.datasource.username=root
search.datasource.password=ysten123
search.datasource.driverClassName=com.mysql.jdbc.Driver
analysis.datasource.url=jdbc:mysql://192.168.50.21:3306/search_phrase?useUnicode=true&characterEncoding=utf8
analysis.datasource.username=root
analysis.datasource.password=ysten123
analysis.datasource.driverClassName=com.mysql.jdbc.Driver
user.datasource.url=jdbc:mysql://192.168.50.21:3306/springbootdb?useUnicode=true&characterEncoding=utf8
user.datasource.username=root
user.datasource.password=ysten123
user.datasource.driverClassName=com.mysql.jdbc.Driver
city.datasource.url=jdbc:mysql://192.168.50.21:3306/springbootdb_cluster?useUnicode=true&characterEncoding=utf8
city.datasource.username=root
city.datasource.password=ysten123
city.datasource.driverClassName=com.mysql.jdbc.Driver