SpringBoot双数据源配置

配置文件

spring:
  #关键词检索、5G网关组件配置
  datasource:
    name: test
    type: com.alibaba.druid.pool.DruidDataSource
    #   #本地
    db:
      url: jdbc:mysql://localhost:3306/5g?allowMultiQueries=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
      username: root
      password: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver
    db2:
      url: jdbc:mysql://localhost:3306/5gg?allowMultiQueries=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
      username: root
      password: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver

写两个类接收配置文件的参数

@Component
@ConfigurationProperties(prefix = "spring.datasource.db")
public class DB {
    private static final Logger logger = LoggerFactory.getLogger(DB.class);
    private String url;
    private String username;
    private String password;
    private String driverClassName;
    省略set,get
}
@Component
@ConfigurationProperties(prefix = "spring.datasource.db2")
public class DB2 {
    private static final Logger logger = LoggerFactory.getLogger(DB2.class);
    private String url;
    private String username;
    private String password;
    private String driverClassName;
  	省略set,get
}

写两个配置类

@Configuration
//只从org.cmic.rcs.dao.db包下读取dao文件,并且该报下的dao使用本datasource
@MapperScan(basePackages = "org.cmic.rcs.dao.db", sqlSessionTemplateRef = "dbSqlSessionTemplate")
public class DBConfig {
    private static final Logger logger = LoggerFactory.getLogger(DBConfig.class);

    @Bean(name = "dbDataSource")
    //对于多数据源,必须制定primary,否则报错有2个datasource,并且,只能制定一个primary
    @Primary
    //从db从获取配置信息
    public DataSource setDataSource(DB dbc) {
        DruidDataSource ds = new DruidDataSource();
        ds.setUrl(dbc.getUrl());
        ds.setUsername(dbc.getUsername());
        ds.setPassword(dbc.getPassword());
        ds.setDriverClassName(dbc.getDriverClassName());
        return ds;
    }

    @Bean(name = "dbSqlSessionFactory")
    @Primary
    public SqlSessionFactory setSqlSessionFactory(@Qualifier("dbDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //指定mapper.xml文件存放位置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:org/cmic/rcs/dao/db/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "dbSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("dbSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
@Configuration
//只从org.cmic.rcs.dao.db2包下读取dao文件,并且该报下的dao使用本datasource
@MapperScan(basePackages = "org.cmic.rcs.dao.db2", sqlSessionTemplateRef = "db2SqlSessionTemplate")
public class DB2Config {
    private static final Logger logger = LoggerFactory.getLogger(DB2Config.class);

    @Bean(name = "db2DataSource")
    public DataSource setDataSource(DB2 dbc) {
        DruidDataSource ds = new DruidDataSource();
        ds.setUrl(dbc.getUrl());
        ds.setUsername(dbc.getUsername());
        ds.setPassword(dbc.getPassword());
        ds.setDriverClassName(dbc.getDriverClassName());
        return ds;
    }

    @Bean(name = "db2SqlSessionFactory")
    public SqlSessionFactory setSqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //设置读取xml文件的位置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:org/cmic/rcs/dao/db2/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "db2SqlSessionTemplate")
    public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

对应的dao写两个
在这里插入图片描述
对应的mapper写两个在这里插入图片描述
总体图
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值