shardingsphere

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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值