• Springboot创建多数据源


    yml文件
    spring:
      datasource:
        dynamic:
          # 设置默认的数据源或者数据源组,默认值即为 master
          primary: master
          datasource:
            # 主库数据源
            master:
              driver-class-name: com.mysql.cj.jdbc.Driver
              url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&autoReconnect=true
              username: root
              password: xxxxx
            # 本地库数据源
            local:
              driver-class-name: com.mysql.cj.jdbc.Driver
              url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&autoReconnect=true
              username: root
              password: xxxxx
            #Doris数据源
            doris:
              driver-class-name: com.mysql.cj.jdbc.Driver
              url: jdbc:mysql://xxx.xxx.xxx.xxx:9030/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&nuLlCatalogMeansCurrent=true
              username: root
              password: xxxxx
     # ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    这里省略了其他的配置信息,多数据源的配置关注这些内容即可.

    构建每个数据对应的注解
    // 主数据源,@DS中填入yml文件中已经配置的数据源的名称即可
    @Target({ ElementType.TYPE, ElementType.METHOD })
    @Retention(RetentionPolicy.RUNTIME)
    @DS("master")
    public @interface MysqlMaster {
    }
    
    // 本地数据源,@DS中填入yml文件中已经配置的数据源的名称即可
    @Target({ ElementType.TYPE, ElementType.METHOD })
    @Retention(RetentionPolicy.RUNTIME)
    @DS("master")
    public @interface MysqlLocal {
    }
    
    // Doris数据源,@DS中填入yml文件中已经配置的数据源的名称即可
    @Target({ ElementType.TYPE, ElementType.METHOD })
    @Retention(RetentionPolicy.RUNTIME)
    @DS("doris")
    public @interface Doris {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    在Mapper中引入注解
    @Mapper
    @MysqlMaster
    public interface MasterMapper extends BaseMapper {
        User1 selectAll();
        // ...
    }
    
    @Mapper
    @MysqlLocal
    public interface LocalMapper extends BaseMapper {
        User2 selectAll();
        // ...
    }
    
    @Mapper
    @Doris
    public interface DorisMapper extends BaseMapper {
        User3 selectAll();
        // ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    多数据源的配置及使用到这里就结束了.

  • 相关阅读:
    设计模式——2_A 访问者(Visitor)
    大数据技术之——zookeeper的安装部署
    MySQL关于Count你知道多少
    《机器学习实战》学习记录-ch2
    SimpleAdmin手摸手教学之:项目架构设计2.0
    thinkphp5.1 关联查询
    如何利用PHP快速抓取音频数据?
    Presto 聚合中groupBy分组的实现
    ss928 开发记录二 设置网络 telnet连接开发板
    路径总和II-力扣113-C++
  • 原文地址:https://blog.csdn.net/AnameJL/article/details/134013363