Why PageHelper Pagination Breaks with List Operations and How to Fix It
This article explains why PageHelper pagination yields incorrect totals when list operations are performed before or after pagination, analyzes the underlying cause that startPage only affects subsequent SQL, and provides a step‑by‑step solution that processes the list after pagination and returns a correctly populated PageInfo object.
Problem
When using PageHelper, pagination behaves incorrectly if the list is processed before or after the pagination is started.
1.1 Start pagination first, then manipulate the list
@Override
public PageInfo<HdQueryVo> getRecordsByView(int pageNo, int pageSize) {
PageHelper.startPage(pageNo, pageSize);
List<HdQueryVo> hdQueryVosByView = actionMapper.getActionByView();
List<HdQueryVo> hdQueryVos = new ArrayList<>();
for (HdQueryVo hdQueryVo : hdQueryVosByView) {
HdQueryVo hdQueryVoSingle = new HdQueryVo();
hdQueryVoSingle.setHdId(hdQueryVo.getHdId());
hdQueryVoSingle.setHdType(hdQueryVo.getHdType());
hdQueryVoSingle.setHdTitle(hdQueryVo.getHdTitle());
hdQueryVoSingle.setHdStartDate(hdQueryVo.getHdStartDate());
hdQueryVoSingle.setHdEndDate(hdQueryVo.getHdEndDate());
hdQueryVoSingle.setHdStatus(hdQueryVo.getHdStatus());
hdQueryVoSingle.setHdImage(hdQueryVo.getHdImage());
hdQueryVoSingle.setHdNumber(hdQueryVo.getHdNumber());
hdQueryVoSingle.setGmtCreate(hdQueryVo.getGmtCreate());
hdQueryVoSingle.setGmtModified(hdQueryVo.getGmtModified());
hdQueryVoSingle.setUserId(hdQueryVo.getUserId());
if (hdQueryVo.getHdType().equals(0)) {
hdQueryVoSingle.setHdJoinUsers(onlineWorksMapper.getOnlineJoinUsers(hdQueryVo.getHdId()));
} else {
hdQueryVoSingle.setHdJoinUsers(offlineUsersMapper.getOfflineJoinUsers(hdQueryVo.getHdId()));
}
hdQueryVos.add(hdQueryVoSingle);
}
PageInfo<HdQueryVo> pageViewInfo = new PageInfo<>(hdQueryVos);
return pageViewInfo;
}The total count equals the page size, indicating that pagination information is wrong.
1.2 Manipulate the list first, then start pagination
@Override
public PageInfo<HdQueryVo> getRecordsByView(int pageNo, int pageSize) {
List<HdQueryVo> hdQueryVosByView = actionMapper.getActionByView();
List<HdQueryVo> hdQueryVos = new ArrayList<>();
for (HdQueryVo hdQueryVo : hdQueryVosByView) {
HdQueryVo hdQueryVoSingle = new HdQueryVo();
// same field copying as above
if (hdQueryVo.getHdType().equals(0)) {
hdQueryVoSingle.setHdJoinUsers(onlineWorksMapper.getOnlineJoinUsers(hdQueryVo.getHdId()));
} else {
hdQueryVoSingle.setHdJoinUsers(offlineUsersMapper.getOfflineJoinUsers(hdQueryVo.getHdId()));
}
hdQueryVos.add(hdQueryVoSingle);
}
PageHelper.startPage(pageNo, pageSize);
PageInfo<HdQueryVo> pageViewInfo = new PageInfo<>(hdQueryVos);
return pageViewInfo;
}Data is retrieved and the total count is correct, but the pagination does not take effect.
Root Cause
PageHelper's startPage only affects the SQL query that follows it.
When pagination is started before list processing, the subsequent conversion of the list to PageInfo does not influence the already‑executed query.
When pagination is started after the list has been built, there is no SQL statement for PageHelper to intercept, so pagination is ignored.
Solution
Operate on the list after PageHelper has produced the PageInfo object, then copy the pagination metadata back to a new PageInfo.
Extract the list from the source PageInfo.
Perform the required transformations on each element.
Copy pagination attributes to a new PageInfo and set the transformed list.
@Override
public PageInfo<HdQueryVo> getRecordsByView(int pageNo, int pageSize) {
PageInfo<HdQueryVo> source = PageHelper.startPage(pageNo, pageSize).doSelectPageInfo(() -> {
actionMapper.getActionByView();
});
// New PageInfo to hold transformed data
PageInfo<HdQueryVo> target = new PageInfo<>();
BeanUtils.copyProperties(source, target);
List<HdQueryVo> transformed = source.getList().stream().map(hdQueryVo -> {
HdQueryVo single = new HdQueryVo();
single.setHdId(hdQueryVo.getHdId());
single.setHdType(hdQueryVo.getHdType());
single.setHdTitle(hdQueryVo.getHdTitle());
single.setHdStartDate(hdQueryVo.getHdStartDate());
single.setHdEndDate(hdQueryVo.getHdEndDate());
single.setHdStatus(hdQueryVo.getHdStatus());
single.setHdImage(hdQueryVo.getHdImage());
single.setHdNumber(hdQueryVo.getHdNumber());
single.setGmtCreate(hdQueryVo.getGmtCreate());
single.setGmtModified(hdQueryVo.getGmtModified());
single.setUserId(hdQueryVo.getUserId());
if (hdQueryVo.getHdType().equals(0)) {
single.setHdJoinUsers(onlineWorksMapper.getOnlineJoinUsers(hdQueryVo.getHdId()));
} else {
single.setHdJoinUsers(offlineUsersMapper.getOfflineJoinUsers(hdQueryVo.getHdId()));
}
return single;
}).collect(Collectors.toList());
target.setList(transformed);
return target;
}References: PageHelper official documentation and related articles on processing the result list after pagination.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
