在做SSM整合的时候,遇到一个小问题,在我使用pageHelper的时候,分页的效果总是无法正确显示,卡了我几个小时,现在来说一下我的问题。

 1.首先导入pageHelper的包:

<!--引入pageHelper分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.</version>
</dependency>

2.在mybatis-config.xml配置:

<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--分页参数合理化 -->
<property name="reasonable" value="true"/> </plugin>
</plugins>

3.接下来开始测试:
测试代码:

@Test
public void getAll(){
PageHelper.startPage(,);
List<Employee> list2 = employeeService.getAll();
PageInfo<Employee> pi = new PageInfo<>(list2); System.out.println("当前页码:"+pi.getPageNum());
System.out.println("总页码:"+pi.getPages());
System.out.println("总记录数:"+pi.getTotal());
}

测试结果:

发现结果并不是我所需要的,结果并没有分页,于是我在mapper层继续测试:
测试代码:

@Test
public void getAll(){
PageHelper.startPage(,);
List<Employee> list2 = employeeMapper.selectByExampleWithDept(null);
PageInfo<Employee> pi = new PageInfo<>(list2);
System.out.println("当前页码:"+pi.getPageNum());
System.out.println("总页码:"+pi.getPages());
System.out.println("总记录数:"+pi.getTotal());
}

测试结果:

结果正是我所需要的,既然mapper层没错,那么程序的问题就是service层出错了,service层代码如下:

public List<Employee> getAll() {
//部门和员工一起查出来
employeeMapper.selectByExampleWithDept(null);
return employeeMapper.selectByExampleWithDept(null);
}

我们可以发现,查询代码我查了两次,这就是导致我无法分页成功,于是我把 employeeMapper.selectByExampleWithDept(null)注释掉,再次查询就成功了

public List<Employee> getAll() {
//部门和员工一起查出来
// employeeMapper.selectByExampleWithDept(null);
return employeeMapper.selectByExampleWithDept(null);
}

4.总结一下使用pageHelper的注意点:

  • PageHelper.startPage(1,5);要放在查询语句的前面
  • PageHelper.startPage(1,10);只对该语句以后的第一个查询语句得到的数据进行分页,如果有两条查询语句,只对第一条查询语句生效,也就是 employeeMapper.selectByExampleWithDept(null);这条有效,而 employeeMapper.selectByExampleWithDept(null);没有生效,虽然查询出了所有数据,但是分页无效

再次做一个测试:

@Test
public void getAll(){
PageHelper.startPage(,);
employeeMapper.selectByExampleWithDept(null);
List<Employee> list2 = employeeMapper.selectByExampleWithDept(null);
PageInfo<Employee> pi = new PageInfo<>(list2); System.out.println("当前页码:"+pi.getPageNum());
System.out.println("总页码:"+pi.getPages());
System.out.println("总记录数:"+pi.getTotal());
}

结果:

查询结果没有分页,也就是PageHelper.startPage(1,5); 对 employeeMapper.selectByExampleWithDept(null);生效,
而List<Employee> list2 = employeeMapper.selectByExampleWithDept(null); 没有生效,当把 employeeMapper.selectByExampleWithDept(null); 注释后,分页又成功了

使用pageHelper遇到的问题的更多相关文章

  1. PageHelper简单实用

    mybatis-config.xml配置如下: <!-- 分页插件 --> <plugins> <plugin interceptor="com.github. ...

  2. springboot使用之二:整合mybatis(xml方式)并添加PageHelper插件

    整合mybatis实在前面项目的基础上进行的,前面项目具体整合请参照springboot使用之一. 一.整合mybatis 整合mybatis的时候可以从mybatis官网下载mybatis官网整合的 ...

  3. 理解 Mybatis的分页插件 PageHelper

    Mybatis + SpringMVC + Maven实现分页查询 (推荐采用的插件是PageHelper) 先看一下之前的这篇博客,这里推荐了 Mybatis 的分页方法. 按照上面的方法设置后,确 ...

  4. Spring + Mybatis 使用 PageHelper 插件分页

    原文:http://www.cnblogs.com/yucongblog/p/5330886.html 先增加maven依赖: <dependency> <groupId>co ...

  5. Mybatis的分页插件PageHelper

    Mybatis的分页插件PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper  文档地址:http://git.oschina. ...

  6. mybatis分页插件PageHelper的使用(转)

    Mybatis 的分页插件PageHelper-4.1.1的使用 Mybatis 的分页插件 PageHelper 项目地址:http://git.oschina.net/free/Mybatis_P ...

  7. MyBatis学习总结_17_Mybatis分页插件PageHelper

    如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件. 分页插件支持任何复杂的单表.多表分页,部分特殊情况请看重要提示. 想要使用分页插件?请看如何使用分页插件. 物理分页 该 ...

  8. Spring集成PageHelper的简单用法

    1.Maven依赖,注意使用PageHelper时的版本必须与Mybatis版本对应 <!-- 添加Mybatis依赖 --> <dependency> <groupId ...

  9. Mybatis分页插件PageHelper正确的用法(网上有2篇不够科学的文章)

    今天下午在Mybatis项目中.实现分页.由于我是后加入项目中的,Leader用的是PageHelper这个组件.可是我在实际使用的过程中遇到了2个大问题. 1.p=2#comments" ...

  10. maven+springmvc+easyui+fastjson+pagehelper

    1.maven配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

随机推荐

  1. code2800 送外卖

    首先,对图进行一次Floyd(g[][]是图) 1.dfs:(u是当前在的节点,d是已经走的路程) void dfs(int u,int d){ if(d>=ans)return; bool f ...

  2. 使用shell命令操作数据库

    使用mysql的-e参数可以执行各种sql的(创建,删除,增,删,改.查)等各种操作 用法 mysql  -uxxx    –pxxx   -e  "mysql 命令" 当然还可以 ...

  3. JS对象转URL参数(原生JS和jQuery两种方式)

    转自:点击打开链接 现在的js框架将ajax请求封装得非常简单,例如下面: $.ajax({ type: "POST", url: "some.php", da ...

  4. HaXe以及OpenFL部署

    HaXe以及OpenFL部署 Haxe是一种跨平台的编程语言,本文并未HAXE的教程,只是针对OPENFL以及HAXE的部署教程.HAXE的语法非常类似AS3,由于国内部署HAXE艰难,经常下载到一半 ...

  5. kalilinux-漏洞评估

    Nessus\OpenVAS http://www.tenable.com/products/nessus/select-your-operating-system http://www.nessus ...

  6. spark安装配置(scala不是必须的,基于java虚拟机,因此scala可以不配,但是开发需要可以配)

    下载 http://spark.apache.org/downloads.html 下载2.3.1 https://blog.csdn.net/qq_15349687/article/details/ ...

  7. 学习python4

    文件系统实现文件的增删改查 UnicodeDecodeError: 'gbk' codec can't decode byte 0x9a in position 8: illegal multibyt ...

  8. Javascript与数据结构系列(二)——队列的实现

    队列实现 使用数组来实现队列看起来顺理成章.JavaScript 中的数组具有其他编程语言中没有的优点, 数组的 push() 方法可以在数组末尾加入元素,shift() 方法则可删除数组的第一个元素 ...

  9. 关于dm-file-uploader(dmUploader)上传时传参

    官网:https://github.com/danielm/uploaderDemo Online https://danielmg.org/demo/java-script/uploader/bas ...

  10. 解决:The APR based Apache Tomcat Native library which allows optimal performance in production...

    tomcat日志apr报错引发的基于Tomcat Native加速Tomcat性能 tomact服务启动报错日志如下:息: The APR based Apache Tomcat Native lib ...