郑重声明:此SQL语句基于 主键 而行,否则不起作用
mapper:
int saveOrUpdateTStaff(List<TStaff> staff);
xml:
<insert id="saveOrUpdateTStaff" parameterType="java.util.List">
insert into t_staff (
id,
staff_name,
user_id,
user_name
)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.id},
#{item.staffName},
#{item.userId},
#{item.userName}
)
</foreach>
on duplicate key update
staff_name = VALUES(staff_name),
user_id = VALUES(user_id),
user_name = VALUES(user_name)
</insert>
上述SQL update 相当于:
update t_staff
set
staff_name = ?,
user_id = ?,
user_name = ?
WHERE id= ?
所以例如 id 是主键 ,sql表就别自增了,自己随机整吧....