当前位置:网站首页>The level of ShardingSphere depots in actual combat (4)
The level of ShardingSphere depots in actual combat (4)
2022-07-31 00:50:00 【Melting polar】
概述
场景
创建两个数据库
环境搭建
建库、建表

CREATE TABLE `course_1` (
`cid` bigint(20) NOT NULL,
`cname` varchar(50) NOT NULL,
`user_id` bigint(20) NOT NULL,
`cstatus` varchar(10) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
springboot环境搭建
配置分片规则
# 配置分片策略
# 配置数据源
spring.shardingsphere.datasource.names=m1,m2
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/edu_db_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=123456
spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://localhost:3306/edu_db_2?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=123456
# 指定course表分布情况,配置表在哪个数据库中,表名称都是什么
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m$->{
1..2}.course_$->{
1..2}
# 指定course主键cid生成策略SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
# 指定表分片策略, cid是偶数添加到course_1表中,cid是奇数添加到course_2
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{
cid % 2 + 1}
# 指定库的中course表的分片策略
spring.shardingsphere.sharding.tables.course.database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=m$->{
user_id % 2 + 1}
# 打开sql输出日志
spring.shardingsphere.props.sql.show=true
# 解决一个Course实体类不能对应两张表的问题
spring.main.allow-bean-definition-overriding=true
编写测试代码
@Test
void addFk() {
for (int i = 0; i < 10; i++) {
Course course = new Course();
course.setCname("java");
course.setUserId(101L);
course.setCstatus("normal");
courseMapper.insertSelective(course);
}
}
@Test
public void findCourseDb() {
Example example = new Example(Course.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("userId", 100L);
criteria.andEqualTo("cid", 760185728628948993L);
Course course = courseMapper.selectOneByExample(example);
System.out.println(course);
}
边栏推荐
猜你喜欢
![[C language course design] C language campus card management system](/img/36/9e1a97767bbe1e504d8a3cc3ce5448.png)
[C language course design] C language campus card management system
![[In-depth and easy-to-follow FPGA learning 13---------Test case design 1]](/img/1c/a88ba3b01d2e2302c26ed5f730b956.png)
[In-depth and easy-to-follow FPGA learning 13---------Test case design 1]

Jmeter parameter transfer method (token transfer, interface association, etc.)

Can deep learning solve the parameters of a specific function?

In-depth understanding of the auto-increment operator from two error-prone written test questions

typescript17-函数可选参数

Summary of MySQL database interview questions (2022 latest version)

ShardingSphere之垂直分库分表实战(五)

mysql索引失效的常见9种原因详解

论文理解:“Designing and training of a dual CNN for image denoising“
随机推荐
typescript16-void
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
MySQL高级-六索引优化
Rocky/GNU之Zabbix部署(3)
Meeting OA project pending meeting, all meeting functions
DOM系列之 offset 系列
Bypass of xss
Add text watermark to PHP image
redis学习
This project is so geeky
Solution: Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
DNS resolution process [visit website]
SereTOD2022 Track2 Code Analysis - Task-based Dialogue Systems Challenge for Semi-Supervised and Reinforcement Learning
响应式布局与px/em/rem的比对
Huawei's "genius boy" Zhihui Jun has made a new work, creating a "customized" smart keyboard from scratch
Jmeter parameter transfer method (token transfer, interface association, etc.)
TypeScript在使用中出现的问题记录
Adding, deleting, modifying and checking the foundation of MySQL
过滤器(Filter)
Understand from the 11 common examples of judging equality of packaging types in the written test: packaging types, the principle of automatic boxing and unboxing, the timing of boxing and unboxing, a