一、批量删除
删除记录的时候,大多时候都是根据记录的 id 去删除,所以当实现批量删除的时候,只要我们想法传进去多个 id 值就可以了。
思路:
1 2 | 第一种方式:delete from tbl_employee where id in ( 1 , 2 , 3 ) 第二种方式:delete from tbl_employee where id = 1 or id = 2 or id = 3 |
二、方式一
可以将多个 id 值拼接成字符串,然后传递进去,通过 id in (ids) 的方式来删除。
1、在接口中声明方法
//通过id所组成的字符串实现批量删除
public void deleteMoreEmp(String eids);
2、在对应的 xml 文件中配置
<!--
public void deleteMoreEmp(String ids);
-->
<delete id="deleteMoreEmp">
delete from tbl_employee where id in (#{ids})
</delete>
3、测试
@Test
public void testDeleteMore() throws Exception {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession(true);
EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
String eids = "19,20,21";
mapper.deleteMoreEmp(eids);
}
可以发现,这时并没有全部删除,而只是删除了 19 这条记录,这是为什么呢?
执行的SQL语句为:
1 | delete from tbl_employee where id in (?) |
如果这时把 ids 传递过去,因为使用的是 #{} 获取值的方式,
对于字符串类型来说,默认会给字符串加上单引号,就成了下面这种形式:
1 | delete from tbl_employee where id in ( '19,20,21' ) |
对于这条SQL语句来说,只会匹配到第一个 id,即id=19的记录,后面的将不会再执行了,所以只删除了第一条记录。
如果把 #{} 取值改为 ${} 取值呢?
<!--
public void deleteMoreEmp(String ids);
-->
<delete id="deleteMoreEmp">
delete from tbl_employee where id in (${value})
</delete>
这时再进行测试,执行的 SQL 语句是:
1 | delete from tbl_employee where id in ( 19 , 20 , 21 ) |
可以发现,这才是我们想要的结果,并且也在数据库中成功的删除了。
注意:一定要注意 #{} 取值与 ${} 取值的区别和特点,#{}对字符串类型会加单引号,而 ${} 不会。
三、方式二
使用 foreach 标签来进行删除
1、在接口中声明方法
//通过list集合实现批量删除
public void deleteMoreByList(List<Integer> ids);
2、在 xml 中进行配置
<!-- public void deleteMoreByList(List<Integer> ids); -->
<delete id="deleteMoreByList">
delete from tbl_employee where id in
<foreach collection="list" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
3、测试
@Test
public void testBatchDelete() throws IOException {
//1、获取 sqlSessionFactory
SqlSessionFactory sqlSessionFactory = getsqlSessionFactory();
//2、获取 sqlSession 实例,能直接执行已经映射的 SQL 语句
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
EmployeeMapperBatch mapper = sqlSession.getMapper(EmployeeMapperBatch.class);
mapper.deleteMoreByList(Arrays.asList(1, 2, 3));
sqlSession.commit();
} finally {
sqlSession.close();
}
}
4、执行的SQL语句
1 2 | Preparing: delete from tbl_employee where id in ( ? , ? , ? ) Parameters: 19 (Integer), 20 (Integer), 23 (Integer) |
这时也可以执行成功。
四、方式三
使用 foreach 标签来批量删除(思路二,使用 or)
1、在接口中声明方法
//通过list集合实现批量删除
public void deleteMoreByList(List<Integer> ids);
2、在xml中进行配置
<!-- public void deleteMoreByList(List<Integer> ids); -->
<delete id="deleteMoreByList">
delete from tbl_employee where
<foreach collection="list" item="id" separator="or">
id = #{id}
</foreach>
</delete>
把多个 id=value 进行拼接,用 or 作为分隔符。
3、测试
@Test
public void testBatchDelete() throws IOException {
//1、获取 sqlSessionFactory
SqlSessionFactory sqlSessionFactory = getsqlSessionFactory();
//2、获取 sqlSession 实例,能直接执行已经映射的 SQL 语句
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
EmployeeMapperBatch mapper = sqlSession.getMapper(EmployeeMapperBatch.class);
mapper.deleteMoreByList(Arrays.asList(19, 20, 23));
sqlSession.commit();
} finally {
sqlSession.close();
}
}
执行的SQL语句:
1 2 | Preparing: delete from tbl_employee where id = ? or id = ? or id = ? Parameters: 19 (Integer), 20 (Integer), 23 (Integer) |
此时也可以运行成功。
注意:上面的 List 也可以更换成 Array 数组,但是取值的时候要注意,key 为 array。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战