转载请注明出处: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>条记录,当前显示第&nbsp;<i
class="blue">${pagehelper.pageNum}/${pagehelper.pages}</i>&nbsp;页
</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实现分页的更多相关文章

  1. SpringBoot+Mybatis+PageHelper实现分页

    SpringBoot+Mybatis+PageHelper实现分页 mybatis自己没有分页功能,我们可以通过PageHelper工具来实现分页,非常简单方便 第一步:添加依赖 <depend ...

  2. Python分页转Mybatis pagehelper格式分页

    最近工作里遇到一个需求要把之前用Java写的一个http接口替换成用Python写的,出参是带了mybatis pageHelper中PageInfo信息的一个JSON串,而Python这边分页不会涉 ...

  3. SpringBoot+Mybatis+PageHelper简化分页实现

    前言 经过一段时间的测试和修改PageHelper插件逐渐走到了让我觉得靠谱的时候,它功能的就是简化分页的实现,让分页不需要麻烦的多写很多重复的代码. 已经加入我的github模版中:https:// ...

  4. spring-boot + mybatis +pagehelper 使用分页

    转自:https://segmentfault.com/a/1190000015668715?utm_medium=referral&utm_source=tuicool 最近自己搭建一个sp ...

  5. SpringBoot 整合Mybatis + PageHelper 实现分页

    前言: 现在公司大多数都实现了前后端分离,前端使用Vue.React.AngularJS 等框架,不用完全依赖后端.但是如果对于比较小型的项目,没必要前后端分离,而SpringBoot也基本抛弃了Js ...

  6. Mybatis下collections使用pageHelper进行分页

    pageHelper在对mybatis一对多分页时造成查询总页数结果不对的情况. 可以做出如下修改: service层: public CommonResult worksList(String us ...

  7. SpringBoot集成Mybatis并具有分页功能PageHelper

    SpringBoot集成Mybatis并具有分页功能PageHelper   环境:IDEA编译工具   第一步:生成测试的数据库表和数据   SET FOREIGN_KEY_CHECKS=0;   ...

  8. mybatis pagehelper 分页 失效

    pagehelper 不分页几种情况的解决方法 - web洋仔 - CSDN博客https://blog.csdn.net/csdn___lyy/article/details/77160488 分页 ...

  9. 后端分页神器,mybatis pagehelper 在SSM与springboot项目中的使用

    mybatis pagehelper想必大家都耳熟能详了,是java后端用于做分页查询时一款非常好用的分页插件,同时也被人们称为mybatis三剑客之一,下面 就给大家讲讲如何在SSM项目和sprin ...

随机推荐

  1. 【Windows 10 应用开发】使用快捷访问键

    UWP 虽然主要面向触控操作,但 Windows 设备是万能工具,不用手指不用笔的时候,也可能会接上键盘耍耍.因此,给应用界面上的一些元素弄个快捷访问键也挺不错的.为了使用 Windows 上的各类应 ...

  2. 浏览器未安装flash插件,js判断直接去官网安装

    近期做了个活动页,里面根据需求插入了阿里云的视频,常见的浏览器都支持包括低版本的. 由于浏览器的更新换代很多版本放弃了flash的插件安装,火狐就是其中之一. 未安装flash的浏览器如果打开这个链接 ...

  3. 13.ThreadPoolExecutor线程池之submit方法

    jdk1.7.0_79  在上一篇<ThreadPoolExecutor线程池原理及其execute方法>中提到了线程池ThreadPoolExecutor的原理以及它的execute方法 ...

  4. Bash shell执行命令的优先级

    1.别名2.关键字:if.function.while .until等3.函数4.内置命令5.可执行程序或脚本 别关函内可 =-=-=-=-=Powered by Blogilo

  5. 多个项目MyEclipse中启动出现OutOfMemoryError: PermGen space如何解决

    在MyEclipse中启动程序运行,报错java.lang.OutOfMemoryError: PermGen space应该怎么办?这是eclipse 内存不够的原因. PermGen space的 ...

  6. WCF入门的了解准备工作

    了解WCF, 及WCF入门需要掌握哪里基本概念? 1.准备工作 >1.1 . XML >1.2 . Web Service >1.3 . 远程处理 (RPC) >1.4.  消 ...

  7. 用ingress的方式部署jenkins,启动后提示没有下载插件,未解决

    [root@node2 .docker]# docker logs 5c3dd117a10dRunning from: /usr/share/jenkins/jenkins.warwebroot: E ...

  8. nodejs+websocket制作聊天室视频教程

    本套教程主要讲解了node平台的安装,node初级知识.node 服务器端程序响应http请求,通过npm安装第三方包,websocket即时通讯.聊天页面界面制作.拖动原理.拖动效果.遮罩效果.定位 ...

  9. solr 搜索引擎查询

    搜索引擎查询的时候://对于这些filterQuery的字段,必须是indexed="true",如果之前有这个字段后来改这个indexed属性,则需要重新建立索引,否则搜索不到S ...

  10. php5.6在yum下安装gd库

    yum install php-gd --enablerepo=remi,remi-php56 php.ini配置文件中增加 extension=gd.so 重启web服务器即可