pengShuai007 2019-01-24 14:10 采纳率: 0%
浏览 1122

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条回答 默认 最新

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

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

    评论

报告相同问题?

悬赏问题

  • ¥50 安装华大九天aether
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证