Resolve PageHelper total and PageSize are always equal after startpage (page, size)

Posted by 3r0ss on Fri, 04 Feb 2022 14:06:52 +0100

Many people will use PageHelper to automatically page, encapsulate the found data and return to the front end, but find that the amount of data displayed is inconsistent with expectations, and even always equal to the set PageSize. However, after consulting a lot of blog materials, it is found that the words are only half said, which does not solve the problem for beginners. The problem will be completely solved today.

1, Question

PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy());
//First interaction with database
List<Coupon> couponList = couponMapper.selectByExample(couponExample);

//encapsulation
List<SearchCouponResponse> searchCouponResponseList = couponDao.getSearchCouponResponse(couponList, userId);

//Back to the front end
return new PageInfo(searchCouponResponseList);

The Internet says PageHelper Startpage must be followed by the function that interacts with the database. In this way, they can intercept the data interacting with the database for the first time and perform paging processing. That's right, but it's essential for me to get a lot of business. However, the encapsulated data loses a lot of attributes and the total amount of data is also lost. If we want to solve this problem, we must first know why.

2, Cause

Code block I

public PageInfo(List<T> list, int navigatePages) {
        super(list);//Key, code block 2 shows the super content
        if (list instanceof Page) {  //crux
            Page page = (Page) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();

            this.pages = page.getPages();
            this.size = page.size();
            //Since the result is > startRow, the actual need is + 1
            if (this.size == 0) {
                this.startRow = 0;
                this.endRow = 0;
            } else {
                this.startRow = page.getStartRow() + 1;
                //Calculate the actual endRow (special on the last page)
                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;//crux
            this.size = list.size();
            this.startRow = 0;
            this.endRow = list.size() > 0 ? list.size() - 1 : 0;
        }

Code block 2

//The first constructor has a super(), and here is the content of super
public PageSerializable(List<T> list) {
        this.list = list;
        if(list instanceof Page){
            this.total = ((Page)list).getTotal();
        } else {
            this.total = list.size();
        }
    }

According to code block 1, anyone with a little common sense can find that the list we passed in is actually a Page, so he needs to judge whether the list passed in can be transformed into a Page object. Why? Because we said that when we interact with the database for the first time, it is intercepted and encapsulated, so the data returned to you is paged and retains specific information, and this process is the process of encapsulating the list searched by the database into a Page, that is to say

List<Coupon> couponList = couponMapper.selectByExample(couponExample);

This couponList is not a real List, but a Page type. Interested friends make a breakpoint to see what he is.

In other words, the list returned to me has been paged and saved all the necessary data. So I thought, can I create a PageInfo by myself, and I put the necessary data into it, but what about the list? Put my encapsulated data into it, and do a civet cat for Prince operation

3, Solution

public PageInfo(List<T> list, int navigatePages) {
        super(list);
        if (list instanceof Page) {
            Page page = (Page) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();

            this.pages = page.getPages();//Total pages
            this.size = page.size();
 }
public PageSerializable(List<T> list) {
        this.list = list;
        if(list instanceof Page){
            this.total = ((Page)list).getTotal();
        } else {
            this.total = list.size();
        }
    }

Then a person with a little common sense on PageInfo will find that he needs pageNum, pageSize, pages, size, and total. The first two are customized, and the last three are already in the variable returned to our list for the first interaction. There is also this List = list would it be good to replace what we have encapsulated for him?

4, Results

.....................ellipsis
PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy());
        List<Coupon> couponList = couponMapper.selectByExample(couponExample);

        //encapsulation
        List<SearchCouponResponse> searchCouponResponseList = couponDao.getSearchCouponResponse(couponList, userId);

        PageInfo pageInfo = new PageInfo();
        if(couponList instanceof com.github.pagehelper.Page) {
            pageInfo = buildPageInfo((com.github.pagehelper.Page) couponList);//Extract the attribute and create a pageInfo
        }
        pageInfo.setList(searchCouponResponseList); //Then put our sealed ones into it, and the civet cat will change into the crown prince

        if(couponList.size() == 0) return new Page(new PageInfo(new ArrayList()));;


        return new Page(pageInfo);
        
....................ellipsis
}

private PageInfo buildPageInfo(com.github.pagehelper.Page pageList) {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(pageList.getPageNum());
        pageInfo.setPageSize(pageList.getPageSize());
        pageInfo.setPages(pageList.getPages());
        pageInfo.setTotal(pageList.getTotal());
        return pageInfo;
    }

My original CSDN address

Topics: Java