SSM用户管理分页查询
程序员文章站
2024-03-14 19:58:47
...
First:dao层不变。
//查询全部
List<User> findAll();
Second:service层添加形参
List<User> findAll(Integer page,Integer size);
@Override
public List<User> findAll(Integer page,Integer size) {
PageHelper.startPage(page,size);
return userDao.findAll();
}
Third:Controller实现
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "5") Integer size){
//调用service中的方法,得到查询结果
List<User> userList = userService.findAll(page,size);
PageInfo pageInfo = new PageInfo(userList);
ModelAndView mv = new ModelAndView();
mv.addObject("pageInfo",pageInfo);
mv.setViewName("user-list");
return mv;
}
Fourth:修改jsp参数
<c:forEach var="user" items="${pageInfo.list}">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td class="text-center">
<a href="${pageContext.request.contextPath}/user/updateCheckedById.do?id=${user.id}" class="btn bg-olive btn-xs">更新</a>
<a href="${pageContext.request.contextPath}/user/delUserById.do?id=${user.id}" class="btn bg-olive btn-xs">删除</a>
<a href="#" class="btn bg-olive btn-xs">添加角色</a>
</td>
</tr>
</c:forEach>
<ul class="pagination">
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=1&size=5" aria-label="Previous">首页</a></li>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfo.pageNum-1}&size=5">上一页</a></li>
<c:forEach begin="1" end="${pageInfo.pages}" var="pageNum">
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageNum}&size=5">${pageNum}</a></li>
</c:forEach>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfo.pageNum+1}&size=5">下一页</a></li>
<li><a href="${pageContext.request.contextPath}/user/findAll.do?page=${pageInfo.pageSize}" aria-label="Next">尾页</a></li>
</ul>
Finally:调试页面
Tips:如果出现500提示内部服务器错误:
- PageInfo和PageHepler有没有弄错
- ${pageInfo.list}要加list方法
上一篇: SSM框架实现分页查询例子
下一篇: Java JDK动态代理实现原理