JAVAWEB开发批量删除,SSM的几种情况
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>VUE联系</title> <!--自动识别最新稳定版本--> <!--<script src="https://unpkg.com/vue/dist/vue.min.js"></script>--> <!--<script src="https://unpkg.com/vue@2.6.10/dist/vue.min.js"></script>--> <script src="../../js/jquery-3.3.1.min.js"></script> <style type="text/css"> table{ border: black 1px solid; } table td{border:1px solid #00b7ee;background-color: #6ce26c } th{background-color: #d5008f} </style> </head> <body> <table CELLPADDING="1" CELLSPACING="1"> <input type="button" onclick="batchDelete()" value="删除"/> <input type="button" onclick="boxAll()" value="反选/全选"/> <tr> <th>编号</th> <th>电话</th> <th>密码</th> <th>邮箱</th> <th>时间</th> </tr> <#if userList??> <#list userList as item> <tr> <td> <input type="checkbox" name="userNmae" value="${item.id}" /> </td> <td> <span>${item.phone}</span> </td> <td> <span>${item.password}</span> </td> <td> <span>${item.email}</span> </td> <td> <span>${item.times}</span> </td> </tr> </#list> </#if> </table> </body> <script> function batchDelete(){ //判断至少选择了一项 var checkedNum = $("input[type='checkbox']:checked").length; if (checkedNum == 0) { alert("至少选择一项删除!"); return; } if (confirm("确定删除选中的用户?")) { var userList = new Array(); $("input[type='checkbox']:checked").each(function(){ userList.push($(this).val()); }); $.ajax({ type : "post", url : "/user/batchDelete", data : {"userList" : userList.toString()}, dataType:"JSON", success : function(){ alert("删除成功!"); location.reload(); }, error : function(){ alert("刪除失败!") } }); } } //全选 全不选 var flag=true; function boxAll(){ var cd=$("input[type=checkbox]"); for (var i=0;i<cd.length;i++) { cd[i].checked=flag; } flag=!flag; } </script> </html>
public String batchDelete(HttpServletRequest request,HttpServletResponse response){ Result result=new Result(); String userIdListString= request.getParameter("userList"); String[] userIdList=userIdListString.split(","); try{ int num= this.userService.batchDelete(userIdList); }catch (Exception e){ result.setMessage(ExceptionMes.Error_Message02); result.setNo(ExceptionMes.Error_Code02); log.error(JSON.toJSONString(result)); } result.setMessage(ExceptionMes.Error_Message17); result.setNo(ExceptionMes.Error_Code17); return JSON.toJSONString(result); }
<delete id="batchDelete"> DELETE FROM `user` where `id` in <foreach collection="array" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach> </delete>
分别问前端,后台,和MAPPER.
这里注意前端的Array.toString 到后台接收时只是一个集合的字符串,如果需要传入数组或者集合,需要使用String.split(",")进行分割转换。
<!--collection="array"入残维数组-->
<!--collection="ids"入残为M安排-->
<!--collection="list"入残为集合-->
这三种情况对对号入座我就不多少了
一点点学习,一丝丝进步。不懈怠,才不会被时代淘汰