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

本文详细介绍了如何在Spring Boot中配置动态数据源以及JPA和Mybatis的多数据源。通过引入特定依赖,配置多个数据源属性,并使用`SwitchSource`注解在Controller中切换数据源,实现不同场景的数据操作。同时提供了数据访问层的组织结构建议,确保数据源的正确切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



前言

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


一、动态数据源配置

1.引入依赖库

    <dependency>
        <groupId>cn.hiboot.mcn</groupId>
         <artifactId>mcn-spring-boot-starter</artifactId>
         <version>2.7.18</version>
     </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.enabled=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.boot</groupId>
           <artifactId>spring-boot-starter-data-jpa</artifactId>
           <version>2.7.18</version>
     </dependency>

使用mybatis:

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

二、JPA多数据源配置

1.引入依赖库

    <dependency>
        <groupId>cn.hiboot.mcn</groupId>
         <artifactId>mcn-spring-boot-starter</artifactId>
         <version>2.7.18</version>
     </dependency>
     <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-data-jpa</artifactId>
      	  <version>2.7.18</version>
     </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.enabled=true
  1. 数据访问层位置

dao层必须在启动类所在包的子包dao下且用数据源的名称当子包名称,如下图所示
数据访问层位置

  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.mcn</groupId>
         <artifactId>mcn-spring-boot-starter</artifactId>
         <version>2.7.18</version>
     </dependency>
     <dependency>
         <groupId>org.mybatis.spring.boot</groupId>
         <artifactId>mybatis-spring-boot-starter</artifactId>
         <version>2.2.2</version>
     </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.enabled=true
  1. 数据访问层位置

dao层必须在启动类所在包的子包dao下且用数据源的名称当子包名称,如下图所示
数据访问层位置

  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的多数据源配置基本一样,引入不同的依赖就行了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kse_music

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值