Implementation parameters set manually when using PageInfo do pagination

Copyright: code word is not easy, please indicate the source ~~ https://blog.csdn.net/m0_37190495/article/details/88117026

Implementation parameters set manually when using PageInfo do pagination

Reproduced, please indicate the source:
Original starting in: http://www.zhangruibin.com
article from RebornChang's blog

In doing project development time, pagehelper is a page we often use plug-ins, source address: com.github.pagehelper.
For basic use and introduction com.github.pagehelper is not carried out in this blog post describes, I do not know what pagehelper is not clear how or with a friend, please Baidu own, not repeat them here.
Bloggers write technical articles generally prefer nesting is covered in real production development, this introduction is still a problem is encountered during project development blogger when they have gone in to write a detour, so write it for reference .

Demand background

In a project we have a lot of need to call an external interface, so customers want a statistical call these interfaces, such as interfaces A, called many times, each successful or failed, to carry what parameters and parameter returns the response time of the request is. Based on this demand leads to the following question: When statistics on how statistical results page.

Statistical results page

If the direct use pagehelper paging, then directly limit to execute sql database page, so it can not guarantee the integrity of the data, since 20 data I'd like to show on the page, may be sorting through statistics by the tens of thousands of pieces of data out of 20 information, it can not be sorted automatically by pagehelper, and this time you need a manual page.

PageInfo class which required manual page parameters Meaning

//当前页
 private int pageNum;
 //每页的数量
 private int pageSize;
 //当前页的数量
 private int size;
 //当前页展示的数据的起始行
 private int startRow;
 //当前页展示的数据的结束行
 private int endRow;
 //总记录数--所需要进行分页的数据条数
 private long total;
 //总页数
 private int pages;
 //页面展示的结果集,比如说当前页要展示20条数据,则此list为这20条数据
 private List<T> list;
 //前一页页码
 private int prePage;
 //下一页页码
 private int nextPage;
 //是否为第一页,默认为false,是第一页则设置为true
 private boolean isFirstPage ;
 //是否为最后一页默认为false,是最后一页则设置为true
 private boolean isLastPage ;
 //是否有前一页,默认为false,有前一页则设置为true
 private boolean hasPreviousPage ;
 //是否有下一页,默认为false,有后一页则设置为true
 private boolean hasNextPage ;
 //导航页码数,所谓导航页码数,就是在页面进行展示的那些1.2.3.4...
 //比如一共有分为两页数据的话,则将此值设置为2
 private int navigatePages;
 //所有导航页号,一共有两页的话则为[1,2]
 private int[] navigatepageNums;
 //导航条上的第一页页码值
 private int navigateFirstPage;
 //导航条上的最后一页页码值
 private int navigateLastPage;

PageInfo manually set the parameters, the rules specify the paged

Note: no reception page number where mass participation like the code, there is a need, then you can Baidu, bunch.
Reception pass over an object pagebale, object has two parameters. 1: Page. 2: The number of data per page article.

Collate

If the traditional simple paging, directly Pageable default paging on the line:

PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());

//在这里执行要进行分页的sql,获取分页后的结果集resultList

return new PageInfo<Object>(resultList);

Manual page

After reading the above automatic paging code is not understood why there is a feeling of a so-called manual page, just put this return new PageInfo (resultList); PageInfo manually defined parameters at the object on the line.
Early similar physical pages, but are counting something like boundary value, the specific setting method is as follows:

      //2019-03-04 zhrb 手动封装PageInfo参数进行分页
      PageInfo realPageInfo = new PageInfo<InterfaceMonitorLogCountVo>(interfaceMonitorLogCountVoList);
      int start = 0;
      int end = 0;
      int totalPages = 0;
      int totalRecord = 0;
      int pageSize = 0;
      int size  = 0;
      int number = 0;
      size  = pageable.getPageSize();
      number = pageable.getPageNumber();
      pageSize = pageable.getPageSize();
      totalRecord = interfaceMonitorLogCountVoList.size();
      //设置总数
      realPageInfo.setTotal(totalRecord);
      //设置每页的显示条数
      realPageInfo.setPageSize(size);
      //设置要显示的是第几页的数据
      realPageInfo.setPageNum(number);
      realPageInfo.setSize(totalRecord);
      //计算获取对应的要显示的数据
      if(totalRecord%pageSize==0){
          totalPages = totalRecord / pageSize;
      }else {
          totalPages = totalRecord / pageSize + 1;
      }
      realPageInfo.setPages(totalPages);
      //初始边界值计算
      if (number == 1){
          start = 0;
          realPageInfo.setHasPreviousPage(false);
          realPageInfo.setPrePage(0);
          realPageInfo.setIsFirstPage(true);
      }else {
          start = realPageInfo.getPageSize()*(realPageInfo.getPageNum()-1);
          realPageInfo.setHasPreviousPage(true);
          realPageInfo.setPrePage(number-1);
          realPageInfo.setIsFirstPage(false);
      }
      realPageInfo.setStartRow((number-1)*pageSize);
      //结束边界值计算
      if ((start+realPageInfo.getPageSize() > realPageInfo.getTotal())){
          end = totalRecord;
          realPageInfo.setHasNextPage(false);
          realPageInfo.setIsLastPage(true);
          realPageInfo.setEndRow(totalRecord);
      }else {
          end = start + realPageInfo.getPageSize();
          realPageInfo.setHasNextPage(true);
          realPageInfo.setNextPage(number + 1);
          realPageInfo.setIsLastPage(false);
          realPageInfo.setEndRow((number)*pageSize);
      }
      if (start < end && end <= totalRecord){
          realPageInfo.setList(interfaceMonitorLogCountVoList.subList(start,end));
      }
      if(realPageInfo.getSize() == 0) {
          realPageInfo.setStartRow(0);
          realPageInfo.setEndRow(0);
      } else {
          realPageInfo.setStartRow(realPageInfo.getStartRow() + 1);
          realPageInfo.setEndRow(realPageInfo.getStartRow()-1+realPageInfo.getSize());
      }
      realPageInfo.setPages(totalPages);
      realPageInfo.setNavigateLastPage(totalPages>number?number+1:totalPages);
      return realPageInfo;

Then placed in ModelMap layer controller will return the result on the line:


modelMap.addAttribute("page",realPageInfo ); 

In addition, the blog provides a good package PageBean, content taken from the network, had the idea to write a manual page Friends of Bo can refer to, if infringement please leave a message bloggers, bloggers will delete the contents of the response:

package com.oms.util;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;
/**
* @ClassName PageBean
* @Description TODO 手动分页基础类
* @Date 2019/3/4 9:12
* @Version
*/
public class PageBean<T> {
  //已知的参数
  //当前页,从请求那边传过来
  private int pageNum;
  //每页显示的数据条数
  private int pageSize;
  //总的记录条数。查询数据库得到的数据
  private int totalRecord;

  //需要计算得来
  //总页数,通过totalRecord和pageSize计算可以得来
  private int totalPage;
  //开始索引,也就是我们在数据库中要从第几行数据开始拿,有了startIndex和pageSize,
  //就知道了limit语句的两个数据,就能获得每页需要显示的数据了
  private int startIndex;
  //将每页要显示的数据放在list集合中
  private List<T> list;
  //分页显示的页数,比如在页面上显示1,2,3,4,5页,start就为1,end就为5,这个也是算过来的
  private int start;
  private int end;
  //通过pageNum,pageSize,totalRecord计算得来tatalPage和startIndex
  //构造方法中将pageNum,pageSize,totalRecord获得
  public PageBean(int pageNum,int pageSize,int totalRecord) {
      this.pageNum = pageNum;
      this.pageSize = pageSize;
      this.totalRecord = totalRecord;

      //totalPage 总页数
      if(totalRecord%pageSize==0){
          //说明整除,正好每页显示pageSize条数据,没有多余一页要显示少于pageSize条数据的
          this.totalPage = totalRecord / pageSize;
      }else{
          //不整除,就要在加一页,来显示多余的数据。
          this.totalPage = totalRecord / pageSize +1;
      }
      //开始索引
      this.startIndex = (pageNum-1)*pageSize ;
      //显示5页,这里自己可以设置,想显示几页就自己通过下面算法修改
      this.start = 1;
      this.end = 5;
      //显示页数的算法

      if(totalPage <=5){
          //总页数都小于5,那么end就为总页数的值了。
          this.end = this.totalPage;
      }else{
          //总页数大于5,那么就要根据当前是第几页,来判断start和end为多少了,
          this.start = pageNum - 2;
          this.end = pageNum + 2;

          if(start < 0){
              //比如当前页是第1页,或者第2页,那么就不如和这个规则,
              this.start = 1;
              this.end = 5;
          }
          if(end > this.totalPage){
              //比如当前页是倒数第2页或者最后一页,也同样不符合上面这个规则
              this.end = totalPage;
              this.start = end - 5;
          }
      }
  }
  //get、set方法。
  public int getPageNum() {
      return pageNum;
  }

  public void setPageNum(int pageNum) {
      this.pageNum = pageNum;
  }

  public int getPageSize() {
      return pageSize;
  }

  public void setPageSize(int pageSize) {
      this.pageSize = pageSize;
  }

  public int getTotalRecord() {
      return totalRecord;
  }

  public void setTotalRecord(int totalRecord) {
      this.totalRecord = totalRecord;
  }

  public int getTotalPage() {
      return totalPage;
  }

  public void setTotalPage(int totalPage) {
      this.totalPage = totalPage;
  }

  public int getStartIndex() {
      return startIndex;
  }

  public void setStartIndex(int startIndex) {
      this.startIndex = startIndex;
  }

  public List<T> getList() {
      return list;
  }

  public void setList(List<T> list) {
      this.list = list;
  }

  public int getStart() {
      return start;
  }

  public void setStart(int start) {
      this.start = start;
  }

  public int getEnd() {
      return end;
  }

  public void setEnd(int end) {
      this.end = end;
  }
}

Over!

Guess you like

Origin blog.csdn.net/m0_37190495/article/details/88117026