分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

PageHelper分页插件及通用分页js

发布时间:2023-09-06 02:16责任编辑:傅花花关键词:js

 分页概述

1.物理分页

物理分页依赖的是某一物理实体,这个物理实体就是数据库,比如MySQL数据库提供了limit关键字,程序员只需要编写带有limit关键字的SQL语句,数据库返回的就是分页结果。建议使用。

2.逻辑分页

逻辑分页依赖的是程序员编写的代码。数据库返回的不是分页结果,而是全部数据,然后再由程序员通过代码获取分页数据,常用的操作是一次性从数据库中查询出全部数据并存储到List集合中,因为List集合有序,再根据索引获取指定范围的数据。

MyBatis 分页插件 - PageHelper

该插件目前支持以下数据库的物理分页:

  1. Oracle
  2. Mysql
  3. MariaDB
  4. SQLite
  5. Hsqldb
  6. PostgreSQL
  7. DB2
  8. SqlServer(2005,2008)
  9. Informix
  10. H2
  11. SqlServer2012
  12. Derby
  13. Phoenix

分页插件 5.0

由于分页插件 5.0 版本和 4.2.x 实现完全不同,所以 master 分支为 5.x 版本,4.2 作为一个分支存在,如果有针对 4.2 的 PR,请注意提交到分支版本。

集成

使用 PageHelper 你只需要在 classpath 中包含 pagehelper-x.x.x.jar 和 jsqlparser-0.9.5.jar。

如果你使用 Maven,你只需要在 pom.xml 中添加下面的依赖:

<dependency> ???<groupId>com.github.pagehelper</groupId> ???<artifactId>pagehelper</artifactId> ???<version>最新版本</version></dependency>

本次开发环境:JDK1.8+MySql5.0+Mybatis-pagehelper4.2.1+maven

注意maven中的其他依赖的版本可能会影响该插件,导致引入失败

使用步骤

1.在maven pom.xml中添加依赖

2.在Mybatis-config.xml中sqlSessionFactory中添加插件配置

<!--分页插件-->
<property name="plugins">
???<array>
???????<bean class="com.github.pagehelper.PageHelper">
???????????<property name="properties">
???????????????<value>
???????????????????dialect=mysql
???????????????</value>
???????????</property>
???????</bean>
???</array>
</property>

3.写sql、service

 ?public PageInfo<Student> selectByUnitId(Integer id,Integer currPage,Integer pageSize) { ???????PageHelper.startPage(currPage,pageSize);//当前页面编号+(从第0页开始),每页的大小 ???????PageInfo<Student> pageInfo ?= new PageInfo<Student>(studentDao.selectByUnitId(id)); ???????return pageInfo; ???}
建议sql不要这样简单粗暴的查询
sql:select * from student;
即使如此pageHelper插件也会自动的查询指定的条数
PageInfo:大致包含以下信息:{pageNum=1, pageSize=5, size=5, startRow=1, endRow=5, total=10, pages=2, list{...}}
每次查询之后将指定条数的数据放在PageInfo中。
在Controller层 把pageInfo放在requestScope中,命名student(与下文同)

 这样就可以根据pageNum输出指定的信息

jsp:分页标签

 ????这是一个完整的分页标签,只要更改参数即可
?????????????????????????<div class="text-center"> ???????????????????????????????????<nav> ???????????????????????????????????????<ul class="pagination"> ???????????????????????????????????????????<li> ???????????????????????????????????????????????<a href="<c:url value="/student?unitId=${param.unitId}&currPage=1&unitName=${param.unitName}"/>">首页</a> ???????????????????????????????????????????</li> ???????????????????????????????????????????<li> ???????????????????????????????????????????????<a href="<c:url value="/student?unitId=${param.unitId}&currPage=${student.pageNum-1>1?student.pageNum-1:1}&unitName=${param.unitName} ??"/>">&laquo;</a> ???????????????????????????????????????????</li> ???????????????????????????????????????????<c:forEach begin="1" end="${student.pages}" varStatus="loop"> ???????????????????????????????????????????????<c:set var="active" value="${loop.index==student.pageNum?‘active‘:‘‘}"/> ???????????????????????????????????????????????<li class="${active}"><a ???????????????????????????????????????????????????????href="<c:url value="/student?unitId=${param.unitId}&currPage=${loop.index}&unitName=${param.unitName}"/>">${loop.index}</a> ???????????????????????????????????????????????</li> ???????????????????????????????????????????</c:forEach> ???????????????????????????????????????????<li> ???????????????????????????????????????????????<a href="<c:url value="/student?unitId=${param.unitId}&currPage=${student.pageNum+1<student.pages?student.pageNum+1:student.pages}&unitName=${param.unitName}"/>">&raquo;</a> ???????????????????????????????????????????</li> ???????????????????????????????????????????<li> ???????????????????????????????????????????????<a href="<c:url value="/student?unitId=${param.unitId}&currPage=${student.pages}&unitName=${param.unitName}"/>">尾页</a> ???????????????????????????????????????????</li> ???????????????????????????????????????</ul> ???????????????????????????????????</nav> ???????????????????????????????</div>

js:分页代码

function createPaginationNav(e, t, a, n, p, o, i, s) { ???null == e && (e = ""), e = e.replace(/\&currPage=\d+\&/, "&"), e = e.replace(/\&?currPage=\d+\&?/, ""); ???var r = e.length; ???r > 0 && "?" == e.charAt(r - 1) && (e = e.replace("?", "")), null == i && (i = ""), "undefined" == typeof s && (s = 10); ???var g = s, ???????l = e, ???????f = l.indexOf("?"); ???if (l += f > 0 ? "&currPage=" : "?currPage ?=", document.write(‘<span style="font-size:12px;">第‘ + t + "页&nbsp;</span>"), document.write(‘<span style="font-size:12px;">共‘ + p + "页&nbsp;</span>"), p > 1) { ???????1 == t ? (document.write(‘<a href="#">首页</a>‘), document.write(‘<a href="#" title="上一页"><span style="font-size:12px;">&lt;&lt;</span></a>‘)) : (document.write(‘<a href="‘ + l + ‘1">首页</a>‘), document.write(‘<a href="‘ + l + a + ‘" title="上一页"><span style="font-size:12px;">&lt;&lt;</span></a>‘)); ???????var d = 1; ???????if (p > g) { ???????????var u = 0, ???????????????c = 0, ???????????????m = Math.round(g / 2); ???????????for (d = p / g + 1, t > m && p - m >= t ? (u = t - m, c = t + m - 1) : m >= t ? (u = 1, c = g) : (u = p - g + 1, c = p), c > p && (c = p), ipage = u; c >= ipage; ipage++) p >= ipage && document.write(t == ipage ? ‘<a href="‘ + l + ipage + ‘" ><span style="font-size:18px;color:red;">‘ + ipage + "</span></a>" : ‘<a href="‘ + l + ipage + ‘" ><span style="font-size:12px;">‘ + ipage + "</span></a>") ???????} else ???????????for (ipage = 1; p >= ipage; ipage++) document.write(t == ipage ? ‘<a href="‘ + l + ipage + ‘" ><span style="font-size:18px;color:red;">‘ + ipage + "</span></a>" : ‘<a href="‘ + l + ipage + ‘" ><span style="font-size:12px;">‘ + ipage + "</span></a>"); ???????t == p ? (document.write(‘<a href="#" title="下一页"><span style="font-size:12px;">&gt;&gt;</span></a>‘), document.write(‘<a href="#">尾页</a>‘)) : (document.write(‘<a href="‘ + l + n + ‘" title="下一页"><span style="font-size:12px;">&gt;&gt;</span></a>‘), document.write(‘<a href="‘ + l + p + ‘">尾页</a>‘)), document.write(‘<input type="text" id="gotopage‘ + i + ‘" size="2" onkeypress="if (event.keyCode == 13)goto_page(\‘‘ + l + "‘,‘" + i + ‘\‘)">&nbsp;&nbsp;<a href="#" onclick="goto_page(\‘‘ + l + "‘,‘" + i + ‘\‘)"><span style="font-size:12px;">GOTO</span></a>‘) ???}}function goto_page(e, t) { ???var a = /^[0-9]+$/; ???return gotoInputId = "gotopage" + t, a.test(document.getElementById(gotoInputId).value) ? void(location.href = e + document.getElementById(gotoInputId).value) : (alert("请输入数字!"), document.getElementById(gotoInputId).value = "", !1)}

//在jsp中调用此函数
<div class="page right">
???<script language="javascript">
???????createPaginationNav(‘${pageContext.request.contextPath}/student?unitId=${param.unitId}&currPage=0‘,
???????????${student.pageNum},
???????????${student.prePage},
???????????${student.nextPage},
???????????${student.pages},
???????????${student.pageSize},
???????????‘‘);
???</script>
</div>
 

PageHelper分页插件及通用分页js

原文地址:https://www.cnblogs.com/jin-nuo/p/9734358.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved