PageHelper分页插件及相关案例介绍

pagehelper是一个分页插件,用来后端API进行分页处理,pagehelper只需要pageNum(当前页)和pageSize(页大小)就可以自动实现查询的分页数据.只对紧接着后续一条查询语句起作用.

项目运行成功后,查询语句之前加 PageHelper.startPage(pageNum, pageSize) 即可实现分页,PageHelper只对紧跟着的第一个SQL语句起作用.分页成功后,实际分会的结果list类型Page<E> ,可使用 PageInfo<E> 进行封装,后文有相应源码.

pagehelper可以与前端datatablespagination插件配合使用.前端具体插件具体怎么使用这里不再介绍.

1.maven中引入依赖

<!-- 分页插件 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.3</version>
</dependency>
<!-- entity插件 -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.18</version>
</dependency>

2.controller

	@GetMapping("/testresult/{project}/{pageNum}/{pagesize}")
    public ResponseEntity<TestResultResponse> 
    getPhaseSmokeResult(@PathVariable(value="pageNum",defaultValue="1")Integer pageNum,
                        @PathVariable(value="pageSize",defaultValue="6")Integer pageSize,
                        @PathVariable("project")String project){
        log.debug("REST request to get {} phase smoke result from {} to {} ", project, pageNum, pageSize);
        TestResultResponse response = smokeResultService.getPhaseSmokeResult(pageNum,pagesize,project);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

3.serviceImpl

	@Override
    public TestResultResponse getPhaseSmokeResult(Integer pageNum, Integer pagesize, String project) {
        PageHelper.startPage(pageNum, pagesize);
        List<SmokeResult> smokeResultList = smokeResultMapper.getPhaseSmokeResult(project);
        PageInfo<SmokeResult> pageInfo = new PageInfo<>(smokeResultList);
        TestResultResponse response = new TestResultResponse();
        response.setProject(project).setPageInfo(pageInfo);
        return response;
    }

4.entity

TestResultResponse类

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class TestResultResponse {

	private String project;
	private String version;
	private String starttime;
	private String endtime;
	private String total;
	private PageInfo pageInfo;
}

5.PageInfo源码

import com.github.pagehelper.Page;
import lombok.Data;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Data
public class PageInfo<T> implements Serializable{

    private static final long serialVersionUID = 1L;

    //当前页
    private int pageNum;
    //每页的数量
    private int pageSize;
    //当前页的数量
    private int size;

    //由于startRow和endRow不常用,这里说个具体的用法
    //可以在页面中"显示startRow到endRow 共size条数据"

    //当前页面第一个元素在数据库中的行号
    private int startRow;
    //当前页面最后一个元素在数据库中的行号
    private int endRow;
    //总记录数
    private long total;
    //总页数
    private int pages;
    //结果集
    private List<T> list;

    //前一页
    private int prePage;
    //下一页
    private int nextPage;
    //是否为第一页
    private boolean isFirstPage = false;
    //是否为最后一页
    private boolean isLastPage = false;
    //是否有前一页
    private boolean hasPreviousPage = false;
    //是否有下一页
    private boolean hasNextPage = false;

    //导航页码数,页码导航连续显示的页数
    private int navigatePages;
    //所有导航页号
    private int[] navigatepageNums;
    //导航条上的第一页
    private int navigateFirstPage;
    //导航条上的最后一页
    private int navigateLastPage;

    public PageInfo() {
    }

    /**
     * 包装Page对象
     *
     * @param list
     */
    public PageInfo(List<T> list) {
        this(list, 8);
    }

    /**
     * 包装Page对象
     *
     * @param list          page结果
     * @param navigatePages 页码数量,页码导航连续显示的页数
     */
    public PageInfo(List<T> list, int navigatePages) {
        if (list instanceof Page) {
            Page page = (Page) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();

            this.pages = page.getPages();
            this.list = page;
            this.size = page.size();
            this.total = page.getTotal();
            //由于结果是>startRow的,所以实际的需要+1
            if (this.size == 0) {
                this.startRow = 0;
                this.endRow = 0;
            } else {
                this.startRow = page.getStartRow() + 1;
                //计算实际的endRow(最后一页的时候特殊)
                this.endRow = this.startRow - 1 + this.size;
            }
        } else if (list instanceof Collection) {
            this.pageNum = 1;
            this.pageSize = list.size();

            this.pages = this.pageSize > 0 ? 1 : 0;
            this.list = list;
            this.size = list.size();
            this.total = list.size();
            this.startRow = 0;
            this.endRow = list.size() > 0 ? list.size() - 1 : 0;
        }

        if (list instanceof Collection) {
            this.navigatePages = navigatePages;
            //计算导航页
            calcNavigatepageNums();
            //计算前后页,第一页,最后一页
            calcPage();
            //判断页面边界
            judgePageBoudary();
        }
    }

    /**
     * 计算导航页
     */
    private void calcNavigatepageNums() {
        //当总页数小于或等于导航页码数时
        if (pages <= navigatePages) {
            navigatepageNums = new int[pages];
            for (int i = 0; i < pages; i++) {
                navigatepageNums[i] = i + 1;
            }
        } else { //当总页数大于导航页码数时
            navigatepageNums = new int[navigatePages];
            int startNum = pageNum - navigatePages / 2;
            int endNum = pageNum + navigatePages / 2;
            if (startNum < 1) {
                startNum = 1;
                //最前navigatePages页
                for (int i = 0; i < navigatePages; i++) {
                    navigatepageNums[i] = startNum++;
                }
            } else if (endNum > pages) {
                endNum = pages;
                //最后navigatePages页
                for (int i = navigatePages - 1; i >= 0; i--) {
                    navigatepageNums[i] = endNum--;
                }
            } else {
                //所有中间页
                for (int i = 0; i < navigatePages; i++) {
                    navigatepageNums[i] = startNum++;
                }
            }
        }
    }

    /**
     * 计算前后页,第一页,最后一页
     */
    private void calcPage() {
        if (navigatepageNums != null && navigatepageNums.length > 0) {
            navigateFirstPage = navigatepageNums[0];
            navigateLastPage = navigatepageNums[navigatepageNums.length - 1];
            if (pageNum > 1) {
                prePage = pageNum - 1;
            }
            if (pageNum < pages) {
                nextPage = pageNum + 1;
            }
        }
    }

    /**
     * 判定页面边界
     */
    private void judgePageBoudary() {
        isFirstPage = pageNum == 1;
//        isLastPage = pageNum == pages;
        isLastPage = pageNum == pages || pages == 0;
        hasPreviousPage = pageNum > 1;
        hasNextPage = pageNum < pages;
    }
}

参考链接:

posted @ 2020-03-07 19:01  zlgSmile  阅读(861)  评论(1编辑  收藏  举报