1.可以直接使用foreach标签,来实现批量更新,但是这种效率太低.就不写了
2.还可以通过case when 语句来实现批量更新,这种写法效率更高,实际应用中一般使用此方法
例如:
int updateBatch(List<MeetingSign> meetingSignList);
<update id="updateBatch" parameterType="java.util.List">
update meetingSign
<trim prefix="set" suffixOverrides=",">
<trim prefix="accommodation =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.accommodation!=null">
when id=#{i.id} then #{i.accommodation}
</if>
</foreach>
</trim>
<trim prefix=" teamGrouping =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.teamGrouping!=null">
when id=#{i.id} then #{i.teamGrouping}
</if>
</foreach>
</trim>
<trim prefix="remark =case" suffix="end," >
<foreach collection="list" item="i" index="index">
<if test="i.remark!=null">
when id=#{i.id} then #{i.remark}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index" >
id=#{i.id}
</foreach>
</update>
这是实际上执行的sql语句
UPDATE meetingSign
SET accommodation = CASE
WHEN id =? THEN
?
WHEN id =? THEN
?
WHEN id =? THEN
?
WHEN id =? THEN
?
WHEN id =? THEN
?
END,
teamGrouping = CASE
WHEN id =? THEN
?
WHEN id =? THEN
?
WHEN id =? THEN
?
WHEN id =? THEN
?
WHEN id =? THEN
?
END,
remark = CASE
WHEN id =? THEN
?
WHEN id =? THEN
?
WHEN id =? THEN
?
WHEN id =? THEN
?
WHEN id =? THEN
?
END
WHERE
id =?
OR id =?
OR id =?
OR id =?
OR id =?