pageHelper分页插件 --> mybatis专门的分页插件
1.在pom.xml中添加分页插件坐标
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
2.在mybatis的配置文件里加入pageHelper的配置
<plugins>
<plugin interceptor=\"com.github.pagehelper.PageInterceptor\">
<!-- config params as the following -->
<!--<!–分页参数合理化 –>-->
<property name=\"reasonable\" value=\"true\"/>
</plugin>
</plugins>
注意加载顺序问题,settings-->typeAliases-->plugins
3. 在要进行分页的action中进行如下配置
@RequestMapping(\"/findAllProducts\")
public ModelAndView findAllProducts(
@RequestParam(required=false,defaultValue=\"1\",value=\"pi\") Integer pageIndex,
@RequestParam(required=false,defaultValue=\"10\",value=\"ps\") Integer pageSize)throws Exception{
// 创建方法的返回值
ModelAndView mv = new ModelAndView();
// 设定分页条件
//在查询之前传入当前页,然后多少记录
PageHelper.startPage(pageIndex,pageSize);
//使用PageInfo包装查询结果,只需要将pageInfo交给页面就可以
PageInfo pageInfo = new PageInfo<Product>(productService.findAllProducts(),pageSize/2);
// 保存数据
mv.addObject(\"pageInfo\", pageInfo);
// 转发地址
mv.setViewName(\"product-list\");
// 返回
return mv;
}
此处要注意pageInfo的用法,和PageHelper的设置
4.在要分页的页面加入如下配置
<!-- 分页开始 -->
<div class=\"box-footer\">
<div class=\"pull-left\">
<div class=\"form-group form-inline\">
当前第 ${pageInfo.pageNum} 页.总共 ${pageInfo.pages} 页.一共 ${pageInfo.total} 条记录.
每页 <select class=\"form-control\" onchange=\"changePS(${pageInfo.pageNum},this);\">
<option <c:if test=\"${pageInfo.pageSize == 10}\">selected</c:if>>10</option>
<option <c:if test=\"${pageInfo.pageSize == 15}\">selected</c:if>>15</option>
<option <c:if test=\"${pageInfo.pageSize == 20}\">selected</c:if>>20</option>
<option <c:if test=\"${pageInfo.pageSize == 25}\">selected</c:if>>25</option>
<option <c:if test=\"${pageInfo.pageSize == 30}\">selected</c:if>>30</option>
</select> 条
</div>
</div>
<div class=\"box-tools pull-right\">
<ul class=\"pagination\">
<!-- 首页 -->
<li><a href=\"###\" onclick=\"goPage(1);\">首页</a></li>
<!-- 上一页 -->
<li>
<c:if test=\"${pageInfo.hasPreviousPage}\">
<a href=\"###\" onclick=\"goPage(${pageInfo.pageNum-1});\" aria-label=\"Previous\">
<span aria-hidden=\"true\">«</span>
</a>
</c:if>
</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=\"###\" onclick=\"goPage(${page_num});\">${page_num}</a></li>
</c:if>
</c:forEach>
<!--下一页-->
<li>
<c:if test=\"${pageInfo.hasNextPage}\">
<a href=\"###\" aria-label=\"Next\" onclick=\"goPage(${pageInfo.pageNum+1});\">
<span aria-hidden=\"true\">»</span>
</a>
</c:if>
</li>
<!-- 尾页 -->
<li><a href=\"###\" onclick=\"goPage(${pageInfo.pages});\">尾页</a></li>
</ul>
</div>
</div>
<!-- 分页结束 -->
5. 编写JS方法
<!-- 分页函数 -->
<script type=\"text/javascript\">
function goPage(pageIndex){
window.location.href=\"${pageContext.request.contextPath}/ProductViewAction/findAllProducts?pi=\"+pageIndex;
}
function changePS(pageIndex,pageSize){
window.location.href=\"${pageContext.request.contextPath}/ProductViewAction/findAllProducts?pi=\"+pageIndex+\"&ps=\"+pageSize.value+\"\";
}
</script>
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。



