分页概述

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>
 

MyBatis plus通用Mapper实现分页

安装:https://www.cnblogs.com/jin-nuo/p/9734599.html

逻辑分页
private ApplicationContext ioc =
new ClassPathXmlApplicationContext("applicationContext.xml");
private EmployeeMapper employeeMapper=
ioc.getBean("employeeMapper", EmployeeMapper.class);
    //5. 分页查询page集成rowBounds Page(currPage,Pagesize);(内存分页)
    List<Employee> emps = employeeMapper.selectPage(new Page<>(3, 2), null);
    System.out.println(emps);

 使用Mybatisplus插件中的PageIntercepter插件实现物理分页

   <property name="plugins">
<!--分页插件注册-->
<list>
<bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"></bean>
</list>
</property>
//测试分页
public void testPaginationInterceptor(){
IPage<Employee> pages =employeeMapper.selectPage(new Page<>(1,5),null);//即可实现分页
System.out.println(pages.getRecords());
}
sql执行:说明这是一个实实在在的物理分页
DEBUG 10-02 11:37:07,239 ==>  Preparing: SELECT id,last_name AS lastName,email,gender,age FROM tbl_employee
LIMIT 0,5 

IPage对象的方法:
default IPage<T> setPages(long pages) {
    return this;
}
List<T> getRecords();
IPage<T> setRecords(List<T> var1);
long getTotal();
IPage<T> setTotal(long var1);
long getSize();
IPage<T> setSize(long var1);
long getCurrent();
IPage<T> setCurrent(long var1);

PageHelper分页插件及通用分页js的更多相关文章

  1. SSM 使用 mybatis 分页插件 pagehepler 实现分页

    使用分页插件的原因,简化了sql代码的写法,实现较好的物理分页,比写一段完整的分页sql代码,也能减少了误差性. Mybatis分页插件 demo 项目地址:https://gitee.com/fre ...

  2. JSP分页之结合Bootstrap分页插件进行简单分页

    结合Bootstrap的分页插件实现分页,其中策略是每次显示5个按钮,然后根据当前页的不同来进行不同的显示: 1. 当前页<3,如果当前页大于5页就显示前五页,不然就显示1~totalPage. ...

  3. 整合mybatis分页插件及通用接口测试出现问题

    严重: Servlet.service() for servlet [springmvc] in context with path [/mavenprj] threw exception [Requ ...

  4. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  5. (转)淘淘商城系列——MyBatis分页插件(PageHelper)的使用以及商品列表展示

    http://blog.csdn.net/yerenyuan_pku/article/details/72774381 上文我们实现了展示后台页面的功能,而本文我们实现的主要功能是展示商品列表,大家要 ...

  6. 单纯用JS做的分页插件

    最近公司需要用到分页插件,由于市面上大多都是jQuery的分页插件,而且项目中有自己的工具类,所以说我在工具类中又添加了不依赖jQuery的分页插件,而且分页插件来说对DOM的操作也不是很多,就是用J ...

  7. Jquery 分页插件 Jquery Pagination

    Jquery 分页插件 Jquery Pagination 分页插件来说,我觉得适用就行,尽量简单然后能够根据不同的应用场景能够换肤.展现形式等. 对于初学者想写分页插件的同学,也可以看下源码,代码也 ...

  8. Flask学习之旅--分页功能:分别使用 flask--pagination 和分页插件 layPage

    一.前言 现在开发一个网站,分页是一个很常见的功能了,尤其是当数据达到一定量的时候,如果都显示在页面上,会造成页面过长而影响用户体验,除此之外,还可能出现加载过慢等问题.因此,分页就很有必要了. 分页 ...

  9. Mybatis分页插件的使用流程

    如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件.该插件支持任何复杂的单表.多表分页. 1.引入PageHelper的jar包 在pom.xml中添加如下依赖: 12345 ...

随机推荐

  1. PyCharm 项目删除

    Pycharm 删除项目具体操作如下: 1.选择菜单 File   close project 2.选择要删除的项目右上角选择× 3.找到项目所在目录,删除相应文件夹 之后再次打开pycharm 发现 ...

  2. ABP架构学习系列四:集成Dapper

    之前,一直想集成Dapper到项目中,但是一直没成功,今天把abp升级到最新版,然后按教程来,就可以了,呵呵    现在,基于上一篇的源码进行升级和集成dapper,将abp升级到3.8.2   官方 ...

  3. 终于有人把P2P、P2C、O2O、B2C、B2B、C2C 的区别讲透了!

    原文地址:https://www.cnblogs.com/sap-ronny/p/8149960.html P2P.P2C .O2O .B2C.B2B. C2C,每天看着这些常见又陌生的名词,如果有人 ...

  4. 关于typecho0.9代码高亮与数学公式支持

    闲来无事,搭了一个博客,记录一下自己的学习生活,博客模板取自原来typecho官方博客,稍加修改,改了一下涂装,不得不说插件支持有一些问题,目前大多数插件已经同步更新到typecho1.0版本,新插件 ...

  5. 留恋 nyoj 854

    留恋 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 大家都知道,高中的时候,座位基本都是固定的,但是对于视力不好却又坐在后面的人是很不公平的. 念情的高中班主任安哥 ...

  6. 【Flask】报错解决方法:AssertionError: View function mapping is overwriting an existing endpoint function: main.user

    运行Flask时出现了一个错误, AssertionError: View function mapping is overwriting an existing endpoint function: ...

  7. virtualenv安装及使用

    环境 Windows 10 python 3.6.7 安装 virtualenv用于创建虚拟环境,用于隔离不同的python版本的运行,是容器类软件.这里在Windows下通过pip安装: pip i ...

  8. 详解MariaDB数据库的外键约束

    1.什么是外键约束 外键约束(foreign key)就是表与表之间的某种约定的关系,由于这种关系的存在,我们能够让表与表之间的数据,更加的完整,关连性更强. 关于数据表的完整性和关连性,可以举个例子 ...

  9. Clipboard---将文本复制到剪切板上

    第一步:链接 Clipboard 的js文件 < script src = “ https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.m ...

  10. Netty 学习系列

    Netty实现httpserver简单示例 3个Java类实现最基本的接收请求,响应一个文本的简单http服务器. https://www.cnblogs.com/demingblog/p/99707 ...