批量删除(1课时讲解+1课时练习)

批量删除

相关测试用例:

public class DelTest {
   @Test
   public void test1(){
       String[] ids={"1","2","4"};
       //(1,2,3)
       //第一步把ids转成字符串
       System.out.println(Arrays.toString(ids).replace("[", "(").replace("]", ")"));
   }
}

jsp页form

 <form action="${pageContext.request.contextPath }/delServlet">
    <input type="checkbox" name="chkall" id="chkall"
        onclick="selectAll(this)" />全选
    <table width="100%" border="1" align="center" bordercolor="#dadada">
        <tr align="center">
            <td width="44" height="25" valign="middle">选择</td>
            <td width="98" height="25" valign="middle">账号</td>
            <td width="132" height="25" valign="middle">email</td>
            <td width="132" height="25" valign="middle">身份证号</td>
            <td width="132" height="25" valign="middle">权限</td>
            <td width="132" height="25" valign="middle">操作</td>
        </tr>
        <!-- 循环显示每个用户的信息 用jstl-->
        <c:forEach items="${userList}" var="user" varStatus="status">
            <tr align="center">
                <td width="44" height="25" valign="middle"><input
                    type="checkbox" name="chkone" value="${user.id }"
                    onclick="chkOne(this)" /></td>
                <td width="98" height="25" valign="middle">${user.userName }</td>
                <td width="132" height="25" valign="middle">${user.email }</td>
                <td width="132" height="25" valign="middle">${user.idCard }</td>
                <td width="132" height="25" valign="middle">${user.power }</td>
                <td width="132" height="25" valign="middle"><a href="#">修改</a></td>
            </tr>
        </c:forEach>
        <input type="button" value="删除" onclick="del();">
        </form>

js

function del() {
    var result = confirm("您确定要删除吗?");
    if (result == true) {
        document.forms[0].submit();
    }

}

servlet

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //第一步 获取页面选中的ID
    String[] ids=request.getParameterValues("chkone");
    //至少选中一个
    if(ids!=null&&ids.length>0){
        //调用删除方法执行删除
        IUserService service=new UserServiceImpl();
        int count=service.delete(ids);
        
        if(count>0) {
            //删除成功
            request.setAttribute("resultMsg", "操作成功");
        }
    }else{
        //给客户端一个响应
        request.setAttribute("resultMsg", "操作失败");
    }
    request.getRequestDispatcher("/findAllUserServlet").forward(request, response);
}

service略

dao

@Override
public int delete(String[] ids) {
    // delete from t where id in(1,2,3);
    int count=0;
    conn=DBUtil.getConnection();
    try {
        StringBuffer sql=new StringBuffer("delete from t_user where user_id in ");
        sql.append(Arrays.toString(ids).replace("[", "(").replace("]", ")"));
        ps=conn.prepareStatement(sql.toString());
        count=ps.executeUpdate();
    } catch (Exception e) {
        e.printStackTrace();
    }
    DBUtil.closeConnection(rs, ps, conn);
    return count;
}

猜你喜欢

转载自blog.csdn.net/weixin_33738982/article/details/87638276