Java MyBatis XXXMapper.xml中的批量删除

作者: java 发布时间: 2023-01-25 浏览: 535 次 编辑

1、准备环境

这篇文章的批量操作都将是针对User用户表,下面给出相关的代码。

SQL建表语句

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `username` varchar(50) DEFAULT NULL COMMENT '用户名称',
  `password` varchar(50) DEFAULT NULL COMMENT '密码',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3010 DEFAULT CHARSET=utf8;

2、批量删除

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.demo.mapper.UserMapper">
    <!-- 批量删除 -->
    <delete id="batchDelete" parameterType="java.lang.Integer">
        delete from user
        where id in
        <foreach collection="ids" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </delete>
</mapper>