Java mybatis-plus配置动态多数据源

1、pom

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

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.6</version>
</dependency>

2、yml配置

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    dynamic:
      # 设置默认的数据源或者数据源组,默认值即为 master
      primary: master
      # 严格模式 匹配不到数据源则报错
      strict: true
      datasource:
        # 主库数据源
        master:
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: xxxxx
          url: jdbc:mysql://127.0.0.1:3306/ifssc-iot?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai
        # 从库库数据源
        slave:
          driver-class-name: com.taosdata.jdbc.rs.RestfulDriver
          url: jdbc:TAOS-RS://127.0.0.1:6041/power?charset=UTF-8&locale=en_US.UTF-8&timezone=UTC-8
          username: root
          password: xxxxx
        # 从库库数据源
        slave2:
          driver-class-name: com.taosdata.jdbc.rs.RestfulDriver
          url: jdbc:TAOS-RS://127.0.0.1:6041/power?charset=UTF-8&locale=en_US.UTF-8&timezone=UTC-8
          username: root
          password: xxxxx
      druid:
        enable: true
        max-active: 50
        min-idle: 50
        initial-size: 50
        max-wait: 60000
        time-between-eviction-runs-millis: 60000
        validation-query: SELECT 1 FROM dual
        test-on-return: false
        test-while-idle: true
        test-on-borrow: false
        async-close-connection-enable: true
        async-init: true

3、使用 @DS注解 访问

 

posted @ 2024-04-16 16:55  都是城市惹的祸  阅读(251)  评论(0)    收藏  举报