https://shardingsphere.apache.org/document/5.2.0/cn/user-manual/shardingsphere-jdbc/spring-boot-starter/rules/sharding/
当前我们演示的是水平分表
CREATE TABLE `address_0` (
`id` bigint(20) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`addr` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `address_1` (
`id` bigint(20) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`addr` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.1version>
dependency>
<dependency>
<groupId>org.apache.shardingspheregroupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starterartifactId>
<version>5.2.0version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
server.port=8200
spring.application.name=sharding
## 单节点
spring.shardingsphere.mode.type=Standalone
## 日志打印
spring.shardingsphere.props.sql-show=true
# 配置真实数据源,多个英文逗号间隔
spring.shardingsphere.datasource.names=ds1
# 配置第 1 个数据源
## 格式 spring.shardingsphere.datasource.<定义的数据源名称>.
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://192.168.1.184:3306/db1?useSSL=false&characterEncoding=UTF8
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=root
## 格式 spring.shardingsphere.rules.sharding.tables.<定义的表名称>.
spring.shardingsphere.rules.sharding.tables.address.actual-data-nodes=ds1.address_$->{0..1}
spring.shardingsphere.rules.sharding.tables.address.table-strategy.standard.sharding-column=id
spring.shardingsphere.rules.sharding.tables.address.table-strategy.standard.sharding-algorithm-name=address-inline
## 主键标识 以及生产策略
spring.shardingsphere.rules.sharding.tables.address.key-generate-strategy.column=id
spring.shardingsphere.rules.sharding.tables.address.key-generate-strategy.key-generator-name=snowflake
## 主键取模存储具体那张表上
spring.shardingsphere.rules.sharding.sharding-algorithms.address-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.address-inline.props.algorithm-expression=address_$->{id % 2}
## sharding主键生成策略
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
## 开启mybatis-plus的扫描位置
mybatis-plus.mapper-locations=classpath*:com/cloud/**/xml/*Mapper.xml
@Data
public class Address {
@TableId(value = "id")
private Long id;
@TableField("name")
private String name;
@TableField("addr")
private String addr;
}
@RestController
public class AddressController {
@Autowired
private AddressService addressService;
private static String[] addrArrays = new String[]{"青岛","北京","上海","遂州","杭州","烟台","内蒙古","海南","台湾","香港","石家庄"};
private static String[] fisterName = new String[]{"张","北","上","遂","杭","烟","古","海","台","港","石"};
@GetMapping("/saveAddr")
public void save(){
int nextName = new Random().nextInt(10);
List<Address> addressList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Address address = new Address();
int nextInt = new Random().nextInt(10);
address.setName(fisterName[nextName]+"-"+i);
address.setAddr(addrArrays[nextInt]);
addressList.add(address);
}
addressService.saveBatch(addressList);
}
@GetMapping("/getAddr")
public Object get(){
return addressService.list(new QueryWrapper<Address>().last(" LIMIT 10 "));
}
}
server:
port: 8200
spring:
application:
name: sharding
shardingsphere:
mode:
type: Standalone
props:
sql-show: true
datasource:
names: db-0,db-1
db-0:
jdbc-url: jdbc:mysql://192.168.1.184:3306/ddd1?useSSL=false&characterEncoding=UTF8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
db-1:
jdbc-url: jdbc:mysql://192.168.1.184:3306/bbb2?useSSL=false&characterEncoding=UTF8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
rules:
sharding:
tables:
address:
##分库策略
database-strategy:
standard:
sharding-column: id
sharding-algorithm-name: id-inline
actual-data-nodes: db-$->{0..1}.address_$->{0..1}
##分表策略
table-strategy:
standard:
sharding-column: id
sharding-algorithm-name: address-inline
key-generate-strategy:
column: id
key-generator-name: snowflake
sharding-algorithms:
id-inline:
type: INLINE
props:
algorithm-expression: db-$->{id % 2}
address-inline:
type: INLINE
props:
algorithm-expression: address_$->{id % 2}
key-generators:
snowflake:
type: SNOWFLAKE
mybatis-plus:
# ** 代表多级 *代表一级
mapper-locations: classpath*:com/cloud/**/xml/*Mapper.xml