Springboot2.x整合Oracle和MySql(多数据源)

有很多时候需要在项目中整合多数据源,跨数据库的那种。最近项目有使用到多数据源,写一篇博客分享给大家,也当给自己做个记录。
我使用的数据库版本为:
MySql:8.0.15
Orcale:11g(11.2.0.1.0)

1:pom文件添加配置文件:

<!-- mysql -->
<dependency>
    <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<!--orcale-->
<dependency>
    <groupId>com.jslsolucoes</groupId>
        <artifactId>ojdbc6</artifactId>
    <version>11.2.0.1.0</version>
</dependency>

2:添加链接的配置文件:

注:需要把数据库链接地址改成自己的

#第一个数据库链接对象
spring:
  datasource:
    driver-class-name: oracle.jdbc.driver.OracleDriver
    jdbc-url: jdbc:oracle:thin:@127.0.0.1:1521:ORCL
    username: system
    password:123456

custom:
  datasource:
    mysql:
      #  com.mysql.cj.jdbc.Driver 8之上的连接
      #  com.mysql.jdbc.Driver  8之下的连接
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://127.0.0.1:3306/workorder?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&allowMultiQueries=true
      username: root
      password: root

mybatis:
#  configuration:
#    log-impl: STDOUT_LOGGING
#    map-underscore-to-camel-case: true
  config-location: classpath:mybatis/mybatis.cfg.xml    #  mybatis主配置文件所在路径
#  mapper-locations:  classpath:mybatis/orcaleMapper/*.xml     #  所有的mapper映射文件

3:添加数据库的连接配置
注:里面的basePackages 地址是自己的映射地址,其中sql地址也需要写自己的

//msql配置对象
@Configuration
//这里的自己的mapper映射地址也需要写自己的 有的喜欢写dao
@MapperScan(basePackages = "yq.mysqlMapper", sqlSessionTemplateRef = "mysqlSqlSessionTemplate")
public class MysqlDataSource {

    @Bean(name = "mysqlDataSources")
    @Qualifier("mysqlDataSources")
    //这是主数据库链接对象 一定要添加的
    @Primary
    //读取数据库中的配置文件属性
    @ConfigurationProperties(prefix = "custom.datasource.mysql")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "mysqlSqlSessionFactory")
    @Primary
    public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("mysqlDataSources") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(
        new PathMatchingResourcePatternResolver()
        //这里很重要 就是指定自己的数据库sql文件的地址 其他的偶可以复制 这里需要改  
        .getResources("classpath:mybatis/mysqlMapper/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "mysqlTransactionManager")
    @Primary
    public PlatformTransactionManager secondaryTransactionManager(
            @Qualifier("mysqlDataSources") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "mysqlSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate secondarySqlSessionTemplate(
            @Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}



//orcale配置  其中的意思同上mysql
@Configuration
@MapperScan(basePackages = "yq.orcaleMapper", sqlSessionTemplateRef  = "orcaleSqlSessionTemplate")
public class OrcaleDataSource {

    @Bean(name = "orcaleDataSources")
    @Qualifier("orcaleDataSources")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "orcaleSqlSessionFactory")
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("orcaleDataSources") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/orcaleMapper/*.xml"));
        return bean.getObject();
    }

    //配置声明式事务管理器
    @Bean(name = "orcaleTransactionManager")
    public PlatformTransactionManager primaryTransactionManager(@Qualifier("orcaleDataSources") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "orcaleSqlSessionTemplate")
    public SqlSessionTemplate primarySqlSessionTemplate(
            @Qualifier("orcaleSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    
}

总结:到此基于Springboot2.x的多数据源就配置完成了,这里因为我们配置了扫包的,就自动区分了什么包需要使用什么数据源,只要是大家使用的MVC开发模式开发就应该很好理解。
最后:附上多数据源的事物

 //没错 就是这样的 
 //mysqlTransactionManager 这是我们在配置数据源的时候自己添加的
 //不同的事物只需要把mysqlTransactionManager 修改为对应的名称即可
 @Transactional(transactionManager = "mysqlTransactionManager")

问题:
1:当在一个方法需要执行两种不同的数据源的时候,只能保证一个事物,不能保证两种数据源的事物
2:需要在每一个@Transactional事物上加上transactionManager 属性,如果不加的话就是我们指定的@Primary主事物。相对来说比较麻烦。

发布了25 篇原创文章 · 获赞 9 · 访问量 3171

猜你喜欢

转载自blog.csdn.net/qq_40053836/article/details/95500654