pengShuai007
pengShuai007
采纳率0%
2019-01-24 14:10 阅读 1.1k

springboot+mybatis切换数据源问题

问题描述

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();
    }

问题

感觉数据源创建什么的都没问题,但是最后一步切换不生效,不知道什么问题,还请大神赐教

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • qq_37461349 不喜欢吃猫的鱼 2019-01-24 15:10

    试试用aop做切换数据源头,我是springmvc+mybaris的项目也做了数据源切换,mysql是主数据源,sqlserver是副数据源

    点赞 评论 复制链接分享

相关推荐