ssm下使用分页插件PageHelper进行分页

1. 导入maven依赖:

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.</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); 这条语句后面!!

 /**
* 查看所有问题
*
* @param model
* @return
*/
@RequestMapping("/findAll")
public String findAll(@RequestParam(value="pn",defaultValue="1")Integer pn, Model model) {
//从第一条开始 每页查询10条数据
PageHelper.startPage(pn, 10);
List<Question> questions = questionService.findAll();
//将用户信息放入PageInfo对象里
PageInfo page = new PageInfo(questions,10);
model.addAttribute("pageInfo", page);
return "question_list";
}

4. jsp页面显示:question_list.jsp

         <%--页面数据展示--%>
<div id="table-div">
<table class="table table-hover">
<c:forEach items="${pageInfo.list}" var="question" varStatus="vs">
<tr>
<td class="td1">
<a href="${pageContext.request.contextPath}/question/look?qid=${question.qid}">
<img src="${pageContext.request.contextPath}/images/question_logo.png"
style="width: 20px;height: 20px;"/> ${question.title}
</a>
<font color="red"> - [悬赏 ${question.credit} 积分] </font>
<c:if test="${question.isResolved == 1}">
(已解决)
</c:if>
</td>
<td class="td2">
<fmt:formatDate value="${question.askTime}" pattern="yyyy-MM-dd HH:mm"/>
&nbsp;&nbsp;&nbsp;
</td>
</tr>
</c:forEach>
</table>
</div> <!-- 分页条 -->
<div id="div_pagination_bottom">
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="${pageContext.request.contextPath}/question/findAll?pn=${pageInfo.pageNum-1}"
aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<c:forEach items="${pageInfo.navigatepageNums }" var="page_Num">
<c:if test="${page_Num == pageInfo.pageNum }">
<li class="active"><a href="#">${ page_Num}</a></li>
</c:if>
<c:if test="${page_Num != pageInfo.pageNum }">
<li>
<a href="${pageContext.request.contextPath}/question/findAll?pn=${ page_Num}">${ page_Num}</a>
</li>
</c:if>
</c:forEach>
<li>
<a href="${pageContext.request.contextPath}/question/findAll?pn=${pageInfo.pageNum+1}"
aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
上一篇:Java在Linux下 不能处理图形的解决办法 Can't connect to X11 window server


下一篇:Mybatis分页插件PageHelper的配置和使用方法