人员批量删除功能

人员批量删除功能

  • controller
   //批量删除
    protected void batch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.收集参数
        String[] ids = request.getParameterValues("ids");
        //2.调用逻辑层批量删除方法,
        if (Validator.isNotEmpty(ids)) {
            int[] array = new int[ids.length];
            for (int i = 0; i < ids.length; i++) {
                array[i] = Integer.parseInt(ids[i]);
            }
            personnelService.batch(array);
        }
        //3.返回响应(删除完后查询语句,所以返回此页面),重定向跳转页面,跳转页面不带参数,所以批删之后size=10
        response.sendRedirect(request.getContextPath() + "/personnel.do");
    }
  • service
//人员模块逻辑层接口
public interface PersonnelService {
    //批量删除
    int batch(int... ids);
}
//人员模块逻辑层实现类
public class PersonnelServiceImpl implements PersonnelService {
    //逻辑层中获取数据层实现类
    private PersonnelDao personnelDao = PersonnelFactory.getPersonnelDao();
    
    @Override
    //批量删除
    public int batch(int... ids) {
        return personnelDao.batch(ids);
    }
}
  • dao
//人员数据层接口
public interface PersonnelDao {
    //批量删除
    int batch(int... ids);
}
//人员模块数据层实现类
public class PersonnelDaoImpl implements PersonnelDao {
    //构造方法
    public PersonnelDaoImpl() {
    }

    //懒汉模式
    private static PersonnelDaoImpl instance = null;

    public static PersonnelDaoImpl getInstance() {
        //双重校验锁:保证不会出现并发
        if (instance == null) {
            synchronized (PersonnelDaoImpl.class) {
                if (instance == null) {
                    instance = new PersonnelDaoImpl();
                }
            }
        }
        return instance;
    }
    
    @Override
    //批量删除
    public int batch(int... ids) {
        //查询数据库,true:自动提交,如果没有则删除失败
        SqlSession session = MybatisUtils.openSession(true);
        int count = session.delete("personnel.batch", ids);
        session.close();
        return count;
    }
}
  • xml
<?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="personnel">
    <!--批量删除-->
    <delete id="batch" parameterType="int">
        delete from rms_personnel where id in
        <foreach collection="array" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>
</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值