MyBatis+PageHelper实现分页
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7256105.html
前面讲到Spring+SpringMVC+MyBatis深入学习及搭建(十七)——SpringMVC拦截器
本文通过MyBatis+PageHelper实现列表分页,先上效果图:

注意:MyBaits最低版本不能低于3.3。
PageHelper插件及依赖jar包:jsqlparser-0.9.5.jar pagehelper-4.2.1.jar
sqlMapConfig.xml中,配置PageHelper插件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 全局setting配置,根据需要添加 -->
<settings>
<setting name="logImpl" value="LOG4J" />
</settings> <!-- 配置别名 -->
<typeAliases>
<package name="pojo对象所在包名"/>
</typeAliases> <!-- 插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 4.0.0以后版本可以不设置该参数 -->
<property name="diaect" value="mysql"/>
<!--
该参数默认为false
设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用
和startPage中的pageNum效果一样
-->
<property name="offsetAsPageNum" value="true"/>
<!--
该参数默认为false
设置为true时,使用RowBounds分页会进行count查询
-->
<property name="rowBoundsWithCount" value="true"/>
<!--
设置为true时,如果pageSize=0或者RowBounds.limit=0就会查询出全部的结果
(相当于没有执行分页查询,只是返回结果仍然是Page类型)
-->
<property name="pageSizeZero" value="true"/>
<!--
3.3.0版本可用-分页参数合理化,默认false禁用
启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页
禁用合理化时,如果pageNum<1或pages会返回空数据
-->
<property name="reasonable" value="true"/>
<!--
3.5.0版本可用-为了支持startPage(Object params)方法
增加了一个'params'参数来配置参数映射,用于从Map或ServletRequest中取值
可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值
不理解该含义的前提下,不要随便复制该配置
-->
<property name="params" value="pageNum=start;pageSize=limit;"/>
<!-- 支持通过Mapper接口参数来传递分页参数 -->
<property name="supportMethodsArguments" value="true"/>
<!-- always重视返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->
<property name="returnPageInfo" value="check"/>
</plugin>
</plugins> </configuration>
后台代码:
//查询单个item
@RequestMapping(value={"/query"})
public String query(HttpSession session,Model model,@RequestParam(required=true)Integer id,@RequestParam(required=true)Integer pageNum) throws Exception{
MaindeviceCustom maindeviceCustom=maindeviceService.findById(id);
model.addAttribute("maindevice", maindeviceCustom);
int roleId=(int) session.getAttribute("roleId");
RoleFunction rfListAble=roleFunctionService.find(roleId, 27);
RoleFunction rfUpdateAble=roleFunctionService.find(roleId, 32);
if(rfListAble!=null){
model.addAttribute("listAble", 1);
int pageSize=10;
Page<?> page=PageHelper.startPage(pageNum, pageSize);
List<DeviceUserecordCustom> deviceUserecordCustomList=deviceUserecordService.itemRecords(id);
PageInfo<?> pagehelper=page.toPageInfo();
model.addAttribute("deviceUserecordCustomList", deviceUserecordCustomList);
model.addAttribute("pagehelper", pagehelper);
}else{
model.addAttribute("listAble", 0);
}
if(rfUpdateAble!=null){
model.addAttribute("updateAble", 1);
}else{
model.addAttribute("updateAble", 0);
}
return "devices/query_device";
}
前端代码:
<div class="row" id="deviceRecordList">
<div class="col-lg-12" style="height: 15px;top:700px" id="paging">
<div class="panel">
<div class="panel-heading bk-bg-primary">
<h6><i class="fa fa-table red"></i><span class="break"></span>使用记录列表</h6>
<div class="panel-actions">
<a href="table.html#" class="btn-setting"><i class="fa fa-rotate-right"></i></a>
<a href="table.html#" class="btn-minimize"><i class="fa fa-chevron-up"></i></a>
</div>
</div>
<div class="panel-body" style="height: 900px;">
<div class="table-responsive">
<table class="table table-striped table-bordered bootstrap-datatable datatable">
<thead>
<tr>
<th>序号</th>
<th>科目</th>
<th>收费类型</th>
<th>单价</th>
<th>数量</th>
<th>总金额</th>
<th>开始时间</th>
<th>结束时间</th>
<th>结束类型</th>
</tr>
</thead>
<tbody> <c:forEach items="${deviceUserecordCustomList }" var="deviceUserecordCustom" varStatus="status">
<tr>
<td>${status.index+1 }</td>
<td>${deviceUserecordCustom.courseName }</td>
<td>${deviceUserecordCustom.paymenttypeName }</td>
<td>
<span class="label label-warning">${deviceUserecordCustom.price }</span>
</td>
<td>${deviceUserecordCustom.usedamount }</td>
<td>${deviceUserecordCustom.monetary }</td>
<td>${deviceUserecordCustom.begintimeStr }</td>
<td>${deviceUserecordCustom.endtimeStr }</td>
<td>${deviceUserecordCustom.endtypeName }</td>
</tr>
</c:forEach> </tbody>
</table>
<!-- 分页 -->
<div class="message">
共<i class="blue">${pagehelper.total}</i>条记录,当前显示第 <i
class="blue">${pagehelper.pageNum}/${pagehelper.pages}</i> 页
</div>
<div style="text-align:center;">
<ul class="pagination">
<c:if test="${!pagehelper.isFirstPage}">
<li><a href="javascript:queryDeviceRecords(${pagehelper.firstPage});">首页</a></li>
<li><a href="javascript:queryDeviceRecords(${pagehelper.prePage});">上一页</a></li>
</c:if>
<c:forEach items="${pagehelper.navigatepageNums}" var="navigatepageNum"> <c:if test="${navigatepageNum==pagehelper.pageNum}">
<li class="active"><a href="javascript:queryAllDevices(${navigatepageNum});">${navigatepageNum}</a></li>
</c:if>
<c:if test="${navigatepageNum!=pagehelper.pageNum}">
<li><a href="javascript:queryDeviceRecords(${navigatepageNum});">${navigatepageNum}</a></li>
</c:if>
</c:forEach>
<c:if test="${!pagehelper.isLastPage}">
<li><a href="javascript:queryDeviceRecords(${pagehelper.nextPage});">下一页</a></li>
<li><a href="javascript:queryDeviceRecords(${pagehelper.lastPage});">最后一页</a></li>
</c:if>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
其中id="paging",为需要局部重新加载的代码块。
<script type="text/javascript">
function queryDeviceRecords(pageNum) {
var id=$("#deviceId").val();
$("#paging").load("<%=basePath %>/maindevice/query.html?id="+id+"&pageNum=" + pageNum);
}
</script>
如果此文对您有帮助,微信打赏我一下吧~

MyBatis+PageHelper实现分页的更多相关文章
- SpringBoot+Mybatis+PageHelper实现分页
SpringBoot+Mybatis+PageHelper实现分页 mybatis自己没有分页功能,我们可以通过PageHelper工具来实现分页,非常简单方便 第一步:添加依赖 <depend ...
- Python分页转Mybatis pagehelper格式分页
最近工作里遇到一个需求要把之前用Java写的一个http接口替换成用Python写的,出参是带了mybatis pageHelper中PageInfo信息的一个JSON串,而Python这边分页不会涉 ...
- SpringBoot+Mybatis+PageHelper简化分页实现
前言 经过一段时间的测试和修改PageHelper插件逐渐走到了让我觉得靠谱的时候,它功能的就是简化分页的实现,让分页不需要麻烦的多写很多重复的代码. 已经加入我的github模版中:https:// ...
- spring-boot + mybatis +pagehelper 使用分页
转自:https://segmentfault.com/a/1190000015668715?utm_medium=referral&utm_source=tuicool 最近自己搭建一个sp ...
- SpringBoot 整合Mybatis + PageHelper 实现分页
前言: 现在公司大多数都实现了前后端分离,前端使用Vue.React.AngularJS 等框架,不用完全依赖后端.但是如果对于比较小型的项目,没必要前后端分离,而SpringBoot也基本抛弃了Js ...
- Mybatis下collections使用pageHelper进行分页
pageHelper在对mybatis一对多分页时造成查询总页数结果不对的情况. 可以做出如下修改: service层: public CommonResult worksList(String us ...
- SpringBoot集成Mybatis并具有分页功能PageHelper
SpringBoot集成Mybatis并具有分页功能PageHelper 环境:IDEA编译工具 第一步:生成测试的数据库表和数据 SET FOREIGN_KEY_CHECKS=0; ...
- mybatis pagehelper 分页 失效
pagehelper 不分页几种情况的解决方法 - web洋仔 - CSDN博客https://blog.csdn.net/csdn___lyy/article/details/77160488 分页 ...
- 后端分页神器,mybatis pagehelper 在SSM与springboot项目中的使用
mybatis pagehelper想必大家都耳熟能详了,是java后端用于做分页查询时一款非常好用的分页插件,同时也被人们称为mybatis三剑客之一,下面 就给大家讲讲如何在SSM项目和sprin ...
随机推荐
- Cordova各个插件使用介绍系列(二)—$cordovaBarcodeScanner扫描二维码与生成二维码
详情链接地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/cordova-2-cordovabarcodescanner/ 这是 ...
- 源码安装LNMP环境
新装CentOS 6.7,安装默认服务版本basic server 安装顺序linux(忽略...)--> Nginx--> Mariadb--> PHP 为了不影响测试效果,首先关 ...
- js判断密码强度是否符合
/** 判断密码强度是否符合 */ function check_passwd_intensity(password) { value = $.trim(password); if( value.le ...
- 一起学习c++11——c++11中的新语法
c++11新语法1: auto关键字 c++11 添加的最有用的一个特性应该就是auto关键字. 不知道大家有没有写过这样的代码: std::map<std::string, std::vect ...
- iOS9新特性之常见关键字、泛型
#pragma mark -- nullable nullable:可以为空,只能修饰对象,不能修饰基本数据类型 // 方式一: @property (nonatomic, copy, nullabl ...
- rails 多态
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #f4f4f4; background-color: rgba( ...
- 排序算法总结及Java实现
1. 整体介绍 分类 排序大的分类可以分为两种,内排序和外排序.在排序过程中,全部记录存放在内存,则称为内排序,如果排序过程中需要使用外存,则称为外排序.主要需要理解的都是内排序算法: 内排序可以分为 ...
- 使用 Live555 搭建流媒体服务器
最近因为工作需要,需要搭建流媒体服务器,所以研究了一下,在此分享我的搭建过程. 搭建过程还是非常简单的! 搭建环境为Centos 7.2 64bit 一.安装gcc编译器 yum install gc ...
- MySQL常用基本命令
启动MySQL /etc/init.d/mysqld start 优雅的关闭数据库的方法 1:使用MySQLadmin mysqladmin -uroot -p123456 shutdown 2:使用 ...
- Jenkins+Git配置
Jenkins+Git配置 一.GitHub上配置 前提:Jenkins能正常打开 将本地文件上传到GitHub上:进入终端 cd Documents cd project git clone htt ...