方式一:case when then....
<!-- 批量更新拼音字段 -->
<update id="updatePinyin">
UPDATE sys_region_continents
SET C_NAME_PINYIN =
<foreach collection="idNames" index="index" item="item"
open="CASE ID" separator=" " close="END">
WHEN #{item.id} THEN #{item.pinyin}
</foreach>
WHERE ID IN
<foreach collection="idNames" index="index" item="item"
open="(" separator="," close=")">
#{item.id,jdbcType=BIGINT}
</foreach>
</update>
方式二:insert into ... on duplicate key update
如果列不能为空或者没有默认值也许会报错:Field XXX doesn't have a default value,
<!-- 批量更新拼音字段 -->
<update id="updatePinyin">
INSERT INTO sys_region(ID,C_NAME_PINYIN) VALUES
<foreach collection="idNames" index="index" item="item" open="" separator="," close="">
(#{item.id},#{item.pinyin})
</foreach>
ON DUPLICATE KEY UPDATE
ID = VALUES(ID),
C_NAME_PINYIN = VALUES(C_NAME_PINYIN)
</update>
方式三:replace into
这种方式只有替换的字段(示例中ID,C_NAME_PINYIN)有值,其余都会被清空掉
<!-- 批量更新拼音字段 -->
<update id="updatePinyin">
REPLACE INTO sys_region_continents(ID,C_NAME_PINYIN) VALUES
<foreach collection="idNames" index="index" item="item" open="" separator="," close="">
(#{item.id},#{item.pinyin})
</foreach>
</update>