如何在Java SpringBoot项目中配置动态数据源你知道吗
作者:t梧桐树t 时间:2021-07-23 11:10:08
在我们工作中涉及到一些场景需要我们配置多数据源的操作,之前来说我们配置数据源需要写繁琐的配置类来配置我们的数据源,哪个是默认数据源等等,而现在我们可以使用“苞米豆”为我们提供的提供的第三方工具,只需要简单配置就可以实现多数据源之间的灵活切换了!
首先需要引入第三方依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.3.5</version>
</dependency>
只需要在配置文件中按照如下配置
spring:
datasource:
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
master: # 名称可以自己定义
url: jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
slave: # 名称可以自己定义
url: jdbc:mysql://localhost:3306/teachers?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
创建如下两个数据库
entity
@Data
public class Student implements Serializable {
/**
* 主键
*/
private Integer id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 性别
*/
private String sex;
private static final long serialVersionUID = 1L;
}
@Data
public class Teacher implements Serializable {
/**
* 主键
*/
private Integer id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 学科
*/
private String subject;
private static final long serialVersionUID = 1L;
}
mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mysql.mapper.StudentDao">
<select id="findAllStudent" resultType="com.example.demo.entity.Student">
select *
from student;
</select>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mysql.mapper.TeacherDao">
<select id="findAllTeacher" resultType="com.example.demo.entity.Teacher">
select *
from teacher;
</select>
</mapper>
mapper层
@Mapper
public interface StudentDao {
List<Student> findAllStudent();
}
@Mapper
public interface TeacherDao {
List<Teacher> findAllTeacher();
}
Service层
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public List<Student> findAllStudent() {
return studentDao.findAllStudent();
}
@Service
@DS("slave")
public class TeacherServiceImpl implements TeacherService {
@Autowired
private TeacherDao teacherDao;
@Override
public List<Teacher> findAllTeacher() {
return teacherDao.findAllTeacher();
}
}
下面是两个测试方法
@RestController
public class TestController {
@Autowired
private StudentService studentService;
@Autowired
private TeacherService teacherService;
/**
* 查询所有学生信息
*
* @return Student
*/
@GetMapping("/test/1")
public List<Student> findAllStudent() {
return studentService.findAllStudent();
}
/**
* 查询所有教师信息
*
* @return Teacher
*/
@GetMapping("/test/2")
public List<Teacher> findAllTeacher() {
return teacherService.findAllTeacher();
}
}
需要注意的一点是mapper和mapper.xml路径需一致否自会导致编译后找不到xml文件
下面可以来看一下测试结果:
查询学生:
查询老师:
此时不管是学生还是老师的信息是都可以查询出来的,这就全靠TeacherServiceImpl中使用的DS(“slave”)指定了数据源,假如说我们把注解去掉再来一次试试,可以看到请求报错提示students库中没有teacher这个表,所以说在我们没有指定数据源的时候,它是会自动去匹配默认的数据源来查询!
来源:https://blog.csdn.net/winerpro/article/details/120122423
标签:Java,Spring,Boot,动态,数据源
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入理解Spring中bean的生命周期介绍
2023-02-08 17:21:37
![](https://img.aspxhome.com/file/2023/8/61318_0s.png)
springboot 中整合mybatis多数据源不使用JPA
2023-03-01 08:43:02
![](https://img.aspxhome.com/file/2023/3/61823_0s.png)
Java创建子线程的两种方法
2023-11-24 07:00:05
Java命令行下Jar包打包小结
2023-01-04 19:25:44
![](https://img.aspxhome.com/file/2023/7/70517_0s.jpg)
关于idea中SpringBoot启动失败的坑
2022-07-18 13:02:24
![](https://img.aspxhome.com/file/2023/4/62234_0s.png)
dubbo将异常转换成RuntimeException的原因分析 ExceptionFilter
2023-11-24 14:25:13
![](https://img.aspxhome.com/file/2023/3/64143_0s.jpg)
Java中Synchronized的用法解析
2023-07-28 22:40:51
![](https://img.aspxhome.com/file/2023/7/62227_0s.png)
Android RecyclerBarChart绘制使用教程
2023-06-19 12:18:36
![](https://img.aspxhome.com/file/2023/1/83521_0s.png)
java中List删除时需要的注意事项
2023-11-11 01:28:00
Java使用pulsar-flink-connector读取pulsar catalog元数据代码剖析
2023-11-05 17:25:41
Android应用开发SharedPreferences存储数据的使用方法
2023-06-23 16:02:47
![](https://img.aspxhome.com/file/2023/0/82410_0s.jpg)
java基于swing实现的五子棋游戏代码
2023-09-24 17:31:17
![](https://img.aspxhome.com/file/2023/1/63381_0s.jpg)
Linux下g++编译与使用静态库和动态库的方法
2023-06-21 13:41:46
![](https://img.aspxhome.com/file/2023/2/68232_0s.jpg)
SpringBoot中并发定时任务的实现、动态定时任务的实现(看这一篇就够了)推荐
2023-01-21 13:58:45
Spring Boot+Shiro实现一个Http请求的Basic认证
2022-06-01 22:22:31
![](https://img.aspxhome.com/file/2023/5/61065_0s.png)
Java设计模式之享元模式示例详解
2022-12-08 22:19:46
![](https://img.aspxhome.com/file/2023/0/66060_0s.png)
Java Property类使用详解
2023-11-06 21:52:57
![](https://img.aspxhome.com/file/2023/7/58667_0s.png)
Java项目开发中实现分页的三种方式总结
2021-08-25 21:36:36
使用C++ Matlab中的lp2lp函数教程详解
2023-07-13 17:13:12
![](https://img.aspxhome.com/file/2023/0/103080_0s.png)
解决Springboot项目启动后自动创建多表关联的数据库与表的方案
2023-11-24 01:11:27
![](https://img.aspxhome.com/file/2023/9/59719_0s.jpg)