亲测有效,喜欢的老铁双击666
配置文件
DynamicDataSourceConfig.java
/**
* 在spring容器初始化时候,实例化两个数据源对象,
*/
@Configuration
public class DynamicDataSourceConfig {
@Bean(name = "test")
@ConfigurationProperties("spring.datasource.druid.first")
public DataSource firstDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "ye")
@ConfigurationProperties("spring.datasource.druid.second")
public DataSource secondDataSource() {
return DruidDataSourceBuilder.create().build();
}
//此处为数据源切换代码。不加也可以。
@Primary
@Bean
public DynamicDataSource dataSource() {
Map<String, DataSource> targetDataSources = new HashMap<String, DataSource>();
targetDataSources.put(DataSourceNames.FIRST, firstDataSource());
targetDataSources.put(DataSourceNames.SECOND, secondDataSource());
return new DynamicDataSource(firstDataSource(), targetDataSources);
}
}
以下为两个数据源的 配置
数据源配置文件2:TestMybatisConfig.java
/**
* @MapperScan 为映射持久层的路径
* @Qualifier 从容器中获取指定实例
*/
@Configuration
@MapperScan(basePackages = {"com.javademos.train.dynamicdatasource.test.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory1",sqlSessionTemplateRef = "sqlSessionTemplate1")
public class TestMybatisConfig {
@Autowired
@Qualifier("test")
private DataSource test;
@Bean(name = "sqlSessionFactory1")
@Primary
public SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(test); // 使用titan数据源, 连接titan库
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
//注意此处,需要加上xml文件来寻找mybatis 的sql文件。
return factoryBean.getObject();
}
@Bean(name = "sqlSessionTemplate1")
@Primary
public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory) throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
return template;
}
/**
* 事务
* @param
* @return
*/
@Bean(name = "test1TransactionManager")
@Primary
public PlatformTransactionManager testTransactionManager() {
return new DataSourceTransactionManager(test);
}
}
数据源配置文件2 :YeMybatisConfig.java
@Configuration
@MapperScan(basePackages = {"com.javademos.train.dynamicdatasource.ye.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory2",sqlSessionTemplateRef = "sqlSessionTemplate2")
public class YeMybatisConfig {
@Autowired
@Qualifier("ye")
private DataSource ye;
@Bean(name = "sqlSessionFactory2")
public SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(ye); // 使用titan数据源, 连接titan库
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
return factoryBean.getObject();
}
@Bean(name = "sqlSessionTemplate2")
public SqlSessionTemplate sqlSessionTemplate2(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
return template;
}
/**
* 事务
* @param
* @return
*/
@Bean(name = "test2TransactionManager")
public PlatformTransactionManager testTransactionManager() {
return new DataSourceTransactionManager(ye);
}
}
数据源 yml文件
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
druid:
first:
url: jdbc:mysql://localhost:3306/test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
username: root
password:
second:
url: jdbc:mysql://localhost:3306/ye?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: