MYBATIS

【已解决】mybatis - 通过3种方式 批量更新

本文凯哥(公众号:凯哥Java)来讲讲mybatis的三者中批量更新方式通过foreach批量更新通过casewhen批量更新(100条级别使用,批量更新数据太多会适得其反)通过ExecutorType.Batch批量更新一、通过foreach批量更新原始mysql的语句:update table set id=1,name="泽虞生" 

本文凯哥(公众号:凯哥Java)来讲讲mybatis的三者中批量更新方式

  1. 通过foreach批量更新

  2. 通过case when批量更新(100条级别使用,批量更新数据太多会适得其反)

  3. 通过ExecutorType.Batch批量更新

一、通过foreach批量更新

原始mysql的语句:

update table set id=1,name="泽虞生" where id = 1;
update table set id=2,name="泽虞生" where id = 2;
update table set id=3,name="泽虞生" where id = 3;
...

对应的mybatis的写法:

<foreach collection="list" item="i" index="index" open="" close="" separator=";">
     UPDATE table
     <trim prefix="set" suffixOverrides=",">
         <if test="i.id!=null">id= #{i.id}</if>
         <if test="i.name!=null">name= #{i.name}</if>
      </trim>
      WHERE id= #{i.id} and is_deleted = 0
</foreach>

需要注意:

  1. 分隔符是分号。open和close都是空的

  2. 需要有主键或者是能够唯一区分的字段


二、通过case when批量更新

原始mysql语句写法:

update table set
id

原创不易,完成人机校验,阅读全文

相关推荐