个人随笔
目录
五分钟整合springboot+mybatis-plus+druid+dynamic-datasource多数据源
2023-10-19 11:13:59

版本

  • springboot 2.7.16
  • druid 1.1.22
  • mybatis-plus-boot-starter 3.4.2
  • mysql 5.7.38
  • dynamic-datasource-spring-boot-starter 4.1.3

当然别的版本应该也差不多,最多改改相关类

步骤

1、搭建一个简单springboot2.7.16+druid+mybatis-plus环境

五分钟搭建springboot2.7.16+druid+mybatis-plus环境

2、引入相关依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <!--数据库配置-->
  6. <dependency>
  7. <groupId>mysql</groupId>
  8. <artifactId>mysql-connector-java</artifactId>
  9. <version>8.0.25</version>
  10. </dependency>
  11. <!--数据源-->
  12. <dependency>
  13. <groupId>com.alibaba</groupId>
  14. <artifactId>druid</artifactId>
  15. <version>1.1.22</version>
  16. </dependency>
  17. <!--自动化配置-->
  18. <dependency>
  19. <groupId>com.alibaba</groupId>
  20. <artifactId>druid-spring-boot-starter</artifactId>
  21. <version>1.1.22</version>
  22. </dependency>
  23. <!--mybatis-plus -->
  24. <dependency>
  25. <groupId>com.baomidou</groupId>
  26. <artifactId>mybatis-plus-boot-starter</artifactId>
  27. <version>3.4.2</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>com.baomidou</groupId>
  31. <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  32. <version>4.1.3</version>
  33. </dependency>

3、配置文件引入数据源

  1. spring:
  2. autoconfigure:
  3. #自动化配置 例外处理
  4. exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
  5. datasource:
  6. type: com.alibaba.druid.pool.DruidDataSource
  7. dynamic:
  8. primary: master #设置默认的数据源或者数据源组,默认值即为mysql1
  9. strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
  10. datasource:
  11. master:
  12. url: jdbc:mysql://localhost:3306/demo?serverTimezone=Asia/Shanghai
  13. username: root
  14. password: 123456
  15. driver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
  16. slave1:
  17. url: jdbc:mysql://localhost:3306/demo2?serverTimezone=Asia/Shanghai
  18. username: root
  19. password: 123456
  20. driver-class-name: com.mysql.cj.jdbc.Driver

4、建库建表插入数据

  1. #demo1
  2. CREATE DATABASE demo1;
  3. CREATE TABLE user(
  4. userid BIGINT not null primary key COMMENT '主键ID,雪花算法生成',
  5. username VARCHAR(64) COMMENT '用户名称:这里用微信的昵称'
  6. );
  7. INSERT INTO `demo1`.`user` (`userid`, `username`) VALUES (1, '小林');
  8. INSERT INTO `demo1`.`user` (`userid`, `username`) VALUES (2, '小马');
  9. #demo2
  10. CREATE DATABASE demo2;
  11. CREATE TABLE user(
  12. userid BIGINT not null primary key COMMENT '主键ID,雪花算法生成',
  13. username VARCHAR(64) COMMENT '用户名称:这里用微信的昵称'
  14. );
  15. INSERT INTO `demo2`.`user` (`userid`, `username`) VALUES (1, '小林2');
  16. INSERT INTO `demo2`.`user` (`userid`, `username`) VALUES (2, '小马2');

5、新建User.class

  1. package com.example.demo;
  2. public class User {
  3. private String userid;
  4. private String username;
  5. public String getUserid() {
  6. return userid;
  7. }
  8. public void setUserid(String userid) {
  9. this.userid = userid;
  10. }
  11. public String getUsername() {
  12. return username;
  13. }
  14. public void setUsername(String username) {
  15. this.username = username;
  16. }
  17. @Override
  18. public String toString() {
  19. return "User{" +
  20. "userid='" + userid + '\'' +
  21. ", username='" + username + '\'' +
  22. '}';
  23. }
  24. }

6、在mapper,service或者方法上加上注解即可

MasterUserMapper.class

  1. package com.example.demo.mapper.master;
  2. import com.baomidou.dynamic.datasource.annotation.DS;
  3. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  4. import com.example.demo.User;
  5. import org.springframework.stereotype.Repository;
  6. @Repository
  7. @DS("master")
  8. public interface MasterUserMapper extends BaseMapper<User> {
  9. }

Slave1UserMapper.class

  1. package com.example.demo.mapper.slave1;
  2. import com.baomidou.dynamic.datasource.annotation.DS;
  3. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  4. import com.example.demo.User;
  5. import org.springframework.stereotype.Repository;
  6. @Repository
  7. @DS("slave1")
  8. public interface Slave1UserMapper extends BaseMapper<User> {
  9. }

7、在DemoController加上测试逻辑

  1. package com.example.demo;
  2. import com.example.demo.mapper.master.MasterUserMapper;
  3. import com.example.demo.mapper.slave1.Slave1UserMapper;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.List;
  8. @RestController
  9. public class DemoController {
  10. @Autowired()
  11. MasterUserMapper masterUserMapper;
  12. @Autowired()
  13. Slave1UserMapper slave1UserMapper;
  14. @GetMapping("/find1")
  15. public List<User> find1(){
  16. return masterUserMapper.selectList(null);
  17. }
  18. @GetMapping("/find2")
  19. public List<User> find2(){
  20. return slave1UserMapper.selectList(null);
  21. }
  22. }

8、启动类加上注解@MapperScan

  1. package com.example.demo;
  2. import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
  3. import org.mybatis.spring.annotation.MapperScan;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. @SpringBootApplication
  7. @MapperScan({"com.example.demo.mapper"})
  8. public class DemoApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(DemoApplication.class, args);
  11. }
  12. }

9、启动访问

http://localhost:8080/find1

  1. [
  2. {
  3. "userid": "1",
  4. "username": "小林1"
  5. },
  6. {
  7. "userid": "2",
  8. "username": "小马1"
  9. }
  10. ]

http://localhost:8080/find2

  1. [
  2. {
  3. "userid": "1",
  4. "username": "小林2"
  5. },
  6. {
  7. "userid": "2",
  8. "username": "小马2"
  9. }
  10. ]

搞定,还是很简单的!

10、注意事项

  • p- rimary: master配置后,全局默认master主数据源,只要没有@DS注解显式申明切换数据源,默认就是master主数据源。
  • 在同一个事务中,数据源不能切换。
  • @DS 可以注解在方法上和类上,同时存在方法注解优先于类上注解。
  • 官方强烈建议只在service的类和方法上添加注解,不建议在mapper上添加注解。(我觉得还是得看具体需求)
 257

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2