shardingsphere 集成springboot【水平分表】

创建sharding_sphere数据库

在数据库中创建两张表,t_order_1和t_order_2

分片规则:如果订单编号是偶数添加到t_order_1,如果是奇数添加到t_order_2

创建实体类

public class Order {  
  
    private Integer id;  
    private Integer orderType;  
    private Integer customerId;  
    private Double amount;  
  
    public Integer getId() {  
        return id;  
    }  
  
    public void setId(Integer id) {  
        this.id = id;  
    }  
  
    public Integer getOrderType() {  
        return orderType;  
    }  
  
    public void setOrderType(Integer orderType) {  
        this.orderType = orderType;  
    }  
  
    public Integer getCustomerId() {  
        return customerId;  
    }  
  
    public void setCustomerId(Integer customerId) {  
        this.customerId = customerId;  
    }  
  
    public Double getAmount() {  
        return amount;  
    }  
  
    public void setAmount(Double amount) {  
        this.amount = amount;  
    }  
  
    @Override  
    public String toString() {  
        return "Order{" +  
                "id=" + id +  
                ", orderType='" + orderType + '\'' +  
                ", customerId=" + customerId +  
                ", amount=" + amount +  
                '}';  
    }  
}

创建Mapper

@Repository  
@Mapper  
public interface OrderMapper {  
  
    @Insert("insert into t_order(order_type,customer_id,amount) values(#{orderType},#{customerId},#{amount})")  
    public void insert(Order orders);  
  
    @Select("select * from t_order where id = #{id}")  
    @Results({  
            @Result(property = "id",column = "id"),  
            @Result(property = "orderType",column = "order_type"),  
            @Result(property = "customerId",column = "customer_id"),  
            @Result(property = "amount",column = "amount")  
    })  
    public Order selectOne(Integer id);
}    

创建配置文件

spring:  
  shardingsphere:  
    datasource: #数据源配置  
      names: ds1  
      ds1: #数据源0  
        type: com.alibaba.druid.pool.DruidDataSource  
        driver-class-name: com.mysql.cj.jdbc.Driver  
        url: jdbc:mysql://localhost:3306/ds1?serverTimezone=UTC&useSSL=false  
        username: root  
        password: wsrbb  
    sharding:  
      tables:  
        t_order:  
          actual-data-nodes: ds1.t_order_${1..2}   #指定orders表的分布情况,配置表在哪个数据库中,表名称是什么  
          # 雪花算法  
          key-generator:  
            column: id  
            type: SNOWFLAKE  
            props:  
              worker-id: 123  
              max-vibration-offset: 3  
          ##指定分片策略。根据id的奇偶性来判断插入到哪个表  
          table-strategy:  
            inline:  
              sharding-column: id  
              algorithm-expression: t_order_${id % 2 +1}  
      #打开sql输出日志  
      props:  
        sql:  
          show: true

运行测试类即可

@Test  
public void addOrders(){  
    for (int i = 1; i <=10 ; i++) {  
        Order orders = new Order();  
        //orders.setId(i);  
        orders.setCustomerId(new Random().nextInt(10));  
        orders.setOrderType(i);  
        orders.setAmount(1000.0*i);  
        orderMapper.insert(orders);  
    }  
}

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您好!要将ShardingSphere集成Spring Boot项目中,您可以按照以下步骤进行操作: 1. 添加依赖:在您的项目的pom.xml文件中添加如下依赖: ```xml <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>5.0.0</version> </dependency> ``` 2. 配置数据源:在application.properties或application.yml文件中配置您的数据源信息,例如: ```properties spring.shardingsphere.datasource.names=dataSource0,dataSource1 spring.shardingsphere.datasource.dataSource0.jdbc-url=jdbc:mysql://localhost:3306/db0 spring.shardingsphere.datasource.dataSource0.username=root spring.shardingsphere.datasource.dataSource0.password=root spring.shardingsphere.datasource.dataSource0.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.dataSource1.jdbc-url=jdbc:mysql://localhost:3306/db1 spring.shardingsphere.datasource.dataSource1.username=root spring.shardingsphere.datasource.dataSource1.password=root spring.shardingsphere.datasource.dataSource1.driver-class-name=com.mysql.cj.jdbc.Driver ``` 3. 配置分片规则:在application.properties或application.yml文件中配置您的分片规则,例如: ```properties spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{user_id % 2} spring.shardingsphere.sharding.tables.order.actual-data-nodes=dataSource$->{0..1}.order_$->{0..1} spring.shardingsphere.sharding.tables.order.table-strategy.inline.sharding-column=order_id spring.shardingsphere.sharding.tables.order.table-strategy.inline.algorithm-expression=order_$->{order_id % 2} ``` 4. 编写业务代码:在您的Spring Boot项目中,可以直接使用`DataSource`进行数据访问,ShardingSphere会自动根据配置的分片规则进行数据路由。 这样,您就成功将ShardingSphere集成Spring Boot项目中了。希望能对您有所帮助!如果您还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王小睿丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值