平面设计鉴赏网站,做门户网站挣钱吗,网站未备案什么意思,低价网站建设案例模拟多数据源环境
多数据源
适用于多种场景#xff1a;纯粹多库、 读写分离、 一主多从、 混合模式等
目前我们就来模拟一个纯粹多库的一个场景#xff0c;其他场景类似
场景说明#xff1a;
我们创建两个库#xff0c;分别为#xff1a;mybatis_plus#xff08;以前…模拟多数据源环境
多数据源
适用于多种场景纯粹多库、 读写分离、 一主多从、 混合模式等
目前我们就来模拟一个纯粹多库的一个场景其他场景类似
场景说明
我们创建两个库分别为mybatis_plus以前的库不动与mybatis_plus_1新建将mybatis_plus库的product表移动到mybatis_plus_1库这样每个库一张表通过一个测试用例分别获取用户数据与商品数据如果获取到说明多库模拟成功
创建数据库及表
CREATE DATABASE mybatis_plus_1 /*!40100 DEFAULT CHARACTER SET utf8mb4 */; use mybatis_plus_1; CREATE TABLE product ( id BIGINT(20) NOT NULL COMMENT 主键ID,name VARCHAR(30) NULL DEFAULT NULL COMMENT 商品名称, price INT(11) DEFAULT 0 COMMENT 价格, version INT(11) DEFAULT 0 COMMENT 乐观锁版本号, PRIMARY KEY (id) );添加测试数据
INSERT INTO product (id, NAME, price) VALUES (1, 外星人笔记本, 100);删除mybatis_plus库product表
use mybatis_plus;
DROP TABLE IF EXISTS product;引入依赖 !-- mybatis plus 启动器--dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.5.1/version/dependency!-- lombok用于简化实体类开发--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependency!-- mysql驱动--dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope/dependencydependencygroupIdcom.baomidou/groupIdartifactIddynamic-datasource-spring-boot-starter/artifactIdversion3.5.0/version/dependency配置多数据源
spring:# 配置数据源信息datasource:dynamic:# 设置默认的数据源或者数据源组默认值为masterprimary: master #设置默认的数据源或者数据源组,默认值即为master如果读者只是单数据源只需要注释掉slave相关配置即可这里为了方便演示master与slave保持相同# 严格匹配数据源默认为falsetrue未匹配到指定数据源时抛出异常false使用默认数据源strict: falsedatasource:master:url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezoneHongkongallowMultiQueriestrueuseUnicodetruecharacterEncodingUTF-8autoReconnecttrueuseSSLfalse # serverTimezoneHongkong 需要填上时区username: rootpassword: passworddriverClassName: com.mysql.cj.jdbc.Driverslave_1:url: jdbc:mysql://localhost:3306/mybatis_plus_1?serverTimezoneHongkongallowMultiQueriestrueuseUnicodetruecharacterEncodingUTF-8autoReconnecttrueuseSSLfalse # serverTimezoneHongkong 需要填上时区username: rootpassword: passworddriverClassName: com.mysql.cj.jdbc.Driver
创建用户service
package com.xxxx.mybatisplus02.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.xxxx.mybatisplus02.pojo.User;public interface UserService extends IServiceUser {}
package com.xxxx.mybatisplus02.service.Impl;import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.mybatisplus02.mapper.UserMapper;
import com.xxxx.mybatisplus02.pojo.User;
import com.xxxx.mybatisplus02.service.UserService;
import org.springframework.stereotype.Service;DS(master)
Service
public class UserServiceImpl extends ServiceImplUserMapper, User implements UserService {}
创建商品service
package com.xxxx.mybatisplus02.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.xxxx.mybatisplus02.pojo.Product;public interface ProductService extends IServiceProduct {}
package com.xxxx.mybatisplus02.service.Impl;import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.mybatisplus02.mapper.ProductMapper;
import com.xxxx.mybatisplus02.pojo.Product;
import com.xxxx.mybatisplus02.service.ProductService;
import org.springframework.stereotype.Service;DS(slave_1)
Service
public class ProductServiceImpl extends ServiceImplProductMapper, Product implements ProductService {
}
测试
package com.xxxx.mybatisplus02;import com.xxxx.mybatisplus02.service.ProductService;
import com.xxxx.mybatisplus02.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;SpringBootTest
class Mybatisplus02ApplicationTests {Autowiredprivate UserService userService;Autowiredprivate ProductService productService;Testpublic void test(){System.out.println(userService.getById(1));System.out.println(productService.getById(1));}} 结果
都能顺利获取对象则测试成功如果我们实现读写分离将写操作方法加上主库数据源读操作方法加上从库数据源自动切换是不是就能实现读写分离
DS 可以注解在方法上或类上同时存在就近原则 方法上注解 优先于 类上注解。