使用PageHelper插件实现Service层分页

本文实例为大家分享了使用PageHelper插件实现Service层分页的具体代码,供大家参考,具体内容如下

使用场景:

平时分页我们可以直接使用mybatis-plus中内置的IPage进行分页,一般是在mapper中写好接口,在执行sql时就将其进行分页操作,但是有些复杂的查询或者是需要拼接返回格式的数据就难以操作了,所以我们使用PageHelper插件来实现Service分页功能。

1.在pom.xml文件中导入PageHelper插件依赖

<!--pagehelper分页插件-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>4.1.6</version>
</dependency>

2.编写PageHelper配置类

package com.cdtye.itps.jjxt.config;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.github.pagehelper.PageHelper;

/**
 * @Author Zhongks
 * @Description //TODO 分页配置对象
 * @Date 14:47 2021/4/23
 * @Param
 * @return
 **/
@Configuration
public class PageHelperConfiguration {
    /**
     * @Author Zhongks
     * @Description //TODO 分页对象实列化
     * @Date 15:49 2021/4/23
     * @Param []
     * @return com.github.pagehelper.PageHelper
     **/
    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        p.setProperty("dialect", "Oracle");
        pageHelper.setProperties(p);
        return pageHelper;
    }
}

3.在Service层进行分页操作:

/**
     * @Author Zhongks
     * @Description //TODO 列表页面显示
     * @Date 18:42 2021/4/22
     * @Param []
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public PageInfo<Map<String, Object>> getList(BureauNoticeVo vo){
        if(vo.getPage()!=null&&vo.getSize()!=null){
            //设置页码数以及一页显示数量
            PageHelper.startPage(vo.getPage(),vo.getSize());
        }
        //自己发布的或者下发单位中含有当前登入人单位编码的才能看
        List<Map<String, Object>> bureauNoticeList = bureauNoticeMapper.getList(vo,AuthHelper.loginUser().getUnitDeptCode());
        bureauNoticeList.forEach(map->{
            //得到下发单位信息集合
            List<String> deptNameList = bureauNoticeAcceptService.getBureauNoticeAcceptAndDeptByNoticeId((String) map.get("id"));
            map.put("deptNameList",deptNameList);
            //得到附件信息集合
            map.put("fileList",this.getFileList((String) map.get("id")));
        });
        //将需要进行分页的list传入Pagehelper实现分页
        PageInfo<Map<String, Object>> pageInfo = new PageInfo(bureauNoticeList);
        return pageInfo;
    }

4.查询类Vo:

@ApiModel("")
@Getter
@Setter
public class BureauNoticeVo extends BaseVo {

    @ApiModelProperty(value = "开始时间")
    private String startDate;

    @ApiModelProperty(value = "开始时间")
    private String endDate;

    @ApiModelProperty(value = "描述")
    private String noticeDescription;

    @ApiModelProperty(value = "页码")
    private Integer page;

    @ApiModelProperty(value = "页显示数")
    private Integer size;

}

5.接口返回数据:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Mybatis分页插件PageHelper的使用详解

    1.说明 如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件. 该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页. 2.使用方法 第一步:在Mybatis配置xml中配置拦截器插件: <plugins> <!-- com.github.pagehelper为PageHelper类所在包名 --> <plugin interceptor="com.github.pageh

  • 使用mybatis插件PageHelper实现分页效果

    最近都在忙着写一个网站项目,今天做一个分页功能的时候,遇到了分页效果实现不了的问题,查了好久的资料,后来终于是成功解决啦,记录一下~ 1.在pom.xml中添加分页插件依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.5</version> </depend

  • SpringBoot集成MyBatis的分页插件PageHelper实例代码

    昨天给各位总结了本人学习springboot整合mybatis第一阶段的一些学习心得和源码,主要就算是敲了一下SpringBoot的门儿,希望能给各位的入门带给一点儿捷径,今天给各位温习一下MyBatis的分页插件PageHelper和SpringBoot的集成,它的使用也非常简单,开发更为高效.因为PageHelper插件是属于MyBatis框架的,所以相信很多哥们儿都已经用烂了,下面带着各位吃一下回头草. 首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通

  • PageHelper插件实现一对多查询时的分页问题

    项目中经常会使用到一对多的查询场景,但是PageHelper对这种嵌套查询的支持不够,如果是一对多的列表查询,返回的分页结果是不对的 参考Github上的说明:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/Important.md 对于一对多的列表查询,有两种方式解决 1.在代码中处理.单独修改分页查询的resultMap,删除collection标签,然后在代码中遍历结果,查询子集 2.使用mybat

  • SpringMvc+Mybatis+Pagehelper分页详解

    最近公司需要做一个告警页面的功能,需要分页,查了很多资料发现PageHelper比较合适 故写一篇从零开始的PageHelper使用的教程,也记录下忙活一天的东西 1.首先需要在项目中添加PageHelper的依赖,这里我用的Maven添加 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>

  • SpringBoot项目中分页插件PageHelper无效的问题及解决方法

    在Springboot项目中使用分页插件的时候 发现PageHelper插件失效了 我导入的是: 后来才发 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency> 现 PageHelper若要在Springbo

  • Mybatis分页插件PageHelper的配置和简单使用方法(推荐)

    前言 在web开发过程中涉及到表格时,例如dataTable,就会产生分页的需求,通常我们将分页方式分为两种:前端分页和后端分页. 前端分页 一次性请求数据表格中的所有记录(ajax),然后在前端缓存并且计算count和分页逻辑,一般前端组件(例如dataTable)会提供分页动作. 特点是:简单,很适合小规模的web平台:当数据量大的时候会产生性能问题,在查询和网络传输的时间会很长. 后端分页 在ajax请求中指定页码(pageNum)和每页的大小(pageSize),后端查询出当页的数据返回

  • Spring Boot+Mybatis+Druid+PageHelper实现多数据源并分页的方法

    前言 本篇文章主要讲述的是SpringBoot整合Mybatis.Druid和PageHelper 并实现多数据源和分页.其中SpringBoot整合Mybatis这块,在之前的的一篇文章中已经讲述了,这里就不过多说明了.重点是讲述在多数据源下的如何配置使用Druid和PageHelper . Druid介绍和使用 在使用Druid之前,先来简单的了解下Druid. Druid是一个数据库连接池.Druid可以说是目前最好的数据库连接池!因其优秀的功能.性能和扩展性方面,深受开发人员的青睐. D

  • Springboot整合pagehelper分页功能

    本文实例为大家分享了Springboot整合pagehelper分页展示的具体代码,供大家参考,具体内容如下 一.添加依赖 查找maven中pagehelper的版本 在pom中添加依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.2&

  • mybatis分页插件pageHelper详解及简单实例

    mybatis分页插件pageHelper详解及简单实例 工作的框架spring springmvc mybatis3 首先使用分页插件必须先引入maven依赖,在pom.xml中添加如下 <!-- 分页助手 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.7.5

随机推荐