问题描述
springboot+mybatis想实现oracle和Mysql数据库切换,目前用 Mysql作为 主数据源时,
可以正常查询;用Oracle作为主数据源时,也可以正常查询;目前想实现,Mysql作为主数据源时,也可以切换到另外一个数据源。
相关代码如下
创建数据源
创建Mysql数据源
@Bean(name = "dataSource")
@Primary
public ComboPooledDataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass(jdbcDriver);
comboPooledDataSource.setJdbcUrl(jdbcUrl);
comboPooledDataSource.setUser(jdbcUsername);
comboPooledDataSource.setPassword(jdbcPassword);
//连接关闭不自动提交
comboPooledDataSource.setAutoCommitOnClose(false);
return comboPooledDataSource;
}
@Bean(name="sqlSessionFactory")
@Primary
public SqlSessionFactoryBean createSqlSessionFactoryBean(@Qualifier("dataSource") DataSource dataSource)
throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(mybatisConfigFilePath));
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String packageSearchPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperPath;
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageSearchPath));
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage(entityPackage);
return sqlSessionFactoryBean;
}
创建oracle数据源
@Bean(name="oracleDataSource")
public ComboPooledDataSource createOracleDataSource() throws PropertyVetoException {
ComboPooledDataSource comboPooledOracleDataSource = new ComboPooledDataSource();
comboPooledOracleDataSource.setDriverClass(oracleJdbcDriver);
comboPooledOracleDataSource.setJdbcUrl(oracleJdbcUrl);
comboPooledOracleDataSource.setUser(oracleJdbcUser);
comboPooledOracleDataSource.setPassword(oracleJdbcPassword);
comboPooledOracleDataSource.setAutoCommitOnClose(false);
return comboPooledOracleDataSource;
}
@Bean(name="oracleSqlSessionFactory")
public SqlSessionFactoryBean createSqlSessionFactoryBean(@Qualifier("oracleDataSource") DataSource oracleDataSource)
throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(mybatisConfigFilePath));
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String packageSearchPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + oracleMapperPath;
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageSearchPath));
sqlSessionFactoryBean.setDataSource(oracleDataSource);
sqlSessionFactoryBean.setTypeAliasesPackage(entityPackage);
return sqlSessionFactoryBean;
}
事物管理代码段
@Autowired
@Qualifier("dataSource")
private DataSource dataSource;
@Autowired
@Qualifier("oracleDataSource")
private DataSource oracleDataSource;
@Bean(name="localMysql")
public PlatformTransactionManager localMysqlManager(){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name="localOracle")
public PlatformTransactionManager oracleManager(){
return new DataSourceTransactionManager(oracleDataSource);
}
切换数据源代码段
@Override
@Transactional(value = "localOracle")
public List<Map> listOracle() {
return helloOracleDao.listHello();
}
问题
感觉数据源创建什么的都没问题,但是最后一步切换不生效,不知道什么问题,还请大神赐教