Mybatis实现批量删除数据


学习内容:

1. 使用

这里通过动态 SQL 语句来实现批量删除操作,通过标签foreach collection="" 来实现。

2. 代码实现

2.1 UserMapper.java 接口

// 使用注解的方式传递 Long 数组到 UserMapper.xml 文件
public interface UserMapper {
    void delete(@Param("ids") Long[] id);
}

2.2 UserMapper.xml

 <!-- collection 表示遍历的数组或者集合,填写对应 key 的值 item 遍历元素 separator 每遍历元素拼接字符串 open 遍历开始拼接的字符串 close 遍历结束拼接字符串 index 遍历索引 例如本条 SQL 语句,假设传入的集合ids为 {1,2,3} ,那么拼接后的结果为: delete from user where id in(1, 2, 3) -->
<delete id="delete">
     delete from user
     where id in
     <foreach collection="ids" item="id" open="(" close=")" separator=",">
         #{id}
     </foreach>
</delete>

总结:

以上就是Mybatis实现批量删除操作了,代码仅供参考,欢迎讨论交流。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM