目录
一、单个删除
二、批量删除
三、页面展示
与其他增查改功能的实现方法类似,在这里一笔带过:
1、在dao层的接口写 deleteById 的方法。
2、在resources层的 中写按id删除的SQL实现语句。
// resources.mapper.UserInfoMapper.xml <delete id="deleteById" parameterType="int"> delete from userinfo where id=#{id} </delete>3、在service层写 deleteById 的方法。
4、controller层的对应路径标签处完成功能方法。
@RequestMapping("delete.do") public String delete(int id){ userInfoService.deleteById(id); return "redirect:findAll.do"; }5、在页面的单个删除按钮标签处添加路径。
<a href="${pageContext.request.contextPath}/user/delete.do?id=${userInfos.id}" class="btn bg-olive btn-xs">删除</a>1、在webapp下导入jar包。
2、在页面的jsp中引入jar包。
//webapp.pages.user-list.jsp <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.2.1.min.js"></script>3、在页面的jsp中写批量删除的方法。
//webapp.pages.user-list.jsp <script type="text/javascript"> function deleteAll() { var checkNum = $("input[name='ids']:checked").length; if (checkNum == 0) { alert("请至少选择一项"); return; } if (confirm("确定要删除吗?")) { var userList = new Array(); $("input[name='ids']:checked").each(function () { userList.push($(this).val()) }); } $.ajax({ type:"post", url:"${pageContext.request.contextPath}/user/deleteAll.do", data:{userList:userList.toString()}, success:function () { alert("删除成功"); location.reload(); }, error:function () { alert("删除失败"); } }) } </script>4、在批量删除按钮的标签处声明方法名。
//user-list.jsp <button type="button" class="btn btn-default" title="批量删除" onclick="deleteAll()"> <i class="fa fa-refresh"></i> 批量删除 </button>5、在页面勾选框的标签处添加值,以获得要删除用户的id列表。
//user-list.jsp <td><input name="ids" type="checkbox" value="${userInfos.id}"></td>6、在dao层的接口写 deleteAll 的方法。
7、在resources层的 中写按id删除的SQL实现语句。
<delete id="deleteAll" parameterType="list"> delete from userinfo where id in <foreach collection="list" item="id" open="(" close=")" separator=","> #{id} </foreach> </delete>8、在service层写的 deleteAll 方法。
9、在ajax的url对应的路径的标签处写controller层完成功能方法。
//controller.UserInfoController @RequestMapping("deleteAll.do") @ResponseBody public String deleteAll(String userList){ String[] strings=userList.split(","); List<Integer> ids=new ArrayList<>(); for(int i=0;i<strings.length;i++){ ids.add(Integer.parseInt(strings[i])); } userInfoService.deleteAll(ids); return ""; }