1.配置多数据源

  1. pom文件

    1. <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
      <version>2.5.6</version>
      </dependency>
  2. yaml文件(一定对齐)  
    1. 第一种

      spring:
      datasource:
      dynamic:
      primary: db1
      datasource:
      db1:
      username: root
      password: root
      url: jdbc:mysql://127.0.0.1:3306/db1
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      db2:
      username: root2
      password: root2
      url: jdbc:mysql://127.0.0.1:3306/db2
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
    2. 第二种 
      spring:
      datasource:
      dynamic:
      datasource:
      master_db1:
      username: root
      password: root
      url: jdbc:mysql://127.0.0.1:3306/db1
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      db2:
      username: root2
      password: root2
      url: jdbc:mysql://127.0.0.1:3306/db2
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
      druid:
      initial-size: 20
      minIdle: 50 #最小连接池数量
      xxxxxxxxx 

2.设置默认数据源

  配置中的  primary: db1  , 即是指定默认使用的数据库

 spring:
datasource:
dynamic:
primary: db1

3.使用注解切换数据源

  1. 类上

    @DS("db2")
    public class xxx(){}
  2. 方法上
    @DS("db2")
    public void xxx(){}
  3.  mapper方法上
    @Select("SELECT SFZH,USER_NAME,SEX FROM T_USER_INFO WHERE A = #{A}")
    @DS("db2")
    Map<String, Object> queryView(@Param("A") String A);

4.其他配置 , 启动

修改Application注解
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)

最新文章

  1. HTML学习(二)进阶篇
  2. [BZOJ1501][NOI2005] 智慧珠游戏
  3. sys,os,模块-正则表达式
  4. python 生成 xml文件 属性的顺序问题
  5. Java synchronized 详解
  6. PHP获取Mp3文件信息
  7. java web开发时的绝对路径与相对路径
  8. iOS学习——UI相关小结
  9. studio中碰到的jni问题:java.lang.UnsatisfiedLinkError
  10. 刚学的vue.js的单一事件管理组件通信
  11. 洛谷P1731 生日蛋糕
  12. 树莓派3 之 安装Mysql服务
  13. 17-(基础入门篇)GPRS(Air202)串口
  14. java读取写入文件
  15. Docker4之Stack
  16. Hadoop读写流程
  17. docker toolbox在win7下的安装
  18. http和WebSocket
  19. Informatica bulk和normal模式
  20. Hive命令 参数

热门文章

  1. Go实现KMP和Sunday算法
  2. UGUI UI拖拽,UI连线。
  3. 【5】python之基础语法
  4. 1.CD冷却效果
  5. python学习●错误点●expected an indented block
  6. from pathlib import Path
  7. Software--Java--远程调试
  8. nginx 白名单配置
  9. redis统计用户活跃数量
  10. lua 添加的时候去重