一、首先要有mapper接口
/**
*
* 方法描述:根据ids集合进行删除
* @return
*/
int deleteByIds(List<Integer> ids);
二、Mapper.xml动态sql
collection:传递来的参数,可以是list,array(数组),还可以是map的key,可以是pojo中的属性
item:循环中的当前元素
index:当前元素的下标
open:循环的开始
close:循环的结束
separator:分隔符
<delete id="deleteByIds" parameterType="list">
delete from user where id in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
这段SQL最终会被自动组合成:delete from user where id in ( ? , ? )
三、测试
public class TestMapper {
public static void main(String[] args) {
//1.获取会话
SqlSession sqlSession = SqlSessionFactoryUtil.getFactory().openSession();
//2.获取mapper接口的代理对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<Integer> ids = new ArrayList<>();
//添加要删除的id
ids.add(22);
ids.add(23);
//调用mapper动态SQL的批量删除
userMapper.deleteByIds(ids);
sqlSession.commit();
}
}