一、前言
1、版本
基于 sharding-5.0.0
<dependency>-->
<groupId>org.apache.shardingsphere</groupId>-->
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>-->
<version>5.0.0</version>-->
</dependency>-->
2、表结构
create table smiler_user.smiler_user_1
(
id bigint auto_increment
primary key,
username varchar(32) not null comment '用户名称',
birthday datetime default CURRENT_TIMESTAMP not null comment '生日',
gender tinyint(1) default 0 not null comment '性别 0:男 1:女',
address varchar(256) null comment '地址'
)
charset=utf8;
create table smiler_user.smiler_user_2
(
id bigint auto_increment
primary key,
username varchar(32) not null comment '用户名称',
birthday datetime default CURRENT_TIMESTAMP not null comment '生日',
gender tinyint(1) default 0 not null comment '性别 0:男 1:女',
address varchar(256) null comment '地址'
)
charset=utf8;
3、测试数据
INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('小秘密', '2021-01-01 03:11:11', 0, null);
INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('大计划', '2021-01-01 03:11:11', 0, null);
INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('大计划', '2021-01-01 03:11:11', 0, null);
INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('king', '2021-01-01 03:11:11', 0, 'china');
INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('supkingx', '2021-01-02 03:11:11', 1, 'china');
INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('smiler', '2021-01-02 03:11:11', 1, 'china');
INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('smiler1', '2021-01-02 03:11:11', 1, 'china');
INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('smiler1', '2021-01-02 03:11:11', 1, 'china');
INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('smiler1', '2021-01-02 03:11:11', 1, 'china');
INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('superking', '2021-01-02 03:11:11', 1, 'china');
INSERT INTO smiler_user.smiler_user_2 (username, birthday, gender, address) VALUES ('测试', '2021-09-20 16:16:59', 0, null);
二、快速开始
1、配置文件
# 分表配置
spring.shardingsphere.datasource.names=ds-0
spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://localhost:3306/smiler_user?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-0.username=root
spring.shardingsphere.datasource.ds-0.password=123456
#指定表smiler_user的分布情况,配置表在哪个数据库里面,表名称都是什么
spring.shardingsphere.rules.sharding.tables.smiler_user.actual-data-nodes=ds-0.smiler_user_$->{1..2}
# 指定分片策略 约定id值偶数添加到表smiler_user_1,如果id是奇数添加到表smiler_user_2
spring.shardingsphere.rules.sharding.tables.smiler_user.table-strategy.standard.sharding-column=id
spring.shardingsphere.rules.sharding.tables.smiler_user.table-strategy.standard.sharding-algorithm-name=smiler-user-inline
spring.shardingsphere.rules.sharding.sharding-algorithms.smiler-user-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.smiler-user-inline.props.algorithm-expression=smiler_user_$->{id.remainder(2).add(1)}