第一种:Mybatis-Plus的dynamic-datasource
Gitee地址:https://gitee.com/baomidou/dynamic-datasource-spring-boot-starter
要实现其实很简单,一个注解就可以了
1、创建两个一库,一样的表进行测试
2、搭建SpringBoot引入dynamic-datasource依赖
<dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.6.1</version> </dependency>
3、修改SpringBoot配置
spring: datasource: dynamic: primary: master #设置默认的数据源或者数据源组,默认值即为master strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源 datasource: master: url: jdbc:mysql://localhost:3306/datasource-1?serverTimezone=UTC username: root password: xxx driver-class-name: com.mysql.cj.jdbc.Driver slave_1: url: jdbc:mysql://localhost:3306/datasource-2?serverTimezone=UTC username: root password: xxx driver-class-name: com.mysql.cj.jdbc.Driver
4、在方法上面添加@DS注解
标签:jdbc,SpringBoot,数据源,dynamic,datasource,mysql,动态 From: https://www.cnblogs.com/zhangzhixi/p/17086068.html