When using PageHelper's pagination, when the page number is greater than the total number of data pages, the last page of data is returned. Requirement: the returned data is empty

The pagination problem encountered when using Ruoyi’s framework: When using PageHelper, it is found that the number of pages exceeds the maximum number, and the data (the last page of data) can still be returned. Requirement: The returned data is empty.

The reason is the default configuration of pagehelper's configuration information reasonable.

When pagehelper.reasonable=true,
first set the query range in the database, and then query according to the conditions. At this time, the data outside the query range cannot be found.
When pagehelper.reasonable=false,
first query the database according to the condition, and then perform pagination according to the range.

However, the paging configuration in application.yml is modified, and the data obtained by paging does not change. As shown below:

Then directly modify the code in the PageUtils.startPage(); method to solve the problem; as shown below:

If the above content cannot solve the problem, please refer to the following:

Some functions in the project use PageHelper for paging processing, using the following code:

PageHelper.startPage(request.getPageNum(), request.getPageSize(), true);


This way of use will appear,
when the requested page number = 0, all data will be returned;
when the requested page number is less than 0, the first page of data will be returned, and when the page number is greater than the total number of pages, the last page will be returned data.

Through debug tracking, it is found that when the reasonable parameter defaults to true, the pageNum will be reset to the size of pages, resulting in the data problem of the last page being returned when the number of requested pages is greater than the total number of pages.

insert image description here

insert image description here

Solution:
Modify the usage method

PageHelper.startPage(request.getPageNum(), request.getPageSize(), true,false,null);


The above method can solve the problem that when the requested page number is less than 0, the data of the first page is returned, and when the page number is greater than the total number of pages, the data of the last page is returned.

It is also necessary to analyze that when the number of requested pages is less than the total number of pages, all data will be returned;
this is controlled by another parameter pageSizeZero;
pageSizeZero Description: When set to true (the default is true), if pagesize is set to 0 (or RowBounds limit = 0), pagination will not be performed, and all results will be returned
. Set this to false to solve the problem that all data will be returned when the number of requested pages = 0.

modify usage 

PageHelper.startPage(request.getPageNum(), request.getPageSize(), true,false,false);

Reference: Mybatis-PageHelper/HowToUse.md at master · pagehelper/Mybatis-PageHelper · GitHub 

Guess you like

Origin blog.csdn.net/yyongsheng/article/details/127565176