技术:SpringBoot + Mybatis-Plus + Druid

SpringBoot里做多数据源配置,可以直接使用dynamic-datasource提供的服务,简单便捷

一、POM里加入依赖包

<!--多数据源依赖-->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
      <version>3.3.1</version>
    </dependency>

二、yml增加多数据配置:

autoconfigure:
    # 自动化配置 例外处理
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    #多数据源配置
    dynamic:
      #设置默认的数据源
      primary: master
      datasource:
        # 数据库1
        master:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://localhost:3306/praticce_a?useUnicode=true&characterEncoding=utf-8&useSSL=false
          username: root
          password: e0BhauLW2yhi2se9L+7QFaBsPMVgdPR1udDSqZe75Yh5Obp8YHyRw==
          druid:
            filters: stat,slf4j,wall,config
            initial-size: 10
            max-active: 100
            max-open-prepared-statements: 20
            max-pool-prepared-statement-per-connection-size: 20
            max-wait: 60000
            min-evictable-idle-time-millis: 300000
            min-idle: 10
            pool-prepared-statements: true
            test-on-borrow: false
            test-on-return: false
            test-while-idle: true
            time-between-eviction-runs-millis: 60000
            validation-query: select 'x'
            publicKey: MFwwDQYJKoZIhvcNAr7HWXiJQjmIZN6IYPMPzfRe20da5d8EAAQ==
            connection-properties: config.decrypt=true;config.decrypt.key=${spring.datasource.dynamic.datasource.master.druid.publicKey}
        # 数据库2
        slave1:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://localhost:3306/pratice_b?useUnicode=true&characterEncoding=utf-8
          username: root
          password: EVHrRAfXPl0Qpd6j4GsodQOwvsV9Q==
          druid:
            filters: stat,slf4j,wall,config
            initial-size: 10
            max-active: 100
            max-open-prepared-statements: 20
            max-pool-prepared-statement-per-connection-size: 20
            max-wait: 60000
            min-evictable-idle-time-millis: 300000
            min-idle: 10
            pool-prepared-statements: true
            test-on-borrow: false
            test-on-return: false
            test-while-idle: true
            time-between-eviction-runs-millis: 60000
            validation-query: select 'x'
            publicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwQ==
            connection-properties: config.decrypt=true;config.decrypt.key=${spring.datasource.dynamic.datasource.msg.druid.publicKey}

说明:

  1. 数据库配置这里使用了Druid的加密方式,如果数据库连接密码不需要加密,则password可以直接使用明文,druid配置中去掉config配置(filters中去掉config,connection-properties可以都去掉)
  2. spring.datasource.dynamic.primary是用于设置默认的数据源,这个最好设置一个,因为我们不可能每个类或接口都指定数据源
  3. spring.autoconfigure.exclude是去除Druid自动装载数据库配置,也可以直接在项目启动类XXXApplication上加 @SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)

三、在需要切换数据源的类或方法上加@DS注解

@Mapper
@DS("msg")
public interface TestMapper {

    /**
     * 批量数据
     *
     * @param poList 数据
     */
    void batchAdd(@Param("poList") List<TestDTO> poList);

}

@DS优先级:方法 > 类

 

 

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐