? ? ? ?在我们工作中涉及到一些场景需要我们配置多数据源的操作,之前来说我们配置数据源需要写繁琐的配置类来配置我们的数据源,哪个是默认数据源等等,而现在我们可以使用“苞米豆”为我们提供的提供的第三方工具,只需要简单配置就可以实现多数据源之间的灵活切换了!
首先需要引入第三方依赖
<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这个表,所以说在我们没有指定数据源的时候,它是会自动去匹配默认的数据源来查询!
|