最近公司项目需要用到mysql和oracle数据库,就是固定的有些接口用mysql,有些接口用oracle。参考了网上很多资源,大致确定了方向和实现步骤,最终以github上一个开源实现完成了需求。链接地址springboot 和mybatis 集成 双数据源

1.application.yml 配置mysql和oracle数据源

spring:
  datasource:
    db1:
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:mysql://数据库ip地址:3306/数据库名?useUnicode=true&characterEncoding=UTF-8
      username: 用户名
      password: 密码
   db2:
      driver-class-name: oracle.jdbc.driver.OracleDriver
      type: com.alibaba.druid.pool.DruidDataSource
      url: jdbc:oracle:thin:@数据库ip地址:1521:数据库名
      username: 用户名
      password: 密码

2.配置mysql和oracle的config

这两个config配置中值得注意的是:@MapperScan扫描哪个数据源mysql还是oracle的dao层, @ConfigurationProperties(prefix = “spring.datasource.db1/db2”)指的是获取application.yml配置中哪个数据源的基本配置。@Primary是指优先被注入,优先被实现,DataBaseMysqlConfig和DataBaseOracleConfig必须指定一个优先注入的 DataSource,否则会报错。两个DataBaseXXXXConfig中必须指定SqlSessionFactory加载哪一个mappe.xml的路径。这里mapper.xml文件mysql和oracle是分包存放的,作为两个数据源切换加载mapper.xml的一种实现。

2.1DataBaseMysqlConfig

@Configuration
@Component
@MapperScan(basePackages = "xxx.dao.mysql", sqlSessionFactoryRef = "mysqlSqlSessionFactory")
public class DataBaseMysqlConfig {

    @Bean(name = "mysqlDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public  DataSource dataSource() {
        return new org.apache.tomcat.jdbc.pool.DataSource();
    }

    @Bean(name = "mysqlSqlSessionFactory")
    @Primary
    public  SqlSessionFactory sqlSessionFactoryBean() throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        sqlSessionFactoryBean.setMapperLocations(resolver
                .getResources("classpath:mapper/mysql/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

@Bean(name = "mysqlPlatformTransactionManager")
    @Primary
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
}

2.2DataBaseOracleConfig

@Configuration
@Component
@MapperScan(basePackages = "xxx.dao.oracle",sqlSessionFactoryRef = "oracleSqlSessionFactory")
public class DataBaseOracleConfig {

    @Bean(name = "oracleDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public  DataSource dataSource() {
        return new org.apache.tomcat.jdbc.pool.DataSource();
    }

    @Bean(name = "oracleSqlSessionFactory")
    public  SqlSessionFactory sqlSessionFactoryBean() throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        sqlSessionFactoryBean.setMapperLocations(resolver
                .getResources("classpath:mapper/oracle/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean(name = "oraclePlatformTransactionManager")
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
}

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐