多条件分页查询: 修改pojo类user,添加两个属性
1 2 3 4 5 6 7 8 9 public class User { private Integer id; private String username; private String password; private String address; private Integer start; private Integer size; getter/setter... }
先创建一个pojo类Page
1 2 3 4 5 6 7 8 9 public class Page <T > { private int total; private int page; private int size; private int totalPage; private T t; private List<T> rows; getter/setter... }
写controller的处理器方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @RequestMapping ("users.action" )public String findUsers (Model model, @RequestParam(defaultValue = "1" ) Integer page, @RequestParam (defaultValue = "7" ) Integer size,Integer totalPage,String username,String address) { if (page<1 ){ page = 1 ; }else if (totalPage!=null ){ if (page>totalPage){ page = totalPage; } } Page<User> userslist = userService.selectUserList(page,size,username,address); model.addAttribute("page" ,userslist); return "usersList" ; }
接下来写service中对应的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public Page<User> selectUserList (Integer page, Integer size, String username, String address) { User user = new User(); user.setUsername(username); user.setAddress(address); user.setStart((page-1 )*size); user.setSize(size); int total = userDao.selectUserListCount(user); List<User> userslist = userDao.selectUserList(user); Page<User> result = new Page<User>(); result.setPage(page); result.setSize(size); result.setTotal(total); result.setT(user); result.setRows(userslist); result.setTotalPage(total%size==0 ?total/size:(total/size)+1 ); return result; }
最后写mapper文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <sql id ="selectUserListWhere" > <where > <if test ="username!=null" > and username like "%"#{username}"%" </if > <if test ="address!=null" > and address like "%"#{address}"%" </if > </where > </sql > <select id ="selectUserListCount" parameterType ="User" resultType ="int" > select count(*) from user <include refid ="selectUserListWhere" > </include > </select > <select id ="selectUserList" parameterType ="User" resultType ="User" > select * from user <include refid ="selectUserListWhere" > </include > <if test ="size!=null and start!=null" > limit #{start},#{size} </if > </select >
服务器端差不多写好了,开始写前端jsp(顺序按个人习惯):
这里会出现一个问题: 每次查询之后,点击翻页就会重新发起请求,导致结果集显示成不带查询条件的结果集。解决思路: 想办法保存查询条件,每次翻页时都携带着查询条件。
先在page类中加入一个封装查询条件的类(T t),在每次service对应方法中将查询条件也封装到page类的t属性中,每次翻页的时候通过get请求拼接的方法将当前页码(page.page+-1)、总页数(page.totalPage向前翻不需要)、查询条件(page.t.username page.t.address)传到分页查询方法中去。
还有另一种解决办法:使用伪分页,即通过前端分页。一次查出所有符合条件的结果集,然后每次翻页只显示部分。各有好坏:伪分页一次查询结果集大,后端分页查询次数多。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <%-- 条件查询部分的表单--%> <form action="${pageContext.request.contextPath}/users.action" class ="navbar-form navbar-left" method="post" > <div class ="form-group" > <input name="username" type="text" class ="form-control" placeholder="username" > <input name="address" type="text" class ="form-control" placeholder="address" > </div> <button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search"></span>查询</button> </form> <%-- 分页后,页码和翻页按钮部分 --%> <div id="pageNumber" class ="row" > <span class ="col-lg-offset-5 col-lg-7" > 当前:${page.page}/总计:${page.totalPage} </span> <a href="${pageContext.request.contextPath}/users.action?page=1&username=${page.t.username}&address=${page.t.address}" class="glyphicon glyphicon-fast-backward btn btn-lg col-lg-offset-4 col-lg-1"></a> <a href="${pageContext.request.contextPath}/users.action?page=${page.page-1}&username=${page.t.username}&address=${page.t.address}" class="glyphicon glyphicon-chevron-left btn btn-lg col-lg-1"></a> <a href="${pageContext.request.contextPath}/users.action?page=${page.page+1}&totalPage=${page.totalPage}&username=${page.t.username}&address=${page.t.address}" class="glyphicon glyphicon-chevron-right btn btn-lg col-lg-1"></a> <a href="${pageContext.request.contextPath}/users.action?page=${page.totalPage}&totalPage=${page.totalPage}&username=${page.t.username}&address=${page.t.address}" class="glyphicon glyphicon-fast-forward btn btn-lg col-lg-1"></a> </div>