有时候我们的项目并不能满足于一个数据库,不论是共用其他的数据库,还是做数据库的读写分离,都要用到多数据源。
多数据源肯定是要有多个连接 的各项数据了
首先在springBoot的application.xml中添加配置
#
spring.datasource.wjw.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.wjw.username=root
spring.datasource.wjw.password=1754
spring.datasource.wjw.jdbc-url=jdbc:mysql://localhost:3306/wjw?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
#
#
spring.datasource.ycy.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.ycy.username=root
spring.datasource.ycy.password=1754
spring.datasource.ycy.jdbc-url=jdbc:mysql://localhost:3306/ycy?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
前面半段的名字可以随便启,但后半段,driver-class-name username 等一定要按照这种格式起。
接着用java的方式配置
第一个数据源
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper.wjw", sqlSessionTemplateRef = "wjwSqlSessionTemplate")
public class WjwDataSourceConfig {
//配置数据源
@Bean(name = "wjwDataSource")
@ConfigurationProperties(prefix = "spring.datasource.wjw")
public DataSource setDataSource(){
return DataSourceBuilder.create().build();
}
//配置bean工厂并扫描xml文件
@Bean(name = "wjwSqlSessionFactory")
public SqlSessionFactory setSqlSessionFactory(@Qualifier("wjwDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:sumMapper/wjwMapper/*.xml"));
return bean.getObject();
}
//配置事务
@Bean(name = "wjwTransactionManager")
public DataSourceTransactionManager setTransactionMapper(@Qualifier("wjwDataSource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
//
@Bean(name = "wjwSqlSessionTemplate")
public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("wjwSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
第二个数据源
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper.ycy" ,sqlSessionTemplateRef = "ycySqlSessionTemplate")
public class YcyDataSourceConfig {
@Bean(name = "ycyDataSource")
@ConfigurationProperties(prefix = "spring.datasource.ycy")
public DataSource setDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "ycySqlSessionFactory")
public SqlSessionFactory setSqlSessionFactory(@Qualifier("ycyDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:sumMapper/ycyMapper/*.xml"));
return bean.getObject();
}
@Bean(name = "ycyTransactionMapper")
public DataSourceTransactionManager setTransactionMapper(@Qualifier("ycyDataSource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "ycySqlSessionTemplate")
public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("ycySqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
最后只需要在相应的service中注入想要的mapper接口就行,是可以同时注入两个数据源的mapper接口。
望不足之处大神指出,加以学习改正。