PageHelper实现分页

简介: PageHelper实现分页

PageHelper

PageHelper是国内非常优秀的一款开源的mybatis分页插件,它支持基本主流与常用的数据库,例如mysql、

oracle、mariaDB、DB2、SQLite、Hsqldb等。

本项目在 github 的项目地址:

PageHelper使用

本项目在 gitosc 的项目地址:

PageHelper使用


PageHelper使用


1.在父项目的 pom.xml 中添加如下依赖:


<dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.2</version>
    </dependency>

2.在 Spring 配置文件中配置拦截器插件,也就是在applicationContext.xml中添加插件


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!-- 传入PageHelper的插件 -->
    <property name="plugins">
        <array>
            <!-- 传入插件的对象 -->
            <bean class="com.github.pagehelper.PageInterceptor">
                <property name="properties">
                    <props>
                        <prop key="helperDialect">mysql</prop>
                        <prop key="reasonable">true</prop>
                    </props>
                </property>
            </bean>
        </array>
    </property>
</bean>

注意添加代码的位置


3.service的实现类里面真正执行查询代码的上一行编写PageHelper来实现分页,中间有多余的代码则不能实现分页效果


public List<Orders> findAllByPage(int page, int size) throws Exception {
        //参数pageNum 是页码值   参数pageSize 代表是每页显示条数
        PageHelper.startPage(page, size);
        return ordersDao.findAll();
    }

步骤图如下:


4.完整的分页 前面的再加上一个OrdersController类控制器


package com.controller;
import com.domain.Orders;
import com.github.pagehelper.PageInfo;
import com.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
//控制器
@Controller
@RequestMapping("/orders")
public class OrdersController {
//    引入serviced
    @Autowired
    private IOrdersService ordersService;
    //分页展示
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") int page, @RequestParam(name = "size", required = true, defaultValue = "4") int size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Orders> ordersList = ordersService.findAllByPage(page, size);
        //PageInfo就是一个分页Bean
        PageInfo pageInfo = new PageInfo(ordersList);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("orders-page-list");
        return mv;
    }
}

orders-page-list.jsp页面,具体就是显示PageInfo里面的值来实现下面的功能



<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<!-- 页面meta -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>数据 - AdminLTE2定制版</title>
<meta name="description" content="AdminLTE2定制版">
<meta name="keywords" content="AdminLTE2定制版">
<!-- Tell the browser to be responsive to screen width -->
<meta
  content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"
  name="viewport">
<!-- Bootstrap 3.3.6 -->
<!-- Font Awesome -->
<!-- Ionicons -->
<!-- iCheck -->
<!-- Morris chart -->
<!-- jvectormap -->
<!-- Date Picker -->
<!-- Daterange picker -->
<!-- Bootstrap time Picker -->
<!--<link rel="stylesheet" href="${pageContext.request.contextPath}/${pageContext.request.contextPath}/${pageContext.request.contextPath}/plugins/timepicker/bootstrap-timepicker.min.css">-->
<!-- bootstrap wysihtml5 - text editor -->
<!--数据表格-->
<!-- 表格树 -->
<!-- select2 -->
<!-- Bootstrap Color Picker -->
<!-- bootstrap wysihtml5 - text editor -->
<!--bootstrap-markdown-->
<!-- Theme style -->
<!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
<!-- Ion Slider -->
<!-- ion slider Nice -->
<!-- bootstrap slider -->
<!-- bootstrap-datetimepicker -->
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->
<!-- jQuery 2.2.3 -->
<!-- jQuery UI 1.11.4 -->
<!-- Resolve conflict in jQuery UI tooltip with Bootstrap tooltip -->
<!-- Bootstrap 3.3.6 -->
<!-- Morris.js charts -->
<!-- Sparkline -->
<!-- jvectormap -->
<!-- jQuery Knob Chart -->
<!-- daterangepicker -->
<!-- datepicker -->
<!-- Bootstrap WYSIHTML5 -->
<!-- Slimscroll -->
<!-- FastClick -->
<!-- iCheck -->
<!-- AdminLTE App -->
<!-- 表格树 -->
<!-- select2 -->
<!-- bootstrap color picker -->
<!-- bootstrap time picker -->
<!--<script src="${pageContext.request.contextPath}/${pageContext.request.contextPath}/${pageContext.request.contextPath}/plugins/timepicker/bootstrap-timepicker.min.js"></script>-->
<!-- Bootstrap WYSIHTML5 -->
<!--bootstrap-markdown-->
<!-- CK Editor -->
<!-- InputMask -->
<!-- DataTables -->
<!-- ChartJS 1.0.1 -->
<!-- FLOT CHARTS -->
<!-- FLOT RESIZE PLUGIN - allows the chart to redraw when the window is resized -->
<!-- FLOT PIE PLUGIN - also used to draw donut charts -->
<!-- FLOT CATEGORIES PLUGIN - Used to draw bar charts -->
<!-- jQuery Knob -->
<!-- Sparkline -->
<!-- Morris.js charts -->
<!-- Ion Slider -->
<!-- Bootstrap slider -->
<!-- bootstrap-datetimepicker -->
<!-- 页面meta /-->
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/ionicons/css/ionicons.min.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/iCheck/square/blue.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/morris/morris.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/jvectormap/jquery-jvectormap-1.2.2.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/datepicker/datepicker3.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/daterangepicker/daterangepicker.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/datatables/dataTables.bootstrap.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/treeTable/jquery.treetable.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/treeTable/jquery.treetable.theme.default.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/select2/select2.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/colorpicker/bootstrap-colorpicker.min.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/bootstrap-markdown/css/bootstrap-markdown.min.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/adminLTE/css/AdminLTE.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/adminLTE/css/skins/_all-skins.min.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/css/style.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/ionslider/ion.rangeSlider.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/ionslider/ion.rangeSlider.skinNice.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/bootstrap-slider/slider.css">
<link rel="stylesheet"
  href="${pageContext.request.contextPath}/plugins/bootstrap-datetimepicker/bootstrap-datetimepicker.css">
</head>
<body class="hold-transition skin-purple sidebar-mini">
  <div class="wrapper">
    <!-- 页面头部 -->
    <jsp:include page="header.jsp"></jsp:include>
    <!-- 页面头部 /-->
    <!-- 导航侧栏 -->
    <jsp:include page="aside.jsp"></jsp:include>
    <!-- 导航侧栏 /-->
    <!-- 内容区域 -->
    <!-- @@master = admin-layout.html-->
    <!-- @@block = content -->
    <div class="content-wrapper">
      <!-- 内容头部 -->
      <section class="content-header">
        <h1>
          数据管理 <small>数据列表</small>
        </h1>
        <ol class="breadcrumb">
          <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
          <li><a href="#">数据管理</a></li>
          <li class="active">数据列表</li>
        </ol>
      </section>
      <!-- 内容头部 /-->
      <!-- 正文区域 -->
      <section class="content">
        <!-- .box-body -->
        <div class="box box-primary">
          <div class="box-header with-border">
            <h3 class="box-title">列表</h3>
          </div>
          <div class="box-body">
            <!-- 数据表格 -->
            <div class="table-box">
              <!--工具栏-->
              <div class="pull-left">
                <div class="form-group form-inline">
                  <div class="btn-group">
                    <button type="button" class="btn btn-default" title="新建"
                      onclick="location.href='${pageContext.request.contextPath}/pages/product-add.jsp'">
                      <i class="fa fa-file-o"></i> 新建
                    </button>
                    <button type="button" class="btn btn-default" title="删除">
                      <i class="fa fa-trash-o"></i> 删除
                    </button>
                    <button type="button" class="btn btn-default" title="开启">
                      <i class="fa fa-check"></i> 开启
                    </button>
                    <button type="button" class="btn btn-default" title="屏蔽">
                      <i class="fa fa-ban"></i> 屏蔽
                    </button>
                    <button type="button" class="btn btn-default" title="刷新">
                      <i class="fa fa-refresh"></i> 刷新
                    </button>
                  </div>
                </div>
              </div>
              <div class="box-tools pull-right">
                <div class="has-feedback">
                  <input type="text" class="form-control input-sm"
                    placeholder="搜索"> <span
                    class="glyphicon glyphicon-search form-control-feedback"></span>
                </div>
              </div>
              <!--工具栏/-->
              <!--数据列表-->
              <table id="dataList"
                class="table table-bordered table-striped table-hover dataTable">
                <thead>
                  <tr>
                    <th class="" style="padding-right: 0px;"><input
                      id="selall" type="checkbox" class="icheckbox_square-blue">
                    </th>
                    <th class="sorting_asc">ID</th>
                    <th class="sorting_desc">订单编号</th>
                    <th class="sorting_asc sorting_asc_disabled">产品名称</th>
                    <th class="sorting_desc sorting_desc_disabled">金额</th>
                    <th class="sorting">下单时间</th>
                    <th class="text-center sorting">订单状态</th>
                    <th class="text-center">操作</th>
                  </tr>
                </thead>
                <tbody>
                  <c:forEach items="${pageInfo.list}" var="orders">
                    <tr>
                      <td><input name="ids" type="checkbox"></td>
                      <td>${orders.id }</td>
                      <td>${orders.orderNum }</td>
                      <td>${orders.product.productName }</td>
                      <td>${orders.product.productPrice }</td>
                      <td>${orders.orderTimeStr }</td>
                      <td class="text-center">${orders.orderStatusStr }</td>
                      <td class="text-center">
                        <button type="button" class="btn bg-olive btn-xs">订单</button>
                        <button type="button" class="btn bg-olive btn-xs" onclick="location.href='${pageContext.request.contextPath}/orders/findById.do?id=${orders.id}'">详情</button>
                        <button type="button" class="btn bg-olive btn-xs">编辑</button>
                      </td>
                    </tr>
                  </c:forEach>
                </tbody>
                <!--
                            <tfoot>
                            <tr>
                            <th>Rendering engine</th>
                            <th>Browser</th>
                            <th>Platform(s)</th>
                            <th>Engine version</th>
                            <th>CSS grade</th>
                            </tr>
                            </tfoot>-->
              </table>
              <!--数据列表/-->
              <!--工具栏-->
              <div class="pull-left">
                <div class="form-group form-inline">
                  <div class="btn-group">
                    <button type="button" class="btn btn-default" title="新建">
                      <i class="fa fa-file-o"></i> 新建
                    </button>
                    <button type="button" class="btn btn-default" title="删除">
                      <i class="fa fa-trash-o"></i> 删除
                    </button>
                    <button type="button" class="btn btn-default" title="开启">
                      <i class="fa fa-check"></i> 开启
                    </button>
                    <button type="button" class="btn btn-default" title="屏蔽">
                      <i class="fa fa-ban"></i> 屏蔽
                    </button>
                    <button type="button" class="btn btn-default" title="刷新">
                      <i class="fa fa-refresh"></i> 刷新
                    </button>
                  </div>
                </div>
              </div>
              <div class="box-tools pull-right">
                <div class="has-feedback">
                  <input type="text" class="form-control input-sm"
                    placeholder="搜索"> <span
                    class="glyphicon glyphicon-search form-control-feedback"></span>
                </div>
              </div>
              <!--工具栏/-->
            </div>
            <!-- 数据表格 /-->
          </div>
          <!-- /.box-body -->
          <!-- .box-footer-->
                <div class="box-footer">
                    <div class="pull-left">
                        <div class="form-group form-inline">
                            总共2 页,共14 条数据。 每页
                            <select class="form-control" id="changePageSize" onchange="changePageSize()">
                <option>4</option>
                <option>1</option>
                                <option>2</option>
                                <option>3</option>
                <%--<option>4</option>--%>
                                <option>5</option>
                            </select> 条
                        </div>
                    </div>
                    <div class="box-tools pull-right">
                        <ul class="pagination">
                            <li>
                                <a href="${pageContext.request.contextPath}/orders/findAll.do?page=1&size=${pageInfo.pageSize}" aria-label="Previous">首页</a>
                            </li>
                            <li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pageNum-1}&size=${pageInfo.pageSize}">上一页</a></li>
                           <c:forEach begin="1" end="${pageInfo.pages}" var="pageNum">
                 <li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageNum}&size=${pageInfo.pageSize}">${pageNum}</a></li>
               </c:forEach>
                            <li><a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pageNum+1}&size=${pageInfo.pageSize}">下一页</a></li>
                            <li>
                                <a href="${pageContext.request.contextPath}/orders/findAll.do?page=${pageInfo.pages}&size=${pageInfo.pageSize}" aria-label="Next">尾页</a>
                            </li>
                        </ul>
                    </div>
                </div>
                <!-- /.box-footer-->
        </div>
      </section>
      <!-- 正文区域 /-->
    </div>
    <!-- @@close -->
    <!-- 内容区域 /-->
    <!-- 底部导航 -->
    <footer class="main-footer">
      <div class="pull-right hidden-xs">
        <b>Version</b> 1.0.8
      </div>
      <strong>Copyright &copy; 2014-2017 <a
        href="http://www.itcast.cn">研究院研发部</a>.
      </strong> All rights reserved.
    </footer>
    <!-- 底部导航 /-->
  </div>
  <script
    src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/jQueryUI/jquery-ui.min.js"></script>
  <script>
    $.widget.bridge('uibutton', $.ui.button);
  </script>
  <script
    src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/raphael/raphael-min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/morris/morris.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/sparkline/jquery.sparkline.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/jvectormap/jquery-jvectormap-1.2.2.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/jvectormap/jquery-jvectormap-world-mill-en.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/knob/jquery.knob.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/daterangepicker/moment.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/daterangepicker/daterangepicker.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/daterangepicker/daterangepicker.zh-CN.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/datepicker/bootstrap-datepicker.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/datepicker/locales/bootstrap-datepicker.zh-CN.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/slimScroll/jquery.slimscroll.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/fastclick/fastclick.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/iCheck/icheck.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/adminLTE/js/app.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/treeTable/jquery.treetable.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/select2/select2.full.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/colorpicker/bootstrap-colorpicker.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/bootstrap-wysihtml5/bootstrap-wysihtml5.zh-CN.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/bootstrap-markdown/js/bootstrap-markdown.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/bootstrap-markdown/locale/bootstrap-markdown.zh.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/bootstrap-markdown/js/markdown.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/bootstrap-markdown/js/to-markdown.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/ckeditor/ckeditor.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/input-mask/jquery.inputmask.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/input-mask/jquery.inputmask.date.extensions.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/input-mask/jquery.inputmask.extensions.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/datatables/jquery.dataTables.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/datatables/dataTables.bootstrap.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/chartjs/Chart.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/flot/jquery.flot.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/flot/jquery.flot.resize.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/flot/jquery.flot.pie.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/flot/jquery.flot.categories.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/ionslider/ion.rangeSlider.min.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/bootstrap-slider/bootstrap-slider.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/bootstrap-datetimepicker/bootstrap-datetimepicker.js"></script>
  <script
    src="${pageContext.request.contextPath}/plugins/bootstrap-datetimepicker/locales/bootstrap-datetimepicker.zh-CN.js"></script>
  <script>
    function changePageSize() {
      //获取下拉框的值
      var pageSize = $("#changePageSize").val();
      //向服务器发送请求,改变没页显示条数
      location.href = "${pageContext.request.contextPath}/orders/findAll.do?page=1&size="
          + pageSize;
    }
    $(document).ready(function() {
      // 选择框
      $(".select2").select2();
      // WYSIHTML5编辑器
      $(".textarea").wysihtml5({
        locale : 'zh-CN'
      });
    });
    // 设置激活菜单
    function setSidebarActive(tagUri) {
      var liObj = $("#" + tagUri);
      if (liObj.length > 0) {
        liObj.parent().parent().addClass("active");
        liObj.addClass("active");
      }
    }
    $(document).ready(function() {
      // 激活导航位置
      setSidebarActive("admin-datalist");
      // 列表按钮 
      $("#dataList td input[type='checkbox']").iCheck({
        checkboxClass : 'icheckbox_square-blue',
        increaseArea : '20%'
      });
      // 全选操作 
      $("#selall").click(function() {
        var clicks = $(this).is(':checked');
        if (!clicks) {
          $("#dataList td input[type='checkbox']").iCheck("uncheck");
        } else {
          $("#dataList td input[type='checkbox']").iCheck("check");
        }
        $(this).data("clicks", !clicks);
      });
    });
  </script>
</body>
</html>


相关文章
|
2月前
|
SQL Java 关系型数据库
PageHelper分页插件最新源码解读及使用
PageHelper分页插件最新源码解读及使用
|
2月前
pagehelper分页插件
pagehelper分页插件
43 0
|
8月前
|
SQL Java 数据库连接
Mybatis的两种分页方式:RowBounds和PageHelper
Mybatis的两种分页方式:RowBounds和PageHelper
|
10月前
|
SQL Java 数据库连接
PageHelper分页插件的使用
PageHelper分页插件的使用
173 0
|
11月前
|
前端开发 Java 关系型数据库
详解Mybatis之分页插件【PageHelper】
详解Mybatis之分页插件【PageHelper】
|
12月前
|
SQL 前端开发 Java
java后端pageHelper分页实现方法
java后端pageHelper分页实现方法
|
SQL 前端开发 Java
7、PageHelper分页介绍
查询要实现分页、会存在多表关联查询,所以建议使用mybatis实现我的课程查询。 单表查询直接用Spring Data JPA,多表关联查询使用Mybatis有利于后期优化处理。
217 0
7、PageHelper分页介绍
|
XML SQL Java
Mybatis分页插件PageHelper的学习与使用
Mybatis分页插件PageHelper的学习与使用
Mybatis分页插件PageHelper的学习与使用
|
Java 数据库连接 mybatis
Mybatis使用系列-2-PageHelper分页
本篇记录如何使用PageHelper进行分页
186 0
Mybatis使用系列-2-PageHelper分页