对分库分表进行批量操作

对ShardingJDBC基础了解:https://blog.csdn.net/m0_63297646/article/details/131894472

对批量操作案例:https://blog.csdn.net/m0_63297646/article/details/131843517

分为db0和db1两个库,每个库都有三张订单表,分表键根据年份【year】,分库键根据店铺id【store_id】

在db0中存在两张学生信息表,分表键根据年份【year】

一、插入

插入时,对象要带有分库/分表键的值,shardingJdbc会进行改写。一次性插入。

1、分表未分库

 2、分库分表

二、更新

代码简单,但是会增加逻辑。 

db0和db1都会执行,且因为店铺id集合包含两个库的数据,所有的订单都会在两个库重复执行。

建议:在stream所有店铺id时,可以加一个distinct,避免大量重复数据。

@Test
void test15() {
    //分库分表键全作为参数,则全部都要执行一遍。
    List<OrderDO> orderDOList = orderMapper.selectList(null);
    System.out.println(Objects.isNull(orderDOList));
    List<Long> collect = orderDOList.stream().map(obj -> obj.getStoreId()).distinct().collect(Collectors.toList());
    List<Long> collect1 = orderDOList.stream().map(obj -> obj.getId()).distinct().collect(Collectors.toList());
    orderMapper.updateBatchOrderDoList(collect,
            collect1,
            CommonUtil.getYearList()
    );
}


void updateBatchOrderDoList(@Param("store_id_list")List<Long> storeIdList,
                            @Param("order_id_list")List<Long> orderIdList,
                            @Param("year_list")List<Integer> yearList);


<update id="updateBatchOrderDoList">
    UPDATE
    t_order bo
    SET
     no='2222222'
    <where>
        <foreach collection="year_list" separator="," item="item" open=" AND bo.year IN (" close=")">
            #{item}
        </foreach>
        <foreach collection="order_id_list" item="item" separator="," open=" AND bo.id IN (" close=")">
            #{item}
        </foreach>
        <foreach collection="store_id_list" item="item" separator="," open=" AND bo.store_id IN (" close=")">
            #{item}
        </foreach>
    </where>
</update>

1、分库分表

根据分库键【store_id】分为两个list,分别更新。

public static <T> void splitAndProcess(List<T> sourceList, Predicate<T> condition, Consumer<List<T>> action) {
    List<T> matchingItems = new ArrayList<>();
    List<T> nonMatchingItems = new ArrayList<>();

    for (T obj : sourceList) {
        if (condition.test(obj)) {
            matchingItems.add(obj);
        } else {
            nonMatchingItems.add(obj);
        }
    }

    action.accept(matchingItems);
    action.accept(nonMatchingItems);
}
splitAndProcess(orderDOList, obj -> obj.getStoreId() % 2 == 0, splitList -> {
    orderMapper.batchUpdate(splitList);
});

注意:如果使用下面这种sql方式,需要更新0库1库两个库的数据,路由只会进入一个更新,导致没有所有的数据更新。

<update id="batchUpdate">
    <foreach collection="list" item="vo" index="idx" separator=";">
        update
        <if test="idx > 0">
            t_order_${vo.year}
        </if>
        <if test="idx == 0">
            t_order
        </if>
        set no=3
        where id = #{vo.id} and year = #{vo.year} and store_id=#{vo.storeId}
    </foreach>
</update>

2、分表未分库

如果是统一的数据更新,比如student表,更新所有学生的name为‘张三’,可以直接用updateById。

使用sql语句话

<update id="updateStduent">
    update
        s_stduent
    set
       age = age + 1
    <where>
        id=#{id}
        <foreach collection="year_list" item="item" separator="," open=" AND year IN (" close=")">
            #{item}
        </foreach>
    </where>
</update>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值