SpringBoot双数据源配置

近期接手一个项目,根据场景要求,项目中需配置两个oracle数据源,项目不涉及事务

数据源JDBC配置

spring:
    application:
        name: qx-revenue-collect
    datasource:
        primary:
            driver-class-name: oracle.jdbc.OracleDriver
            jdbc-url: jdbc:oracle:thin:@127.0.0.1:8080/JAY
            username: JAY
            password: JAY123
        secondary:
            driver-class-name: oracle.jdbc.OracleDriver
            jdbc-url: jdbc:oracle:thin:@127.0.0.1:8081/JAY
            username: JAY
            password: JAY123

(此处踩到最大的雷就是必须要使用spring.datasource.primary.jdbc-url,平时使用的spring.datasource.primary.url是不行的)

primary:为主数据源即默认数据源

-默认数据源后面在我们引用Dao层接口时无需在指定Template,默认指向主数据源

secondary:为次数据源

-次数据源我们在引用Dao接口时需要使用

@Qualifier("secondaryJdbcTemplate")

在 Spring 中指定某个特定的 Bean,如不使用该注解则默认指向主数据源

配置JdbcTemplate

我这里配置了两个JdbcTemplate,一个指向主数据源,一个指向次数据源

@Configuration
@MapperScan(basePackages = "com.qx.jay.dao", sqlSessionFactoryRef = "primarySqlSessionFactory")
public class PrimaryDataSourceConfig {

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

    @Bean(name = "primarySqlSessionFactory")
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);

        // 加载 XML 映射文件
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:com/qx/jay/mapper/*.xml");
        sessionFactory.setMapperLocations(resources);
        return sessionFactory.getObject();
    }

    @Bean(name = "primaryTransactionManager")
    public DataSourceTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

@Configuration
@MapperScan(basePackages = "com.qx.jay.secondary", sqlSessionFactoryRef = "secondarySqlSessionFactory")
public class SecondaryDataSourceConfig {

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

    @Bean(name = "secondarySqlSessionFactory")
    public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);

        // 加载 XML 映射文件
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:com/qx/jay/secondaryMapper/*.xml");
        sessionFactory.setMapperLocations(resources);
        return sessionFactory.getObject();
    }

    @Bean(name = "secondaryTransactionManager")
    public DataSourceTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "secondaryJdbcTemplate")
    public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

到这里就搞定了双数据源配置

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值