使用pageHelper遇到的问题
在做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遇到的问题的更多相关文章
- PageHelper简单实用
mybatis-config.xml配置如下: <!-- 分页插件 --> <plugins> <plugin interceptor="com.github. ...
- springboot使用之二:整合mybatis(xml方式)并添加PageHelper插件
整合mybatis实在前面项目的基础上进行的,前面项目具体整合请参照springboot使用之一. 一.整合mybatis 整合mybatis的时候可以从mybatis官网下载mybatis官网整合的 ...
- 理解 Mybatis的分页插件 PageHelper
Mybatis + SpringMVC + Maven实现分页查询 (推荐采用的插件是PageHelper) 先看一下之前的这篇博客,这里推荐了 Mybatis 的分页方法. 按照上面的方法设置后,确 ...
- Spring + Mybatis 使用 PageHelper 插件分页
原文:http://www.cnblogs.com/yucongblog/p/5330886.html 先增加maven依赖: <dependency> <groupId>co ...
- Mybatis的分页插件PageHelper
Mybatis的分页插件PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper 文档地址:http://git.oschina. ...
- mybatis分页插件PageHelper的使用(转)
Mybatis 的分页插件PageHelper-4.1.1的使用 Mybatis 的分页插件 PageHelper 项目地址:http://git.oschina.net/free/Mybatis_P ...
- MyBatis学习总结_17_Mybatis分页插件PageHelper
如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件. 分页插件支持任何复杂的单表.多表分页,部分特殊情况请看重要提示. 想要使用分页插件?请看如何使用分页插件. 物理分页 该 ...
- Spring集成PageHelper的简单用法
1.Maven依赖,注意使用PageHelper时的版本必须与Mybatis版本对应 <!-- 添加Mybatis依赖 --> <dependency> <groupId ...
- Mybatis分页插件PageHelper正确的用法(网上有2篇不够科学的文章)
今天下午在Mybatis项目中.实现分页.由于我是后加入项目中的,Leader用的是PageHelper这个组件.可是我在实际使用的过程中遇到了2个大问题. 1.p=2#comments" ...
- maven+springmvc+easyui+fastjson+pagehelper
1.maven配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
随机推荐
- windows命令行下批量拷贝同一后缀的文件到另外一个目录
一个目录下有很多文件夹,想拷贝每个文件夹下面的wmv文件到另外一个目录,如果鼠标打开一个文件,拷贝一个,再打开其他的,逐一操作,很麻烦的,百度了一下,xcopy命令就可以实现:例如将C盘x1目录下所有 ...
- MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error
今天购物车突然不能添加了,发现redis报错了,重启了一下好了,一会又报错了. 错误信息: MISCONF Redis is configured to save RDB snapshots, but ...
- 编写高质量代码改善C#程序的157个建议——建议101:使用扩展方法,向现有类型“添加”方法
建议101:使用扩展方法,向现有类型“添加”方法 考虑如何让一个sealed类型具备新的行为.以往我们会创建一个包装器类,然后为其添加方法,而这看上去一点儿也不优雅.我们也许会考虑修改设计,直接修改s ...
- sublime Text2常见插件介绍
zen coding 一种快速编写HTML/CSS代码的方法,已改名为Emmet,并且搭建了一个新的网站:docs.emmet.io Sublime Text 2安装插件Emmet后,打开sublim ...
- Python 3 Mysql 增删改查
import pymysql import datainfo import time #获取参数 host = datainfo.host username = datainfo.username p ...
- 软件加license的一种实现方法
以前从没干过破解的勾当,这次确实必须要去破解一个,于是下了个反编译工具. 最终拿到反编译出来的文件,欣赏了一把它的license检测代码.原谅我的无知,以下代码在我看来还是比较新鲜,犬神请不要鄙视: ...
- python文件操作os模块
Python 统计某一文件夹下文件数量 使用python pathlib模块 from pathlib import Path dir_path = ' ' print(len(list(Path( ...
- vs2008编译opencv,不能copy CMakeVSMacros2.vsmacros
由于学习opencv,要查看源码文件,所以要先对opencv进行编译,可悲的是出错了 “不能copy CMakeVSMacros2.vsmacros” 通过上网查找资料,之所以出现这种情况,是因为 ...
- C#操作系统计划任务
首先需要引用system32下的taskschd.dll 测试环境:win8+vs2012+.net4.0 /// <summary> /// 删除任务 /// </summary& ...
- ZKEACMS 2.2.1 正式发布,更多特性等您来发现
前言 如果你还不知道ZKEACMS,不妨先了解一下. ASP.NET MVC 开源建站系统 ZKEACMS 推荐,从此网站“拼”起来 官方地址:http://www.zkea.net/zkeacms ...