SSM通过PageHelper进行分页操作

SSM通过PageHelper进行分页操作

第一次写博客,请各位前辈多多指导,谢谢

  1. 首先在整合好SSM的前提下,添加PageHelper的依赖:
	<pagehelper.version>5.1.11pagehelper.version>
	<dependency>
          <groupId>com.github.pagehelpergroupId>
          <artifactId>pagehelperartifactId>
          <version>${pagehelper.version}version>
    dependency>
  1. 用PageInfo封装要分页的对象,例如:
@RequestMapping("/list")
    public String A2(@RequestParam(value = "page", defaultValue = "1")int page, Model model, String name){
        PageHelper.startPage(page, 5); //page表示当前页数,5表示每页的最大数量
        List<User> u = userService.findAll(name);
        PageInfo pageInfo = new PageInfo(u); //用PageInfo封装List对象
        model.addAttribute("pageInfo", pageInfo); //将pageInfo对象存入model,以供前端页面使用
        return "user/list";
    }
  1. 在前端页面进行数据的显示:
<c:forEach items="${pageInfo.list}" var="u">
    *****
</c:forEach>
<p>第${pageInfo.pageNum},共${pageInfo.pages},共${pageInfo.total}条数据</p>
<a href="${pageContext.request.contextPath}/user/list?page=1">首页</a>
<c:if test="${pageInfo.hasPreviousPage}">
    <a href="${pageContext.request.contextPath}/user/list?page=${pageInfo.pageNum-1}">上一页</a>
</c:if>

<c:if test="${pageInfo.hasNextPage}">
    <a href="${pageContext.request.contextPath}/user/list?page=${pageInfo.pageNum+1}">下一页</a>
</c:if>

<a href="${pageContext.request.contextPath}/user/list?page=${pageInfo.pages}">末页</a>

你可能感兴趣的:(分页)