数据源一:主数据源 + @primary注解(处理gover数据库的查询,mapper目录 在gover下)
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
@ComponentScan(value ="com.assist.gover.impl.*", excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = org.springframework.stereotype.Controller.class) })
@MapperScan(basePackages ="com.assist.gover.dao", sqlSessionTemplateRef ="goverSqlSessionTemplate")
public class GoverDataSourceConfig {
@Autowired
private Environmentenv;
@Bean(name ="goverDataSource")
@Primary
public DataSourcegoverDataSource()throws Exception {
ComboPooledDataSource cpds =new ComboPooledDataSource();
cpds.setDriverClass(env.getProperty("spring.datasource.gover.driverClassName"));
cpds.setJdbcUrl(env.getProperty("spring.datasource.gover.url"));
cpds.setUser(env.getProperty("spring.datasource.gover.username"));
cpds.setPassword(env.getProperty("spring.datasource.gover.password"));
cpds.setIdleConnectionTestPeriod(600);
cpds.setLoginTimeout(1800);
cpds.setMaxIdleTime(600);
cpds.setCheckoutTimeout(0);
cpds.setAcquireRetryDelay(1000);
cpds.setAcquireRetryAttempts(30);
return cpds;
}
@Bean(name ="goverSqlSessionFactory")
@Primary
public SqlSessionFactorygoverSqlSessionFactory(@Qualifier("goverDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean =new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:/config/mybatis/mappers/gover/*.xml"));
return bean.getObject();
}
@Bean(name ="transactionManager") //spring 事物管理器 默认值是 transactionmanager
@Primary
public DataSourceTransactionManager goverTransactionManager(@Qualifier("goverDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean(name ="goverSqlSessionTemplate")
public SqlSessionTemplate goverSqlSessionTemplate(
@Qualifier("goverSqlSessionFactory") SqlSessionFactory sqlSessionFactory)throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
2:数据源二:第二个数据源(处理test数据库的查询,mapper目录 在test下)
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
@ComponentScan(value ="com.assist.test.impl.*", excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = org.springframework.stereotype.Controller.class) })
@MapperScan(basePackages ="com.assist.test.dao", sqlSessionTemplateRef ="goverSqlSessionTemplate")
public class testDataSourceConfig {
@Autowired
private Environmentenv;
@Bean(name ="testDataSource")
public DataSourcetestDataSource()throws Exception {
ComboPooledDataSource cpds =new ComboPooledDataSource();
cpds.setDriverClass(env.getProperty("spring.datasource.test.driverClassName"));
cpds.setJdbcUrl(env.getProperty("spring.datasource.test.url"));
cpds.setUser(env.getProperty("spring.datasource.test.username"));
cpds.setPassword(env.getProperty("spring.datasource.test.password"));
cpds.setIdleConnectionTestPeriod(600);
cpds.setLoginTimeout(1800);
cpds.setMaxIdleTime(600);
cpds.setCheckoutTimeout(0);
cpds.setAcquireRetryDelay(1000);
cpds.setAcquireRetryAttempts(30);
return cpds;
}
@Bean(name ="testSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean =new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:/config/mybatis/mappers/test/*.xml"));
return bean.getObject();
}
@Bean(name ="testTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("testDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name ="testSqlSessionTemplate")
public SqlSessionTemplategoverSqlSessionTemplate(
@Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory)throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
总结:跨库添加事物,切换
@Transactional(value ="事物管理器id") spring 这里默认值是 transactionManager,哪个库上的mapper就 加上@Transactional(value = "那个sqlsession上的事物管理器id")
这个也可以配置成动态的数据源 mapper 上加上 @DS注解 去切换数据源,@Transactional 去回滚事物