The pit of PageHelper paging, the detected total is wrong, and it is always equal to the value of pageSize

Project scenario:

Use PageHelper pagination query, return data error


Problem Description

The total number detected is always equal to the number displayed on each page, not the actual number of data

参数:pageNumber=1  pageSize=2,数据库实际数量20

{
  "page": {
    "total": 2,
    "list": ["1","2"],
    "pageNum": 1,
    "pageSize": 2,
    "size": 7,
    "startRow": 1,
    "endRow": 7,
    "pages": 1,
    "prePage": 0,
    "nextPage": 0,
    "isFirstPage": true,
    "isLastPage": true,
    "hasPreviousPage": false,
    "hasNextPage": false,
    "navigatePages": 8,
    "navigatepageNums": [
      1
    ],
    "navigateFirstPage": 1,
    "navigateLastPage": 1
  }
}



Cause Analysis:

List<XxxxVO> xxxxVOS = BeanHelper.copyCollection(list, XxxxVO .class);
 new PageInfo<>(xxxxVOS);//The problem lies here
 //Note: BeanHelper here is a custom type conversion class

After using pagination, after performing type conversion on the queried data, and then assigning it to PageInfo, the total number of data will change to the value of the current collection instead of the total value of the database.


solution:

List<XxxxEntity> list = queryList(dto);
 PageInfo pageInfo = new PageInfo<>(list);//Assign value first, be careful not to write specific generics
List<XxxxVO> xxxxVOS = BeanHelper.copyCollection(list, XxxxVO .class);
pageInfo.setList( xxxxVOS );//Put the transformed collection into it

First put the queried list collection into PageInfo, be careful not to specify data generics here , and then put the converted collection into it after type conversion

{
  "page": {
    "total": 20,
    "list": ["1","2"],
    "pageNum": 1,
    "pageSize": 2,
    "size": 7,
    "startRow": 1,
    "endRow": 7,
    "pages": 1,
    "prePage": 0,
    "nextPage": 0,
    "isFirstPage": true,
    "isLastPage": true,
    "hasPreviousPage": false,
    "hasNextPage": false,
    "navigatePages": 8,
    "navigatepageNums": [
      1
    ],
    "navigateFirstPage": 1,
    "navigateLastPage": 1
  }
}

Guess you like

Origin blog.csdn.net/cccsssrrr/article/details/127784908