网站首页 > 精选文章 / 正文
1. 使用foreach标签绑定集合参数:
<delete id="deleteByIds" parameterType="List">
delete from table_name
where id in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</delete>
调用方式:
List<Integer> ids = Arrays.asList(1, 2, 3);
int result = mapper.deleteByIds(ids);
2. 使用where标签绑定条件参数:
<delete id="deleteByCondition" parameterType="map">
delete from table_name
<where>
<if test="ids != null">
id in
<foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</where>
</delete>
调用方式:
Map<String, Object> map = new HashMap<>();
map.put("ids", Arrays.asList(1, 2, 3));
int result = mapper.deleteByCondition(map);
这两种方式的原理是使用MyBatis的动态SQL,通过foreach标签把List集合或数组中的每个元素绑定到SQL语句的in条件中,从而实现批量删除。使用这两种方式实现批量删除数据时, MyBatis会将底层的JDBC批量操作优化为一个SQL语句,以提高执行效率。所以,在实际使用中,若是对一张表需要批量删除上千条数据,使用这两种MyBatis的批量删除方式,效率会大大高于调用多次单条删除语句。
Tags:mybatis批量删除
猜你喜欢
- 2025-06-13 MyBatis与Hibernate的对比分析:探索持久化框架的最佳选择
- 2025-06-13 【Mybatis实战第9天】Mybatis批量插入 batchInsert
- 2025-06-13 别再用 Mybatis Plus 的伪批量新增了,这才是真正的批量新增方式!
- 2025-06-13 真香警告!Alibaba珍藏版mybatis手写文档,刷起来
- 2025-06-13 mybatis批量插入自动生成主键跟日期
- 2025-06-13 SpringBoot整合MyBatis-Plus:从入门到精通
- 2025-06-13 Mybatis Plus批量插入数据到MySQL中
- 2025-06-13 SpringBoot系列Mybatis之批量插入的几种姿势
- 2025-06-13 项目案例:Java多线程批量拆分List导入数据库
- 2025-06-13 Mybatis 批量更新数据 Mysql批量更新数据