1.在pom文件中加入jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.0</version>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.0</version>
</dependency>
2.在Mybatis配置文件中添加插件
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 指定数据库方言 -->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 指定数据库方言 -->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
3.在Controller中找到自己的查询数据的方法,在方法中加入以下参数
@RequestParam(value = "PageNum", defaultValue = "1") Integer PageNum, @RequestParam(value = "PageSize", defaultValue = "5") Integer PageSize
4.在service实现类中写入分页的方法,也可以写在Controller中,最好是在service实现类中
//设置分页
PageHelper.startPage(PageNum,PageSize);
//执行查询方法
List<user> list=rs.selectuser();
//取得分页结果
PageInfo pageInfo=new PageInfo( list);
model.addAttribute("pageInfo",pageInfo);
PageHelper.startPage(PageNum,PageSize);
//执行查询方法
List<user> list=rs.selectuser();
//取得分页结果
PageInfo pageInfo=new PageInfo( list);
model.addAttribute("pageInfo",pageInfo);
例子:
@RequestMapping("/selectNews") public String selectNews(Model model, @RequestParam(value = "PageNum", defaultValue = "1") Integer PageNum, @RequestParam(value = "PageSize", defaultValue = "5") Integer PageSize ) { PageHelper.startPage(PageNum, PageSize); List<News> list = iNewsService.selectNews(); PageInfo pageInfo = new PageInfo(list); model.addAttribute("pageInfo", pageInfo); model.addAttribute("list", list); return "/weihu/select"; }
5.在jsp页面中加入以下代码
<c:choose>
<c:when test="${pageInfo.size > 0 }">
<div class="feny">
<div class="manu">
<span>显示${pageInfo.startRow }到${pageInfo.endRow}共${pageInfo.total}条</span> <input
type="hidden" id="page" name="page"> <input type="hidden"
id="pageSize" name="pageSize"> <a
href="<%=basePath%>/friend/selectUserAll.action?PageNum=1">首页 </a>
<c:choose>
<c:when test="${pageInfo.hasPreviousPage }">
<a href="<%=basePath%>/friend/selectUserAll.action?PageNum=${pageInfo.pageNum-1}">上一页
</a>
</c:when>
<c:otherwise>
<span>上一页</span>
</c:otherwise>
</c:choose>
<c:forEach var="item" items="${pageInfo.navigatepageNums}">
<c:choose>
<c:when test="${pageInfo.pageNum == item }">
<span class="current">${pageInfo.pageNum }</span>
</c:when>
<c:otherwise>
<a href="<%=basePath%>/friend/selectUserAll.action?PageNum=${item}">${item}</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${!pageInfo.isLastPage }">
<a href="<%=basePath%>/friend/selectUserAll.action?PageNum=${pageInfo.pageNum+1}">下一页
</a>
<a href="<%=basePath%>/friend/selectUserAll.action?PageNum=${pageInfo.lastPage}">尾页</a>
</c:when>
<c:otherwise>
<span>下一页</span>
<span>尾页</span>
</c:otherwise>
</c:choose>
</div>
</div>
</c:when>
<c:otherwise>
<div class="feny">
<span class="emptyData">没有数据可以显示</span>
</div>
</c:otherwise>
</c:choose>
<c:when test="${pageInfo.size > 0 }">
<div class="feny">
<div class="manu">
<span>显示${pageInfo.startRow }到${pageInfo.endRow}共${pageInfo.total}条</span> <input
type="hidden" id="page" name="page"> <input type="hidden"
id="pageSize" name="pageSize"> <a
href="<%=basePath%>/friend/selectUserAll.action?PageNum=1">首页 </a>
<c:choose>
<c:when test="${pageInfo.hasPreviousPage }">
<a href="<%=basePath%>/friend/selectUserAll.action?PageNum=${pageInfo.pageNum-1}">上一页
</a>
</c:when>
<c:otherwise>
<span>上一页</span>
</c:otherwise>
</c:choose>
<c:forEach var="item" items="${pageInfo.navigatepageNums}">
<c:choose>
<c:when test="${pageInfo.pageNum == item }">
<span class="current">${pageInfo.pageNum }</span>
</c:when>
<c:otherwise>
<a href="<%=basePath%>/friend/selectUserAll.action?PageNum=${item}">${item}</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${!pageInfo.isLastPage }">
<a href="<%=basePath%>/friend/selectUserAll.action?PageNum=${pageInfo.pageNum+1}">下一页
</a>
<a href="<%=basePath%>/friend/selectUserAll.action?PageNum=${pageInfo.lastPage}">尾页</a>
</c:when>
<c:otherwise>
<span>下一页</span>
<span>尾页</span>
</c:otherwise>
</c:choose>
</div>
</div>
</c:when>
<c:otherwise>
<div class="feny">
<span class="emptyData">没有数据可以显示</span>
</div>
</c:otherwise>
</c:choose>