使用pageHelper遇到的问题

摘要:
com.github.pagehelper<pagehelper<版本>plugininterceptor=“com.github.pagehelper.PageInterceptor”>propertyname=“合理”value=“true”/>/插件>&书信电报;3.接下来,开始测试;测试代码;


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

 1.首先导入pageHelper的包:

<!--引入pageHelper分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.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(1,5);
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());
}

测试结果:
使用pageHelper遇到的问题第1张

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

@Test
public void getAll(){
PageHelper.startPage(1,5);
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遇到的问题第2张

结果正是我所需要的,既然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(1,5);
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遇到的问题第1张

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

免责声明:文章转载自《使用pageHelper遇到的问题》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Quartz 设置一个半小时任务实现HElib下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

pdfcrop不能使用

最近,用到了pdfcrop,用来去除pdf中空白的边。 但是使用pdfcrop --margins 0  *.pdf 后,给出了错误: Error: pdfcrop cannot call ghostscript <gswin32c> 但是我已经安装了CTEX,里面已经包含Ghostscript,所以就不知道什么错误。在网上针对这个问题找了...

CSS总结(六)——元素的垂直居中(已知高度/未知高度)

元素的垂直居中  1、已知高度宽度元素的水平垂直居中       ①  绝对定位居中          .center{                     margin:auto;                     position:absolute;                     top:0;                  ...

(五)、Eureka服务注册中心

EureKa基础知识 什么是服务治理 Spring Cloud封装了Netflix 公司开发的Eureka模块来实现服务治理 在传统的RPC远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。 什么是服务注册与发现 Eureka采用了...

简写artTemplate模板一二

最近研发的项目当中,用到了artTemplate模板,在此简写一二,一来养成做笔记的习惯,二来习惯了做笔记。 一、展示模板 <div id='mySwipe'  class='swipe'><div id="content"></div></div> 二、存放模板 <script type="text/...

uniapp支付宝小程序上传图片转base64

最近使用uniapp涉及到一个上传图片的功能,原本看官方文档api觉得没问题,正常开发, 1. 首先微信端没问题 uni.getFileSystemManager().readFile({ filePath: url, //选择图片返回的相对路径 encoding: "base64", //编码格式 succes...

vue.js中v-for的使用及索引获取

2.x版本: v-for="(item,index) in items" index即索引值。  ==========================分割线============================== 1.x版本: 1.v-for   示例一: <!DOCTYPE html> <html> <head>...