文章目录
- 一、mybatisplus3.x
- 1. 依赖
- 2. 启动类添加注解
- 3. 添加多数据源注解
- 4. yml
- 5. 测试类
- 6. 源码地址
为了适配新的需求,需要同时支持mysql和oracle数据库操作多数据源,因此项目中集成dynamic-datasource-spring-boot-starter
,支持很多场景。
例如:在不配置注解@DS("oracle")
的场景,默认加载master主数据源,如果配置了@DS("oracle")
则会加载配置的数据源。
一、mybatisplus3.x
1. 依赖
<!--整合Knife4j--><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>2.0.4</version></dependency><!--mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.3.0</version></dependency><!--多数据源 以下版本任选其一--><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.2.0</version></dependency><!--oracle 数据库驱动--><dependency><groupId>com.oracle</groupId><artifactId>ojdbc6</artifactId><version>11.2.0.3</version></dependency><!--druid 监控--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.13</version></dependency><!--mysql 数据库驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.21</version></dependency><!--lombok 代码工具--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--devtools热部署--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><scope>true</scope></dependency><!--springMVC 启动器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
2. 启动类添加注解
3. 添加多数据源注解
4. yml
spring:datasource:dynamic:primary: master #设置默认的数据源或者数据源组,默认值即为masterstrict: falsedatasource:master:username: rootpassword: rooturl: jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTTdriver-class-name: com.mysql.cj.jdbc.Driverslave_1:username: rootpassword: rooturl: jdbc:mysql://127.0.0.1:3306/test1?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTTdriver-class-name: com.mysql.cj.jdbc.Driverslave_2:username: rootpassword: rooturl: jdbc:mysql://127.0.0.1:3306/test2?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTTdriver-class-name: com.mysql.cj.jdbc.Driveroracle:driver-class-name: oracle.jdbc.OracleDriverurl: jdbc:oracle:thin:@127.0.0.1:1521:orclusername: orclpassword: orclfilters: wall,mergeStat
logging:level:com.gblfy: debug
5. 测试类
在浏览器访问:http://localhost:8080/doc.html
咱们写的接口就会显示,依次测试即可!
6. 源码地址
https://gitee.com/gb_90/dynamic-datasource
具体详细:请参考官网https://mybatis.plus/guide/dynamic-datasource.html