八,MyBatis-Plus 的“多数据源”的连接操作(详细说明)
文章目录
- 八,MyBatis-Plus 的“多数据源”的连接操作(详细说明)
- 最后:
在学习多数据源之前,我们先来了解一下分库分表
当一个项目的数据库的数据十分庞大时,在完成SQL操作的时候,需要检索的数据就会更多,我们会遇到性能问题,会出现SQL执行效率低的问题。
针对这个问题,我们的解决方案是,将一个数据库中的数据,拆分到多个数据库中,从而减少单个数据库的数据量,从分摊访问请求的压力和减少单个数据库数据量这两个方面,都提升了效率。
我们来演示一下,在 MybatisPlus
中,如何演示数据源切换的效果.
【1】先创建一个新的模块,将之前模块中的内容复制过来
结构如下
引入依赖(连接多个数据库需要导入如下com.baomidou
依赖)
<dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.1.0</version>
</dependency>
完整 pom.xml
的依赖信息。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.8</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.rainbowsea</groupId><artifactId>mp07</artifactId><version>0.0.1-SNAPSHOT</version><name>mp07</name><description>mp07</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.1.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/snapshot</url><releases><enabled>false</enabled></releases></repository></repositories><pluginRepositories><pluginRepository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/snapshot</url><releases><enabled>false</enabled></releases></pluginRepository></pluginRepositories></project>
创建新的数据库,提供多数据源环境
在 application.yaml
编写配置文件,指定多数据源信息。
官网帮助文档:https://baomidou.com/guides/dynamic-datasource/
spring:datasource:dynamic:primary: masterstrict: falsedatasource:master:url: jdbc:mysql://localhost:3306/mybatisplus?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=falseusername: rootpassword: MySQL123driver-class-name: com.mysql.cj.jdbc.Driverslave_1:url: jdbc:mysql://localhost:3306/mybatisplus2?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=falseusername: rootpassword: MySQL123driver-class-name: com.mysql.cj.jdbc.Driver
创建多个Service,分别使用 @DS
,@DS(的值是在 application.yaml 当中配置数据库名称)注解描述不同的数据源信息
package com.rainbowsea.service.Impl;import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rainbowsea.bean.User;
import com.rainbowsea.mapper.UserMapper;
import com.rainbowsea.service.UserService;
import org.springframework.stereotype.Service;@Service
@DS("master")
public class UserServiceImpl extends ServiceImpl<UserMapper, User>implements UserService {}
package com.rainbowsea.service.Impl;import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rainbowsea.bean.User;
import com.rainbowsea.mapper.UserMapper;
import com.rainbowsea.service.UserService;
import org.springframework.stereotype.Service;@Service
@DS("slave_1")
public class UserServiceImpl2 extends ServiceImpl<UserMapper, User>implements UserService {
}
测试 service 多数据源环境执行结果;
测试连接
mybatisplus
数据库的查询结果:import com.rainbowsea.bean.User; import com.rainbowsea.service.Impl.UserServiceImpl; import com.rainbowsea.service.Impl.UserServiceImpl2; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;@SpringBootTest class Mp07ApplicationTests {@Resourceprivate UserServiceImpl userService;@Testpublic void select1() {User user = userService.getById(1L);System.out.println(user);} }
测试连接
mybatisplus2
数据库的查询结果:import com.rainbowsea.bean.User; import com.rainbowsea.service.Impl.UserServiceImpl; import com.rainbowsea.service.Impl.UserServiceImpl2; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;@SpringBootTest class Mp07ApplicationTests {@Resourceprivate UserServiceImpl2 userService2;@Testpublic void select2() {User user = userService2.getById(1L);System.out.println(user);}}
分别连接多个数据库,同时分各自查询对应数据库的当中的数据表的信息成功。
最后:
“在这个最后的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的领域奋斗。感谢你们,我们总会在某个时刻再次相遇。”