SQL模板:
-- 模板
UPDATE `table1`
SET
`column1` = CASE
WHEN condition = ? THEN ?
WHEN condition = ? THEN ?
END,
`column2` = CASE
WHEN id = ? THEN ?
WHEN id = ? THEN ?
END
WHERE
`condition ` IN (?, ?)
SQL模板例子:
-- 例子
UPDATE `t_student`
SET
`name` = CASE
WHEN id= 1 THEN '张三'
WHEN id = 2 THEN '李四'
END,
`sex` = CASE
WHEN id = 1 THEN 1
WHEN id = 2 THEN 1
END
WHERE
`id` IN (1, 2)
MyBatis批量更新写法
<update id="updateList" parameterType="com.yj.test.student.po.Student">
update `t_student`
<trim prefix="set" suffixOverrides=",">
<trim prefix="name = case" suffix="end,">
<foreach collection="parameterList" item="param">
when id = #{param.id} then #{param.name}
</foreach>
</trim>
<trim prefix="sex = case" suffix="end,">
<foreach collection="parameterList" item="param">
when id = #{param.id} then #{param.sex}
</foreach>
</trim>
</trim>
<where>
id in
<foreach collection="parameterList" item="param" open="(" separator="," close=")">
#{param.id}
</foreach>
</where>
</update>