参考博文:
MySQL没有提供直接的方法来实现批量更新,但可以使用case when语法来实现这个功能。
语法:
UPDATE course
SET name = CASE id
WHEN 1 THEN 'name1'
WHEN 2 THEN 'name2'
WHEN 3 THEN 'name3'
END,
title = CASE id
WHEN 1 THEN 'New Title 1'
WHEN 2 THEN 'New Title 2'
WHEN 3 THEN 'New Title 3'
END
WHERE id IN (1,2,3)
这条sql的意思是,如果id为1,则name的值为name1,title的值为New Title1;依此类推。
Mybatis样例:
<update id="updateBylists" parameterType="list">
update cm_track_replay
set province =
<foreach collection="list" item="item" index="index"
separator=" " open="case id" close="end,">
when #{item.id} then #{item.province}
</foreach>
city =
<foreach collection="list" item="item" index="index"
separator=" " open="case id" close="end,">
when #{item.id} then #{item.city }
</foreach>
district =
<foreach collection="list" item="item" index="index"
separator=" " open="case id" close="end,">
when #{item.id} then #{item.district}
</foreach>
street =
<foreach collection="list" item="item" index="index"
separator=" " open="case id" close="end">
when #{item.id} then #{item.street}
</foreach>
where id in
<foreach collection="list" index="index" item="item"
separator="," open="(" close=")">
#{item.id,jdbcType=BIGINT}
</foreach>
</update>