<!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"入殘為集合-->
這三種情況對對號入座我就不多少了