在做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. 修改字段名change

    ALTER TABLE tc_activity_miaosha_item CHANGE `batch_id` `movie_batch_id` INT () NOT NULL DEFAULT ' CO ...

  2. EXTI—外部中断事件控制器

    外部中断概述 STM32F4的每个IO都可以作为外部中断输入. STM32F4的中断控制器支持22个外部中断/事件请求: 从上面可以看出,STM32F4供IO使用的中断线只有16个,但是STM32F4 ...

  3. Android 修改 TextView 的全局默认颜色。

    如果你的应用中大多数TextView的颜色是红色, 或者其他颜色, 你是为每一个TextView都设置一次颜色, 还是有其他更好的办法, 这里教你怎么修改TextView的默认颜色. 当然我们Text ...

  4. 编写高质量代码改善C#程序的157个建议——建议132:考虑用类名作为属性名

    建议132:考虑用类名作为属性名 一般来说,若果属性对应一个类型,应该直接用类型名命名属性名.如下: class Person { public Company Company { get; set; ...

  5. Java NIO学习-预备知识

    java NIO加入了Channels.Buffers.Selector.通过他们可以为java的io添加非阻塞IO. 一.对于经典java IO库 1.除了Buffered开头的类,其他均没有加缓冲 ...

  6. Javascript原型与对象等知识

    声明式函数定义: function add(m,n) { alert(m+n); } 这种方式等同于构造一个Function类的实例的方式: var add = new Function(" ...

  7. android 中 dp和px转换

    DisplayUtils代码: public class DisplayUtil { public static int px2dip(Context context, float px) { flo ...

  8. 介绍自己以及github注册流程

    我叫何季生,来自网络工程141,学号是1413042027,我喜欢看一些动漫和游戏,对于编程并不是很厉害希望今年能够有所突破. github注册流程:在刚开始注册github时,我用的是qq浏览器,却 ...

  9. WPF判断当前窗体是否为模态

    WPF判断当前窗体是否为模态   1.使用System.Windows.Interop.ComponentDispatcher.IsThreadModal来判断 参照:https://social.m ...

  10. Entity Framework 高性能 泛型缓存+动态Lambda

    前言:自学CSharp挺长时间的了,这是我第一编博客,跟大家分享一下.如有不足地方请多多包涵,也欢迎大家提出更好的意见,下面开始进入正题. 一.泛型缓存 1.概念:1.泛型(泛型也是一种推断类型,从而 ...