当前位置:网站首页>ShardingSphere's vertical sub-database sub-table actual combat (5)
ShardingSphere's vertical sub-database sub-table actual combat (5)
2022-07-31 00:49:00 【Rongji】
概述
场景

建库、建表

CREATE TABLE `t_user` (
`user_id` bigint(20) NOT NULL,
`username` varchar(255) DEFAULT NULL,
`ustatus` varchar(255) DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Springboot环境搭建
编写代码
创建User实体类、mapper
package com.study.shardingjdbcdemo.entity;
import lombok.Data;
import javax.persistence.Id;
import javax.persistence.Table;
@Data
@Table(name = "t_user")
public class User {
@Id
private Long userId;
private String username;
private String ustatus;
}
package com.study.shardingjdbcdemo.mapper;
import com.study.shardingjdbcdemo.entity.User;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
@Repository
public interface UserMapper extends Mapper<User> {
}
application.properties配置
# 配置分片策略
# 配置数据源
spring.shardingsphere.datasource.names=m0,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
spring.shardingsphere.datasource.m0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m0.url=jdbc:mysql://localhost:3306/user_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m0.username=root
spring.shardingsphere.datasource.m0.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}
# 指定t_user表分布情况,配置表在哪个数据库中,表名称都是什么
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=m0.t_user
# 指定t_user主键user_id生成策略SNOWFLAKE
spring.shardingsphere.sharding.tables.t_user.key-generator.column=user_id
spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE
# 指定表分片策略
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression=t_user
# 打开sql输出日志
spring.shardingsphere.props.sql.show=true
# 解决一个Course实体类不能对应两张表的问题
spring.main.allow-bean-definition-overriding=true
测试
@Test
void addUser() {
for (int i = 0; i < 10; i++) {
User user = new User();
user.setUsername("lucy");
user.setUstatus("good");
userMapper.insertSelective(user);
}
}
@Test
public void findUser() {
Example example = new Example(User.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("userId", 760195997497294849L);
User user = userMapper.selectOneByExample(example);
System.out.println(user);
}
边栏推荐
- Why use high-defense CDN when financial, government and enterprises are attacked?
- typescript9-常用基础类型
- ELK deployment script---pro test available
- What is Promise?What is the principle of Promise?How to use Promises?
- MySql data recovery method personal summary
- 响应式布局与px/em/rem的比对
- The difference between substring and substr in MySQL
- [Yugong Series] July 2022 Go Teaching Course 016-Logical Operators and Other Operators of Operators
- API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
- BOM系列之history对象
猜你喜欢
![[Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes](/img/52/88ad349eca136048acd0f328d4f33c.png)
[Tang Yudi Deep Learning-3D Point Cloud Combat Series] Study Notes

Preparations for web vulnerabilities

A complete guide to avoiding pitfalls for the time-date type "yyyy-MM-dd HHmmss" in ES

MySQL database advanced articles

MySQL数据库面试题总结(2022最新版)

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
![Xss target drone training [success when pop-up window is realized]](/img/c1/61280bddc9a42a3827735dc31c86e5.png)
Xss target drone training [success when pop-up window is realized]

The difference between h264 and h265 decoding

程序员工作三年攒多少钱合适?

C language force buckles the rotating image of the 48th question.auxiliary array
随机推荐
XSS related knowledge
Unity2D horizontal version game tutorial 4 - item collection and physical materials
ES6中 async 函数、await表达式 的基本用法
mysql主从复制及读写分离脚本-亲测可用
Niuke.com question brushing training (4)
typescript12 - union types
ShardingSphere's unsharded table configuration combat (6)
The difference between substring and substr in MySQL
SereTOD2022 Track2代码剖析-面向半监督和强化学习的任务型对话系统挑战赛
[Deep learning] Detailed explanation of Transformer model
Huawei's "genius boy" Zhihui Jun has made a new work, creating a "customized" smart keyboard from scratch
Zabbix干啥用?
typescript14-(单独指定参数和返回值的类型)
TypeScript在使用中出现的问题记录
Oracle has a weird temporary table space shortage problem
ES 中时间日期类型 “yyyy-MM-dd HHmmss” 的完全避坑指南
typescript13 - type aliases
Mini Program - Global Data Sharing
(5) fastai application
ABC 261 F - Sorting Color Balls (reverse pair)