SpringBoot2.7+ 动态数据数据源以及多数据源自动配置


目录

  • 前言
  • 一、动态数据源配置
    • 1.引入依赖库
    • 2.使用步骤
  • 二、JPA多数据源配置
    • 1.引入依赖库
    • 2.使用步骤
  • 三、mybatis多数据源配置
    • 1.引入依赖库
    • 2.使用步骤
  • 总结


前言

内容包括动态数据源以及多数据源的自动配置包括jpa和mybatis,包含源码以及使用方法。


一、动态数据源配置

1.引入依赖库

    <dependency>
        <groupId>cn.hiboot.mcngroupId>
         <artifactId>mcn-spring-boot-starterartifactId>
         <version>2.7.7version>
     dependency>

2.使用步骤

  1. 配置多个数据源
multiple.datasource.hello.url=jdbc:mysql://127.0.0.1:3306/test?createDatabaseIfNotExist=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai
multiple.datasource.hello.username=root
multiple.datasource.hello.password=123456

multiple.datasource.world.url=jdbc:mysql://127.0.0.1:3306/web_template?createDatabaseIfNotExist=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai
multiple.datasource.world.username=root
multiple.datasource.world.password=123456
  1. 启用动态数据源支持
dynamic.datasource.enable=true
  1. 使用注解SwitchSource切换数据源(数据源名称就是multiple.datasource后的第一个字符串即hello和world
@RequestMapping("test")
@RestController
@SwitchSource("hello")
public class TestRestApi {

    private UserDao userDao;
	public TestRestApi(UserDao userDao) {
        this.userDao = userDao;
    }

    @GetMapping("list")
    public RestResp<List<User>> list() {
        return new RestResp<>(userDao.findAll());
    }

    @GetMapping("list2")
    @SwitchSource("world")
    public RestResp<List<User>> list2() {
        return new RestResp<>(userDao.findAll());
    }
}    

提示:SwitchSource注解既可以用在类上也可以用在方法上,方法上的优先级高

  1. 持久层至于是用JPA还是mybatis都可以

使用jpa:

     <dependency>
           <groupId>org.springframework.bootgroupId>
           <artifactId>spring-boot-starter-data-jpaartifactId>
           <version>2.7.7version>
     dependency>

使用mybatis:

	<dependency>
	     <groupId>org.mybatis.spring.bootgroupId>
	     <artifactId>mybatis-spring-boot-starterartifactId>
	     <version>2.2.2version>
	 dependency>
  1. 源码地址

二、JPA多数据源配置

1.引入依赖库

    <dependency>
        <groupId>cn.hiboot.mcngroupId>
         <artifactId>mcn-spring-boot-starterartifactId>
         <version>2.7.7version>
     dependency>
     <dependency>
           <groupId>org.springframework.bootgroupId>
           <artifactId>spring-boot-starter-data-jpaartifactId>
      	  <version>2.7.7version>
     dependency>

2.使用步骤

  1. 配置多个数据源
multiple.datasource.hello.url=jdbc:mysql://127.0.0.1:3306/test?createDatabaseIfNotExist=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai
multiple.datasource.hello.username=root
multiple.datasource.hello.password=123456

multiple.datasource.world.url=jdbc:mysql://127.0.0.1:3306/web_template?createDatabaseIfNotExist=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai
multiple.datasource.world.username=root
multiple.datasource.world.password=123456
  1. 启用JPA多数据源
	jpa.multiple.datasource.enable=true
  1. 数据访问层位置

dao层必须在启动类所在包的子包dao下且用数据源的名称当子包名称,如下图所示
SpringBoot2.7+ 动态数据数据源以及多数据源自动配置_第1张图片

  1. Controller层使用
@RequestMapping("test")
@RestController
public class TestRestApi {

    private UserDao userDao;
    private UserDao2 userDao2;

    public TestRestApi(UserDao userDao, UserDao2 userDao2) {
        this.userDao = userDao;
        this.userDao2 = userDao2;
    }
    
    @GetMapping("list3")
    public RestResp<List<User>> list3() {
        List<User> all = userDao2.findAll();
        all.addAll(userDao.findAll());
        return new RestResp<>(all);
    }
}     
  1. 源码地址

三、mybatis多数据源配置

1.引入依赖库

    <dependency>
        <groupId>cn.hiboot.mcngroupId>
         <artifactId>mcn-spring-boot-starterartifactId>
         <version>2.7.7version>
     dependency>
     <dependency>
         <groupId>org.mybatis.spring.bootgroupId>
         <artifactId>mybatis-spring-boot-starterartifactId>
         <version>2.2.2version>
     dependency>

2.使用步骤

  1. 配置多个数据源
multiple.datasource.hello.url=jdbc:mysql://127.0.0.1:3306/test?createDatabaseIfNotExist=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai
multiple.datasource.hello.username=root
multiple.datasource.hello.password=123456

multiple.datasource.world.url=jdbc:mysql://127.0.0.1:3306/web_template?createDatabaseIfNotExist=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=Asia/Shanghai
multiple.datasource.world.username=root
multiple.datasource.world.password=123456
  1. 启用动态数据源支持
	mybatis.multiple.datasource.enable=true
  1. 数据访问层位置

dao层必须在启动类所在包的子包dao下且用数据源的名称当子包名称,如下图所示
SpringBoot2.7+ 动态数据数据源以及多数据源自动配置_第2张图片

  1. Controller层使用
@RequestMapping("test")
@RestController
@SwitchSource("hello")
public class TestRestApi {
    @GetMapping("list")
    public RestResp<List<User>> list() {
        return new RestResp<>(userDao.findAll());
    }

    @GetMapping("list2")
    @SwitchSource("world")
    public RestResp<List<User>> list2() {
        return new RestResp<>(userDao.findAll());
    }
}    
  1. 源码地址

总结

  1. 动态数据源开关和jpa多数据源开关以及mybatis多数据源开关三者同时只能开启一个
  2. 当三个开关都没开启时,默认会使用动态数据源模式
  3. jpa和mybatis的多数据源配置基本一样,引入不同的依赖就行了

你可能感兴趣的:(#,Spring,技能,mybatis,java,spring,boot)