批量操作:
批量 insert:
1 | <insert id="batchSaveOrUpdate" useGeneratedKeys="true" keyProperty="id"> |
批量 insert 或 update:
<insert id="batchSaveOrUpdate" useGeneratedKeys="true" keyProperty="id">
INSERT INTO favorite_group(id, name, user_id) VALUES
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id}, #{item.name}, #{item.userId})
</foreach>
ON DUPLICATE KEY UPDATE
name = CASE id
<foreach collection="list" item="item" index="index">
WHEN #{item.id} THEN #{item.name}
</foreach>
END
</insert>
如果有多个字段要改动,
END
后面加逗号分隔。
批量 delete:
<delete id="batchDelete" parameterType="long">
DELETE FROM favorite_group where id IN
<foreach collection="array" item="id" open="(" separator="," close=")">
#{groupIds}
</foreach>
</delete>
最终的语句类似于:
delete from favorite_group where id in (1, 2, 3)
如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理