ssm下使用分页插件PageHelper进行分页
Posted fengzeng666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssm下使用分页插件PageHelper进行分页相关的知识,希望对你有一定的参考价值。
1. 导入maven依赖:
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency>
2. 编写SqlConfig.xml配置文件(这个文件的名字任意)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- mybatis分页插件 --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins> </configuration>
3. 编写controller方法,注意:查询的方法必须紧跟在PageHelper.startPage(pn, 10); 这条语句后面!!
1 /** 2 * 查看所有问题 3 * 4 * @param model 5 * @return 6 */ 7 @RequestMapping("/findAll") 8 public String findAll(@RequestParam(value="pn",defaultValue="1")Integer pn, Model model) { 9 //从第一条开始 每页查询10条数据 10 PageHelper.startPage(pn, 10); 11 List<Question> questions = questionService.findAll(); 12 //将用户信息放入PageInfo对象里 13 PageInfo page = new PageInfo(questions,10); 14 model.addAttribute("pageInfo", page); 15 return "question_list"; 16 }
4. jsp页面显示:question_list.jsp
1 <%--页面数据展示--%> 2 <div id="table-div"> 3 <table class="table table-hover"> 4 <c:forEach items="${pageInfo.list}" var="question" varStatus="vs"> 5 <tr> 6 <td class="td1"> 7 <a href="${pageContext.request.contextPath}/question/look?qid=${question.qid}"> 8 <img src="${pageContext.request.contextPath}/images/question_logo.png" 9 style="width: 20px;height: 20px;"/> ${question.title} 10 </a> 11 <font color="red"> - [悬赏 ${question.credit} 积分] </font> 12 <c:if test="${question.isResolved == 1}"> 13 (已解决) 14 </c:if> 15 </td> 16 <td class="td2"> 17 <fmt:formatDate value="${question.askTime}" pattern="yyyy-MM-dd HH:mm"/> 18 19 </td> 20 </tr> 21 </c:forEach> 22 </table> 23 </div> 24 25 <!-- 分页条 --> 26 <div id="div_pagination_bottom"> 27 <nav aria-label="Page navigation"> 28 <ul class="pagination"> 29 <li> 30 <a href="${pageContext.request.contextPath}/question/findAll?pn=${pageInfo.pageNum-1}" 31 aria-label="Previous"> 32 <span aria-hidden="true">«</span> 33 </a> 34 </li> 35 <c:forEach items="${pageInfo.navigatepageNums }" var="page_Num"> 36 <c:if test="${page_Num == pageInfo.pageNum }"> 37 <li class="active"><a href="#">${ page_Num}</a></li> 38 </c:if> 39 <c:if test="${page_Num != pageInfo.pageNum }"> 40 <li> 41 <a href="${pageContext.request.contextPath}/question/findAll?pn=${ page_Num}">${ page_Num}</a> 42 </li> 43 </c:if> 44 </c:forEach> 45 <li> 46 <a href="${pageContext.request.contextPath}/question/findAll?pn=${pageInfo.pageNum+1}" 47 aria-label="Next"> 48 <span aria-hidden="true">»</span> 49 </a> 50 </li> 51 </ul> 52 </nav> 53 </div>
以上是关于ssm下使用分页插件PageHelper进行分页的主要内容,如果未能解决你的问题,请参考以下文章