PageHelper分页插件及通用分页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>
 

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

时间: 2024-07-28 15:48:31

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

mybatis常用分页插件,快速分页处理

在未分享整个查询分页的执行代码之前,先了解一下执行流程. 1.总体上是利用mybatis的插件拦截器,在sql执行之前拦截,为查询语句加上limit X X 2.用一个Page对象,贯穿整个执行流程,这个Page对象需要用java编写前端分页组件 3.用一套比较完整的三层entity,dao,service支持这个分页架构 4.这个分页用到的一些辅助类 注:分享的内容较多,这边的话我就不把需要的jar一一列举,大家使用这个分页功能的时候缺少什么就去晚上找什么jar包即可,尽可能用maven包导入

jQuery 分页插件(jQuery.pagination.js)ajax 实现分页

首先需要引入jQuery 再次需要引入 <script src="jquery/jquery.pagination.js"></script> 同时也要引入 <link rel="stylesheet" href="css/pagination.css"> 引入之后,分页基本样子 下面看分页配置参数 var defaults = { totalData: 0, //数据总条数 showData: 0, //每页

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

严重: Servlet.service() for servlet [springmvc] in context with path [/mavenprj] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException:

MyBatis 分页插件 PageHelper(转)

如何使用分页插件 . 1. 引入分页插件 2. 配置拦截器插件 3. 如何在代码中使用 4. MyBatis 和 Spring 集成示例 5. Spring Boot 待定 使用方法 1. 引入分页插件 引入分页插件有下面2种方式,推荐使用 Maven 方式. 1). 引入 Jar 包 你可以从下面的地址中下载最新版本的 jar 包 https://oss.sonatype.org/content/repositories/releases/com/github/pagehelper/pageh

Mybatis分页插件PageHelper简单使用

1. 引入分页插件 引入分页插件有下面2种方式,推荐使用 Maven 方式. 1). 引入 Jar 包 你可以从下面的地址中下载最新版本的 jar 包 https://oss.sonatype.org/content/repositories/releases/com/github/pagehelper/pagehelper/ http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/ 由于使用了sql 解析工具,你还需要下载

Jquery 分页插件 Jquery Pagination

Jquery 分页插件 Jquery Pagination 分页插件来说,我觉得适用就行,尽量简单然后能够根据不同的应用场景能够换肤.展现形式等. 对于初学者想写分页插件的同学,也可以看下源码,代码也挺简单明了的,也助于自己写个小插件. 不过我比较懒,一般直接拿着各种插件用,想偷懒的同学们也可以用用看: Jquery Pagination 大家可以去这里查看下演示:http://www.xuetub.com/plugin/jquery/221 使用方法也比较简单: <div id="pag

MySql通用分页存储过程

MySql通用分页存储过程 1MySql通用分页存储过程 2 3过程参数 4p_cloumns varchar(500),p_tables varchar(100),p_where varchar(4000),p_order varchar(100),p_pageindex int,p_pagesize int,out p_recordcount int,out p_pagecount int 5 6$$:begin 7 declare v_sqlcounts varchar(4000); 8

17、Semantic-UI之分页插件

??在很多的前端开发框架中都会有提供分页插件,但是分页插件的使用如果手动编写还是比较复杂的.使用Semantic-UI中的分页插件更加简单方便.分页插件的使用必须要和后台结合. 示例:定义分页插件 <div class="ui pagination menu"> <div class="active item">1</div> <div class="item">2</div> <

spring-boot | 整合通用Mabatis 分页插件PageHelper

Mybatis通用Mapper介绍 Mybatis 通用 Mapper极其方便的使用 Mybatis 单表的增删改查,支持单表操作,不支持通用的多表联合查询优点:通用 Mapper 可以极大的方便开发人员.为了让您更方便的了解通用 Mapper,下面贴一段代码来看实际效果.通用Mapper通用 Mapper 可以缓存,全部针对单表操作,每个实体类都需要继承通用 Mapper 接口来获得通用方法. 实际开发过程中,我们对数据库单表的操作情况有很多,有了通用mapper插件, 我们实现相对应的接口,