当前位置: 首页 > news >正文

对分库分表进行批量操作

对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">UPDATEt_order boSETno='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=3where id = #{vo.id} and year = #{vo.year} and store_id=#{vo.storeId}</foreach>
</update>

2、分表未分库

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

使用sql语句话

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

http://www.lryc.cn/news/151782.html

相关文章:

  • 大数据组件-Flume集群环境的启动与验证
  • 【包过滤防火墙——iptables静态防火墙】的简单使用
  • 关于MySQL数据库版本不同导致表进行比较的时候报错illegal mix of collations...的问题
  • 进程、操作系统
  • hadoop学习:mapreduce入门案例四:partitioner 和 combiner
  • HTTP与SOCKS5的区别对比
  • 在阿里云请求发短信接口去掉证书验证
  • k8s里pv pvc configmap
  • 【Atcoder】 [ARC144D] AND OR Equation
  • python使用字典暴力解析wifi密码
  • java八股文面试[多线程]——synchronized锁升级详细流程
  • ui网页设计实训心得
  • 论文阅读_扩散模型_DDPM
  • 菜鸟教程《Python 3 教程》笔记(15):数据结构
  • CH05_介绍重构名录
  • 1、Nginx 简介
  • C++之——宏
  • 代码随想录打卡—day56—【编辑距离】— 9.2 编辑距离系列
  • uni-app app端.m3u8类型流的播放
  • 使用proxy_pool来为爬虫程序自动更换代理IP | 开源IP代理
  • 【易售小程序项目】修改“我的”界面前端实现;查看、重新编辑、下架自己发布的商品【后端基于若依管理系统开发】
  • Centos7 + Apache Ranger 2.4.0 部署
  • 硬件SPI口扩展
  • 【jsthree.js】全景vr看房进阶版
  • 实战:基于卷积的MNIST手写体分类
  • Ubuntu开启生成Core Dump的方法
  • git视频教程Jenkins持续集成视频教程Git Gitlab Sonar教程
  • 机器学习:Xgboost
  • 《Kubernetes部署篇:Ubuntu20.04基于二进制安装安装cri-containerd-cni》
  • [CISCN 2019初赛]Love Math