ssm批量删除

ssm批量删除

批量删除:顾名思义就是一次性删除多个。删除是根据前台传给后台的id,那么所谓批量删除,就是将多个id传给后台,那么如何传过去呢,前后台的交互该如何实现?

1.jsp页面,先选中所有的要删除的数据,将多个id通过数组的形式传递给后台

<body>
    <button type="button" class="btn btn-danger" id="dele">批量删除</button>
    <table class="table table-hover">
        <tr>
            <th><input type="checkbox" id="check1"></th>
            <th>序号</th>
            <th>姓名</th>
            <th>生日</th>
            <th>图片</th>
            <th>下载</th>
            <th>操作</th>
        </tr>
        <c:forEach varStatus="status" items="${demos }" var="demo">
            <tr>
                <td><input type="checkbox" value="${demo.id }" class="checkbox">
                </td>
                <td>${status.index+1 }</td>
                <td>${demo.uname }</td>
                <td>
                    <fmt:formatDate value="${demo.borth }" pattern="yyyy-MM-dd"/>
                </td>
                <td>
                    <c:if test="${empty demo.image }">
                        <img src="${demo.image }">
                    </c:if>
                </td>
                <td>下载</td>
                <td></td>
            </tr>
        </c:forEach>
    </table>
</body>
<script type="text/javascript">
    $(function(){
        $("#check1").click(function(){
            $("td > input").prop("checked",this.checked);
        });
        $("#dele").click(function(){
            var idArr = new Array();
            $.each($(".checkbox:checked"),function(){
                //alert($(this).val());
                idArr.push($(this).val());
            });
            $.ajax({
                url:"${pageContext.request.contextPath}/deletes",
                type:"POST",
                traditional : "true",
                dataType:"json",
                data:{
                    "idArr":idArr
                },
                success:function(){
                    
                }
            });
        });
    });
    
</script>

(2)controller

@RequestMapping(value="/deletes",method = RequestMethod.POST)
    @ResponseBody
    public String deletes(@RequestParam(value="idArr")String[] idArr) {
        System.out.println("666");

          List<Integer> list=new ArrayList<Integer>(); 
          int ids;
          for (String string : idArr) { 
              ids=Integer.parseInt(string); 
              list.add(ids); 
              }

          demoService.deleteDemos(list);
          return "success";
        
    }

 (3)dao

public void deleteDemos(@Param("ids")List<Integer> ids);

(4)mapper

<delete id="deleteDemos">
        delete from demo where id in
        <foreach collection="ids" item="item_id" separator="," open="(" close=")">
                     #{item_id}
      </foreach>
    </delete>

 

posted @ 2019-06-13 17:53  司徒二条  阅读(771)  评论(0编辑  收藏  举报