您的位置:首页 > 游戏 > 手游 > 大连网站排名公司_手机无货源网店怎么开_台州网络推广_play商店

大连网站排名公司_手机无货源网店怎么开_台州网络推广_play商店

2024/10/5 18:30:10 来源:https://blog.csdn.net/weixin_61635597/article/details/142669339  浏览:    关键词:大连网站排名公司_手机无货源网店怎么开_台州网络推广_play商店
大连网站排名公司_手机无货源网店怎么开_台州网络推广_play商店

八,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);}}

在这里插入图片描述

分别连接多个数据库,同时分各自查询对应数据库的当中的数据表的信息成功。

最后:

“在这个最后的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的领域奋斗。感谢你们,我们总会在某个时刻再次相遇。”

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com