SSM批量删除和搜索的实现

搜索部分

<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语句是第一次见

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值