当前位置:网站首页>ShardingSphere's unsharded table configuration combat (6)
ShardingSphere's unsharded table configuration combat (6)
2022-07-31 00:48:00 【Melting polar】
概述
场景
在实际工作中,Some tables need to participate in sharding operations,Some tables do not require sharding,那么这种场景下,sharding-jdbc该如何配置呢?
SpringBoot环境搭建
参考ShardingSphereThe level of sub-table actual combat(三)
application.properties配置
Configurations involving unallocated tables are as follows,只需要指定default-data-source-name即可.
# 默认数据库,Unsharded tables are all here
spring.shardingsphere.datasource.mx.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.mx.driver-class-name=com.mysql.cj.jdbc.Driver
# course_dbThe database does not need to be created separately,It can also be the same as other sharded databases,比如可以是edu_db_1
spring.shardingsphere.datasource.mx.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.mx.username=root
spring.shardingsphere.datasource.mx.password=123456
# 默认数据源,未分片的表默认执行库
spring.shardingsphere.sharding.default-data-source-name=mx
The full configuration is as follows
# 配置分片策略
# 配置数据源
spring.shardingsphere.datasource.names=m0,m1,m2,mx
# 默认数据库,Unsharded tables are all here
spring.shardingsphere.datasource.mx.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.mx.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.mx.url=jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.mx.username=root
spring.shardingsphere.datasource.mx.password=123456
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
# 默认数据源,未分片的表默认执行库
spring.shardingsphere.sharding.default-data-source-name=mx
# 指定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}
# in the specified librarycourse表的分片策略
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
表结构

CREATE TABLE `t_study` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`course_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
测试代码
package com.study.shardingjdbcdemo.entity;
import lombok.Data;
import javax.persistence.Id;
import javax.persistence.Table;
@Data
@Table(name = "t_study")
public class Study {
@Id
private Long id;
private String name;
private String courseName;
}
package com.study.shardingjdbcdemo.mapper;
import com.study.shardingjdbcdemo.entity.Study;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
@Repository
public interface StudyMapper extends Mapper<Study> {
}
@Test
public void defaultDb() {
Study study = new Study();
study.setName("xm");
study.setCourseName("mysql");
studyMapper.insert(study);
}
参考
边栏推荐
- Meeting OA project pending meeting, all meeting functions
- GO GOPROXY proxy Settings
- [Yugong Series] July 2022 Go Teaching Course 016-Logical Operators and Other Operators of Operators
- XSS related knowledge
- ShardingSphere之未分片表配置实战(六)
- MySQL的触发器
- 牛客网刷题训练(四)
- 【c语言课程设计】C语言校园卡管理系统
- 正则表达式密码策略与正则回溯机制绕过
- 【愚公系列】2022年07月 Go教学课程 015-运算符之赋值运算符和关系运算符
猜你喜欢

Summary of MySQL database interview questions (2022 latest version)

Mysql systemized JOIN operation example analysis

DOM系列之动画函数封装

pytorch bilinear interpolation

牛客网刷题训练(四)

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

MySQL的触发器

MySQL数据库进阶篇

Optimization of aggregate mentioned at DATA AI Summit 2022

MySQL master-slave replication and read-write separation script - pro test available
随机推荐
Go study notes (84) - Go project directory structure
The difference between substring and substr in MySQL
typescript13-类型别名
从两个易错的笔试题深入理解自增运算符
MySQL的触发器
Shell programming of conditional statements
Add text watermark to PHP image
MySQL数据库面试题总结(2022最新版)
The difference between h264 and h265 decoding
【愚公系列】2022年07月 Go教学课程 017-分支结构之IF
MySQL数据库进阶篇
ELK deployment script---pro test available
TypeScript在使用中出现的问题记录
WEB安全基础 - - -漏洞扫描器
MySQL master-slave replication and read-write separation script - pro test available
Neural Network (ANN)
registers (assembly language)
Basic usage of async functions and await expressions in ES6
typescript12-联合类型
Shell编程之条件语句