(一)纯mysql:
UPDATE my_table SETstatus = CASE idWHEN 1 THEN 3WHEN 2 THEN 4WHEN 3 THEN 5END,title = CASE idWHEN 1 THEN 'New Title 1'WHEN 2 THEN 'New Title 2'WHEN 3 THEN 'New Title 3'END
WHERE id IN (1,2,3)
(二)mybatis写法
1:foreach
<update id="batchUpdate" parameterType="java.util.List">update my_table set status=<foreach collection="listName" item="item" index="index" separator=" " open="case id" close="end">when #{item.id} then #{item.status}</foreach>where id in<foreach collection="listName" index="index" item="item" separator="," open="(" close=")">#{item.id,jdbcType=BIGINT}</foreach></update>
2:trim
<update id="batchUpdate" parameterType="java.util.List">update my_table<trim prefix="set" suffixOverrides=","><trim prefix="status =case" suffix="end,"><foreach collection="listName" item="item" index="index">when id=#{item.id} then #{item.status}</foreach></trim></trim>where id in<foreach collection="listName" index="index" item="item" separator="," open="(" close=")">#{item.id,jdbcType=BIGINT}</foreach>
</update>