mybatis批量更新的两种实现方式
mapper.xml文件,后台传入一个对象集合,另外如果是mysql数据库,一点在配置文件上加上&allowMultiQueries=true,这样才可以执行多条sql,不过MySQL有缓存最大限制是1000条,在Java中要控制循环次数,以下为mysql:
<update id="batchUpdate" parameterType="java.util.List"> <foreach separator=";" index="index" item="item" collection="list" close="" open=""> update sys_group set level = #{item.level,jdbcType=INTEGER} where group_id = #{item.groupId,jdbcType=INTEGER} </foreach> </update>
带if判断的写法:
<update id="updateBatch" parameterType="java.util.List" > <foreach collection="list" item="item" index="index" open="" close="" separator=";"> update standard_relation <set > <if test="item.standardFromUuid != null" > standard_from_uuid = #{item.standardFromUuid,jdbcType=VARCHAR}, </if> <if test="item.standardToUuid != null" > standard_to_uuid = #{item.standardToUuid,jdbcType=VARCHAR}, </if> <if test="item.gmtModified != null" > gmt_modified = #{item.gmtModified,jdbcType=TIMESTAMP}, </if> </set> where id = #{item.id,jdbcType=BIGINT} </foreach> </update>
如果是oracle数据库则写法有不同:
<update id="batchUpdateRepayPlan" parameterType="java.util.List"> begin <foreach separator=";" index="index" item="item" collection="list" close="" open=""> update t_ba_repay_plan <set> <if test="item.interest !=null"> REPAY_INTEREST = #{item.interest,jdbcType=VARCHAR}, </if> <if test="item.nimm !=null"> REPAY_NIMM = #{item.nimm,jdbcType=CHAR} </if> </set> where IOU_CODE = #{item.iouCode} </foreach> ;end; </update>
本文转自:https://blog.csdn.net/pangliang_csdn/article/details/68945750
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?