SSM批量删除和搜索的实现

    技术2023-05-15  86

    搜索部分

    <form action="${pageContext.request.contextPath}/user/findAll.do?type=1" method="post"> <div class="col-md-4 data1"> <input type="text" class="form-control" name="username" placeholder="username" value=""> </div> <button type="submit" class="btn bg-maroon">搜索</button> </form>

    跳转到搜索的controller类里

    @RequestMapping("/findAll.do") public ModelAndView findALL(@RequestParam(defaultValue = "1") int currentPage,String username,@RequestParam(defaultValue = "0") int type, HttpSession session){ if(type==1){ session.setAttribute("searchname",username); }else{ username = (String) session.getAttribute("searchname"); } PageInfo<User> pageInfo = userService.findAll(currentPage,username); ModelAndView modelAndView=new ModelAndView(); //将查询的数据放入到ModelAndView对象中 //"itemList":为别名,在前端页面上使用 //items:执行sql语句查询到的数据 modelAndView.addObject("pageInfo", pageInfo); modelAndView.setViewName("user-list"); return modelAndView; }

    新写了个page用来分辨是搜索显示还是全部显示,现在看来这个方法已经从上上节课的简单到现在麻烦的一匹了。

    <select id="findAll" resultType="user"> select * from tb_user <if test = "username!=null and username!=''"> where username like concat("%",#{username},"%") </if> limit #{start},5 </select>

    sql语句,写了个if分辨把上面传过来的username判断搜索显示还是全部显示。 达成

    批量删除

    <button type="button" class="btn btn-default" title="删除" onclick="deleteAll()"> <i class="fa fa-refresh"></i> 删除 </button> function deleteAll() { var checkedNum=$("input[name='ids']:checked").length; alert(checkedNum); if(checkedNum==0){ alert("请至少选择一个进行删除!!!"); return; } if(confirm("确认要删除这些用户吗?")){ var userList=new Array(); $("input[name='ids']:checked").each( function () { userList.push($(this).val()) } ); alert(userList); $.ajax({ type:"post", url: "${pageContext.request.contextPath}/user/deleteAll.do", data:{userList:userList.toString()}, success:function () { alert("删除成功"); location.reload(); }, error:function () { alert("删除失败"); } }); } }

    写个script获取选中的量和id,传到deleteall.do

    @RequestMapping("/deleteAll.do") public String deleteAll(String userlist){ String[] strs = userlist.split(","); List<Integer> ids = new ArrayList<>(); for(String s:strs){ ids.add(Integer.parseInt(s)); } userService.deleteAll(ids); return "redirect:findAll.do"; } <delete id="deleteAll" parameterType="list"> delete from tb_user where id in <foreach collection="ids" item="id" open="(" close=")" separator=","> #{id} </foreach> </delete>

    sql语句删除,这部分理解不深,主要很多script语句和 sql语句是第一次见

    Processed: 0.016, SQL: 9