shardingsphere 集成 SpringBoot 批量更新失败

前言

在近期的项目中引入了 shardingSphere 分库分表,排查过后发现失败的原因是分库分表字段未匹配。

我们举个栗子,我们这边分库分表的字段为 projectId。正常来说只要有传入 projectId 作为查询条件shardingsphere 中的拦截器会自动去根据我们的分库分表规则进行拼接新的sql语句。但实际上根据测试结果所得发现只有在第一次foreach时才会根据分库分表规则进行对sql进行自动拼接。


错误如下图代码所示:

<update id="updateBatch">
        <foreach collection="updateList" item="item" index="index" separator=";">
            update
            b_bank_book_account
            set
            bank_abbreviation = #{item.bankAbbreviation},
            bank_full_name = #{item.bankFullName},
            bank_account = #{item.bankAccount},
            account_nature = #{item.accountNature},
            interest_rate = #{item.interestRate},
            <if test="updater != null">
                update_user_name = #{updater.userName},
                update_full_name = #{updater.fullName},
            </if>
            update_time = unix_timestamp(now())*1000
            where id = #{item.id} and project_id = #{item.projectId}
        </foreach>
 </update>

我们希望得到的每一次的表名都为 update b_bank_book_account_0 但实际上得到的结果只有第一次是 update b_bank_book_account_0 从第二次开始还是 update b_bank_book_account 并没有进行转换。


该如何解决这个问题呢?那就只能曲线救国了。

在接口外通过分库分表键于规则计算好之后在第二次循环时手动拼接了。下附解决代码。
${strategy} 为计算后分表规则

<update id="updateBatch">
        <foreach collection="updateList" item="item" index="index" separator=";">
            update
            <if test="index == 0">
                b_bank_book_account
            </if>
            <if test="index != 0">
                b_bank_book_account_${strategy}
            </if>
            set
            bank_abbreviation = #{item.bankAbbreviation},
            bank_full_name = #{item.bankFullName},
            bank_account = #{item.bankAccount},
            account_nature = #{item.accountNature},
            interest_rate = #{item.interestRate},
            <if test="updater != null">
                update_user_name = #{updater.userName},
                update_full_name = #{updater.fullName},
            </if>
            update_time = unix_timestamp(now())*1000
            where id = #{item.id} and project_id = #{item.projectId}
        </foreach>
</update>

本文由 SoGeek_Studio 发布,有任何问题请留言评论,欢迎指正。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值