MySQL批量update
參數(shù)為List<T>
<update id="batchUpdateEmployerMissionTypeInfoByOrder" parameterType="java.util.List">
update employer_mission_type_manage
<trim prefix="set" suffixOverrides=",">
<trim prefix="type_order=case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.typeId!=null">
when type_id=#{item.typeId} then #{item.typeOrder}
</if>
</foreach>
</trim>
</trim>
where type_id in
<foreach collection="list" item="item" separator="," open="(" close=")">
#{item.typeId,jdbcType=INTEGER}
</foreach>
</update>
這只是mysql批量更新的一種寫法,上述sql也可以寫成直接將 update 語句放到<foreach>塊中去執(zhí)行的方式,這種方式會產(chǎn)生多條 update 語句。有的文章說多條update語句會比本文開始的那種方式快,當然也有人說本文一開始的寫法好,我也不知道哪個好,只是因為這個方法寫著麻煩焚刺,所以我記錄一下。
在本文中的update語句的where條件里门烂,也可以寫成如下形式:
where
<foreach collection="list" item="item" separator="or">
type_id = #{item.typeId,jdbcType=INTEGER}
</foreach>
這種用 or 的寫法據(jù)說會影響性能乳愉,所以本文采用的是 in 的寫法,感興趣的話可以自行百度诅福,有很多重復(fù)的文章匾委。
不扯了,就這樣氓润。