SpringBoot配置多数据源

前言

项目中用到了两个数据库,分别是Oracle和Mysql,涉及到了多数据源问题,这里做下记录
官方讲解:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
日志JDBC配置:https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_LogFilter
Druid常见问题汇总:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98

配置

  • 首先配置application.yml文件,这里利用阿里的Druid作为连接池,base和follow分别代表主库和从库
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    base:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: oracle.jdbc.driver.OracleDriver
      initialize: true #指定初始化数据源,是否用data.sql来初始化,默认: true
      name: base
      jdbc-url: jdbc:oracle:thin:@189.126.156.396:9522:oratest
      username: 用户名
      password: 密码
    follow:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      initialize: true
      name: follow
      url: jdbc:mysql://162.66.66.66:3666/paygateway
      username: 你的歌用户名
      password: 你的密码
---
#下面是分页和日志相关内容,不需要可以删掉
pagehelper:
  reasonable: true
  helperDialect: oracle
  support-methods-arguments: true
  params: count=countSql
mybatis:
  configuration:
    mapUnderscoreToCamelCase: true

logging:
  level:
    com:
      xxx:
        paygateway:
          dao: DEBUG
    org:
      spring:
        springboot:
          dao: DEBUG
      springframework: WARN
  • 配置主库Configuration,注意prefix前缀和@Primary注解(表示主库)和下划线转驼峰方法的配置
@Configuration
@MapperScan(basePackages = "com.xxx.paygateway.dao.db1", sqlSessionTemplateRef = "baseSqlSessionTemplate")
public class BaseDataSourceConfig {
    //这里配置数据源
    @Bean(name = "baseDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.base")
    @Primary 
    public DataSource setDataSource() {
        return DataSourceBuilder.create().build();
    }

    //这里配置事务管理器
    @Bean(name = "baseTransactionManager")
    @Primary
    public DataSourceTransactionManager setTransactionManager(@Qualifier("baseDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    //这里配置SqlSessionFactory,连接工厂
    @Bean(name = "baseSqlSessionFactory")
    @Primary
    public SqlSessionFactory setSqlSessionFactory(@Qualifier("baseDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        //此处配置下划线转驼峰
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(configuration);
        bean.setDataSource(dataSource);
        //此处配置mybatis扫描路径
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/base/*.xml"));
        return bean.getObject();
    }

    //这里配置SqlSessionTemplate,标准操作模板
    @Bean(name = "baseSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("baseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
  • 配置从库Configuration,与从库基本一致,注意注解和前缀就可以了
@Configuration
@MapperScan(basePackages = "com.xxx.paygateway.dao.db2", sqlSessionTemplateRef = "fSqlSessionTemplate")
public class FollowDataSourceConfig {
    @Bean(name = "fDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.follow")
    public DataSource setDataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "fTransactionManager")
    public DataSourceTransactionManager setTransactionManager(@Qualifier("fDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "fSqlSessionFactory")
    public SqlSessionFactory setSqlSessionFactory(@Qualifier("fDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(configuration);
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/follow/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "fSqlSessionTemplate")
    public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("fSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
  • 把两个数据库的dao层和mapper.xml文件分到不同的文件夹下面,1归1,2归2。service层正常调用即可!


    12062369-53b9081ee9ebebe0.png
    image.png

    12062369-56e2b5d2ffe2bc09.png
    image.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值