Mysql Mybatis 批量修改数据
Mysql Mybatis 批量修改数据
Mapper
Integer updateListPO(List<ProjectQuotationItemPO> upateList);
方法一:
<update id="updateListPO">
<foreach collection="list" separator=";" item="item">
UPDATE project_quotation_item
SET product_num = #{item.productNum},
product_price_total = #{item.productPriceTotal},
product_price_wttax = #{item.productPriceWttax},
certificate = #{item.certificate},
deliver_date = #{item.deliverDate},
product_brand = #{item.productBrand},
producer = #{item.producer},
exp = #{item.exp},
is_deleted = #{item.isDeleted},
remark = #{item.remark},
substitute_type = #{item.substituteType},
quotation_status = #{item.quotationStatus}
WHERE
id = #{item.id}
</foreach>
</update>
需要在SQL连接的URL添加 allowMultiQueries=true
但是根据不同的Mysql版本或者是其他环境因素,可能还是会执行失败。
方法二:
<update id="updateListPO">
<!-- 优化后的逻辑 -->
update project_quotation_item
<set>
<trim prefix="product_num = case" suffix="end,">
<foreach collection="list" item="item">
when id=#{item.id} then #{item.productNum}
</foreach>
</trim>
<trim prefix="product_price_total = case" suffix="end,">
<foreach collection="list" item="item">
when id=#{item.id} then #{item.productPriceTotal}
</foreach>
</trim>
<trim prefix="product_price_wttax = case" suffix="end,">
<foreach collection="list" item="item">
when id=#{item.id} then #{item.productPriceWttax}
</foreach>
</trim>
<trim prefix="certificate = case" suffix="end,">
<foreach collection="list" item="item">
when id=#{item.id} then #{item.certificate}
</foreach>
</trim>
<trim prefix="deliver_date = case" suffix="end,">
<foreach collection="list" item="item">
when id=#{item.id} then #{item.deliverDate}
</foreach>
</trim>
<trim prefix="product_brand = case" suffix="end,">
<foreach collection="list" item="item">
when id=#{item.id} then #{item.productBrand}
</foreach>
</trim>
<trim prefix="producer = case" suffix="end,">
<foreach collection="list" item="item">
when id=#{item.id} then #{item.producer}
</foreach>
</trim>
<trim prefix="exp = case" suffix="end,">
<foreach collection="list" item="item">
when id=#{item.id} then #{item.exp}
</foreach>
</trim>
<trim prefix="is_deleted = case" suffix="end,">
<foreach collection="list" item="item">
when id=#{item.id} then #{item.isDeleted}
</foreach>
</trim>
<trim prefix="remark = case" suffix="end,">
<foreach collection="list" item="item">
when id=#{item.id} then #{item.remark}
</foreach>
</trim>
<trim prefix="substitute_type = case" suffix="end,">
<foreach collection="list" item="item">
when id=#{item.id} then #{item.substituteType}
</foreach>
</trim>
<trim prefix="quotation_status = case" suffix="end,">
<foreach collection="list" item="item">
when id=#{item.id} then #{item.quotationStatus}
</foreach>
</trim>
</set>
<where>
id in
<foreach collection="list" separator="," item="item" open="(" close=")">
#{item.id}
</foreach>
</where>
</update>
拼接处的SQL语句
update project_quotation_item
SET product_num = case when id=2203220000003867 then 101
when id=2203220000003868 then 10
when id=2203220000003869 then 20 end,
product_price_total = case when id=2203220000003867 then 1111
when id=2203220000003868 then 220
when id=2203220000003869 then 660 end,
product_price_wttax = case when id=2203220000003867 then 11
when id=2203220000003868 then 22
when id=2203220000003869 then 33 end,
certificate = case when id=2203220000003867 then "11"
when id=2203220000003868 then "22"
when id=2203220000003869 then "33" end,
deliver_date = case when id=2203220000003867 then 11
when id=2203220000003868 then 22
when id=2203220000003869 then 33 end,
product_brand = case when id=2203220000003867 then "11"
when id=2203220000003868 then "22"
when id=2203220000003869 then "33" end,
producer = case when id=2203220000003867 then "11"
when id=2203220000003868 then "22"
when id=2203220000003869 then "33" end,
exp = case when id=2203220000003867 then 11
when id=2203220000003868 then 22
when id=2203220000003869 then 33 end,
is_deleted = case when id=2203220000003867 then 0
when id=2203220000003868 then 0
when id=2203220000003869 then 0 end,
remark = case when id=2203220000003867 then null
when id=2203220000003868 then null
when id=2203220000003869 then null end,
substitute_type = case when id=2203220000003867 then "11"
when id=2203220000003868 then "22"
when id=2203220000003869 then "33" end,
quotation_status = case when id=2203220000003867 then 2
when id=2203220000003868 then 2
when id=2203220000003869 then 2 end
WHERE id in
(
2203220000003867
,
2203220000003868
,
2203220000003869
)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)