一、前言

1、版本

基于 sharding-5.0.0

  1. <dependency>-->
  2. <groupId>org.apache.shardingsphere</groupId>-->
  3. <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>-->
  4. <version>5.0.0</version>-->
  5. </dependency>-->

2、表结构

  1. create table smiler_user.smiler_user_1
  2. (
  3. id bigint auto_increment
  4. primary key,
  5. username varchar(32) not null comment '用户名称',
  6. birthday datetime default CURRENT_TIMESTAMP not null comment '生日',
  7. gender tinyint(1) default 0 not null comment '性别 0:男 1:女',
  8. address varchar(256) null comment '地址'
  9. )
  10. charset=utf8;
  11. create table smiler_user.smiler_user_2
  12. (
  13. id bigint auto_increment
  14. primary key,
  15. username varchar(32) not null comment '用户名称',
  16. birthday datetime default CURRENT_TIMESTAMP not null comment '生日',
  17. gender tinyint(1) default 0 not null comment '性别 0:男 1:女',
  18. address varchar(256) null comment '地址'
  19. )
  20. charset=utf8;

3、测试数据

  1. INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('小秘密', '2021-01-01 03:11:11', 0, null);
  2. INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('大计划', '2021-01-01 03:11:11', 0, null);
  3. INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('大计划', '2021-01-01 03:11:11', 0, null);
  4. INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('king', '2021-01-01 03:11:11', 0, 'china');
  5. INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('supkingx', '2021-01-02 03:11:11', 1, 'china');
  6. INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('smiler', '2021-01-02 03:11:11', 1, 'china');
  7. INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('smiler1', '2021-01-02 03:11:11', 1, 'china');
  8. INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('smiler1', '2021-01-02 03:11:11', 1, 'china');
  9. INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('smiler1', '2021-01-02 03:11:11', 1, 'china');
  10. INSERT INTO smiler_user.smiler_user_1 (username, birthday, gender, address) VALUES ('superking', '2021-01-02 03:11:11', 1, 'china');
  11. INSERT INTO smiler_user.smiler_user_2 (username, birthday, gender, address) VALUES ('测试', '2021-09-20 16:16:59', 0, null);

二、快速开始

1、配置文件

  1. # 分表配置
  2. spring.shardingsphere.datasource.names=ds-0
  3. spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://localhost:3306/smiler_user?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
  4. spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
  5. spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
  6. spring.shardingsphere.datasource.ds-0.username=root
  7. spring.shardingsphere.datasource.ds-0.password=123456
  8. #指定表smiler_user的分布情况,配置表在哪个数据库里面,表名称都是什么
  9. spring.shardingsphere.rules.sharding.tables.smiler_user.actual-data-nodes=ds-0.smiler_user_$->{1..2}
  10. # 指定分片策略 约定id值偶数添加到表smiler_user_1,如果id是奇数添加到表smiler_user_2
  11. spring.shardingsphere.rules.sharding.tables.smiler_user.table-strategy.standard.sharding-column=id
  12. spring.shardingsphere.rules.sharding.tables.smiler_user.table-strategy.standard.sharding-algorithm-name=smiler-user-inline
  13. spring.shardingsphere.rules.sharding.sharding-algorithms.smiler-user-inline.type=INLINE
  14. spring.shardingsphere.rules.sharding.sharding-algorithms.smiler-user-inline.props.algorithm-expression=smiler_user_$->{id.remainder(2).add(1)}

2、demo

https://github.com/supkingx/shardingjdbcTest