批量增加
<insert id="batchSaveUser">
insert into t_user (user_name,sex) values
<!-- 动态SQL之foreach的用法 -->
<!-- collection="users" 用于指定循环集合的名称,如果接口中并未指定参数别名,那么默认就是list
item="u" 用于指定每次循环后的对象的别名
separator="," 用于指定每次循环后之间的分割符-->
<foreach collection="users" item="u" separator=",">
(#{u.userName},#{u.sex})
</foreach>
</insert>
批量删除
<delete id="batchDeleteUser">
delete from t_user where id in (
<foreach collection="ids" item="id" separator=",">
#{id}
</foreach>
)
</delete>
<!-- 第二种批量删除的写法 -->
<!-- open表示该语句以什么开始,close表示以什么结束 -->
<delete id="batchDeleteUser">
delete from t_user where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
复杂条件批量删除
<delete id="deleteByLogic" parameterType = "java.util.List">
delete from user where id > 2000
or id in
<foreach collection="list" item="item" open="(" separator="," close=")" >
#{item}
</foreach>
</delete>
复杂条件批量删除 建议增加联合索引
<!--通过条件删除数据-->
<delete id="batchDeleteByCondition">
delete from test where
<foreach collection="list" item="item" separator=" or " index="index">
(name = #{item.name} and area_code = #{item.areaCode} and type = #{item.type})
</foreach>
</delete>
复杂条件批量删除 IN操作
<delete id="deleteBatch" parameterType="java.util.List">
delete from test
where (PRSNLID, ENTID) in
<foreach item="item" index="index" collection="list" separator="," open="(" close=")">
(#{item.prsnlid},#{item.entid})
</foreach>
</delete>