批量删除、查询显示

部分相关代码

UserController

查询

@RequestMapping(value="/findAll.do")
    public ModelAndView findAll(@RequestParam(defaultValue = "1") int currentPage,String username,HttpSession session,@RequestParam(defaultValue = "0") int type)
    {

        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.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("user-list");
        return modelAndView;
    }

删除

    @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";
    }

UserService
删除

    @Override
    public void deleteAll(List<Integer> ids){
        userDao.deleteAll(ids);
    }

查询

@Override
    public PageInfo<User> findAll(int currentPage,String username){
        PageInfo<User> pageInfo = new PageInfo<>();
        pageInfo.setSize(5);

        int tc = userDao.getTotalCount(username);
        pageInfo.setTotalCount(tc);
        int tp = (int)Math.ceil(tc/5.0);
        pageInfo.setTotalPage(tp);
        if(currentPage<1){
            pageInfo.setCurrentPage(1);
        }else if(currentPage>tp){
            pageInfo.setCurrentPage(tp);
        }else{
            pageInfo.setCurrentPage(currentPage);
        }
        int start = (pageInfo.getCurrentPage()-1)*5;
        List<User> userList = userDao.findAll(start,username);
        pageInfo.setList(userList);
        return pageInfo;
    }

SQL

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


<delete id="deleteAll" parameterType="list">
        delete from tb_user where id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
              #{id}
        </foreach>
    </delete>

JSP

<div class="pull-left">
								<div class="form-group form-inline">
									<div class="btn-group">
										<button type="button" class="btn btn-default" title="新建"
												onclick="location.href='${pageContext.request.contextPath}/pages/user-add.jsp'" >
											<i class="fa fa-file-o"></i> 新建
										</button>
										
										<button type="button" class="btn btn-default" title="删除" onclick="deleteAll()">
											<i class="fa fa-refresh"></i> 删除
										</button>
									</div>
								</div>
							</div>
							<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>




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("删除失败");
                        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值