批量删除(1课时)

批量删除

相关测试用例:

如果id为整数

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

如果id为字符串

public static void main(String[] args) {
        //String[] ids ---->('id1','id2','id3')
        String[] ids={"id1","id2","id3"};
        StringBuffer ids_str = new StringBuffer();
        for(int i=0;i

jsp页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 --id为整型

@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;
}

dao --id为字符串型

public int delAll(String[] ids) {
        int j=0;
        StringBuffer ids_str = new StringBuffer();
        for(int i=0;i

你可能感兴趣的:(批量删除(1课时))

选择 账号 email 身份证号 权限 操作
${user.userName } ${user.email } ${user.idCard } ${user.power } 修改