pom.xml
<!-- for spring boot -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
application.yml
spring:
shardingsphere:
datasource:
#数据源
names: ds0,ds1
#数据源属性
ds0:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/ds0?serverTimezone=UTC&characterEncoding=utf-8
username: root
password: root
ds1:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/ds1?serverTimezone=UTC&characterEncoding=utf-8
username: root
password: root
sharding:
#分库
default-database-strategy:
inline:
#分片字段
sharding-column: id
#分片表达式
algorithm-expression: ds$->{id % 2}
#分表
tables:
#分表的表名
tb:
#数据节点,
actual-data-nodes: ds$->{0..1}.tb$->{0..2}
table-strategy:
inline:
#分表字段
sharding-column: id
#分表表达式
algorithm-expression: tb$->{id % 3}
pojo
实体类要实现Comparable接口
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "tb")
public class Tb implements Comparable{
@Id
private Long id;
@Override
public int compareTo(Object o) {
return id.intValue();
}
}