目录
-
pom配置
shardingsphere-jdbc-core-spring-boot-starter版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<!--分库分表-->
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.1.2</version>
</dependency>
</dependencies>
-
properties配置
# 数据库
spring.shardingsphere.datasource.names=ds0
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://ip:3306/shequ?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=root
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
# 分布式序列算法类型
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline-user.type=INLINE
# 分片算法属性配置,使用properties格式会爆红,但是不影响使用
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline-user.props.algorithm-expression=t_user_$->{year}
# 逻辑表名称
spring.shardingsphere.rules.sharding.tables.t_user.logic-table=t_user
# 主键
spring.shardingsphere.rules.sharding.tables.t_user.key-generate-strategy.column=id
# 主键生成策略UUID、SNOWFLAKE、LEAF、CUSTOMIZED
spring.shardingsphere.rules.sharding.tables.t_user.key-generate-strategy.key-generator-name=SNOWFLAKE
# 物理表
spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodes=ds0.t_user_$->{['2021','2022','2023']}
# 基于哪个属性进行分片
spring.shardingsphere.rules.sharding.tables.t_user.table-strategy.standard.sharding-column=year
# 分片算法名称
spring.shardingsphere.rules.sharding.tables.t_user.table-strategy.standard.sharding-algorithm-name=table-inline-user
ShardingSphere 的 table-strategy 用于指定表的分片策略,其包括了以下几种类型:
1. standard
标准分片策略,表示使用标准的分片算法进行分片,通常需要指定分片列和具体的分片算法。
2. complex
复合分片策略,表示将多个分片键组合成复合分片键进行分片。例如,可以将 user_id 和 order_id 两个字段组合成复合分片键,然后使用具体的分片算法将数据分散到不同的数据节点中。
3. hint
基于 Hint 的分片策略,表示在 SQL 中手动指定分片节点信息。当程序无法根据分片键自动路由到正确的数据节点时,可以使用该分片策略手动指定分片节点信息。
4. inline
内联分片策略,表示使用类似于 Groovy 脚本的方式来计算分片,通常使用场景是一些特殊的分片需求。
5. none
不适用分片策略,表示直接将数据路由到默认的数据节点中,适用于不需要分片的表。
需要注意的是,在实际使用时,我们还需要根据具体的业务需求来选择具体的分片算法、分片键等。同时,由于不同的数据库产品间的语法支持差异,可能需要对配置进行微调和调整。
-
实体类
@Data
@TableName("t_user")
public class User {
private Long id;
private String name;
private Integer age;
private String email;
private Integer year;
private SexEnum sex;
}
-
数据库表结构
create table t_user_2021
(
id bigint(64) not null
primary key,
name varchar(20) not null,
age int null,
email varchar(20) null,
year int null comment '年份',
sex int null comment '性别'
)charset = utf8;
create table t_user_2022
(
id bigint(64) not null
primary key,
name varchar(20) not null,
age int null,
email varchar(20) null,
year int null comment '年份',
sex int null comment '性别'
)charset = utf8;
create table t_user_2023
(
id bigint(64) not null
primary key,
name varchar(20) not null,
age int null,
email varchar(20) null,
year int null comment '年份',
sex int null comment '性别'
) charset = utf8;
-
按照年份进行分表配置
# 基于哪个属性进行分片 spring.shardingsphere.rules.sharding.tables.t_user.table-strategy.standard.sharding-column=year # 分片算法属性配置,使用properties格式会爆红,但是不影响使用 spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline-user.props.algorithm-expression=t_user_$->{year}
-
按照ID进行分表配置
# 基于哪个属性进行分片 spring.shardingsphere.rules.sharding.tables.t_user.table-strategy.standard.sharding-column=id # 分片算法属性配置,使用properties格式会爆红,但是不影响使用 spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline-user.props.algorithm-expression=t_user_$->{id%2}此时的数据库表结构应该为:t_user_0、t_user_1
create table t_user_0 ( ... )charset = utf8; create table t_user_1 ( ... )charset = utf8;
测试:直接基于Mybatis-plus进行增删改查即可