PageHelper是针对数据库层面的分页,而非查询结果出来后在分页
一:与数据库一次交互,可以使用PageHelper分页,如下:
<一>请求对象queryTemplateRequest继承分页对象PageParamRequest:
public class PageParamRequest implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 页码
*/
private Long current = 1L;
/**
* 页长
*/
private Long pageSize = 10L;
使用:
public PlainResult<PageResult> queryTemplate(QueryTemplateRequest queryTemplateRequest) {
//默认值
if (null == queryTemplateRequest.getCurrent()) {
queryTemplateRequest.setCurrent(1L);
}
if (null == queryTemplateRequest.getPageSize()) {
queryTemplateRequest.setPageSize(10L);
}
//前端传入的当前处于的页码
PageHelper.startPage(queryTemplateRequest.getCurrent().intValue(), queryTemplateRequest.getPageSize().intValue());
//数据库查询操作
List<McTemplate> mcTemplateList = mcTemplateMapper.selectList(new QueryWrapper<McTemplate>()
.eq(McTemplate.IS_DELETED, TemplateIsDeletedEnum.NO.getStatus())
.eq(McTemplate.STATUS, TemplateStatusEnum.PASSED.getStatus())
);
//确定总页数等
PageInfo<McTemplate> pageInfo = PageInfo.of(mcTemplateList);
// 定义返回对象
PageResult<QueryTemplateVo> pageResult = new PageResult<>();
pageResult.setCurrent(pageInfo.getPageNum());
pageResult.setPageSize(pageInfo.getPageSize());
pageResult.setTotal(pageInfo.getTotal());
List<QueryTemplateVo> queryTemplateVoList = new ArrayList<>();
mcTemplateList.forEach(mcTemplate ->{
QueryTemplateVo queryTemplateVo = new QueryTemplateVo();
BeanUtils.copyProperties(mcTemplate,queryTemplateVo);
queryTemplateVoList.add(queryTemplateVo);
});
//将返回对象赋值给PageResult传给前端
pageResult.setList(queryTemplateVoList);
return PlainResult.successResult(pageResult);
}
二:多次交互,使用失效,在与数据库第一次交互时候已经分页,如下:
<二>:
//第一次交互,此时
SwsJcSubscriptionReceive swsJcSubscriptionReceive = new SwsJcSubscriptionReceive();
swsJcSubscriptionReceive.setDelStatus(DelStatusEnum.DELETE.getValue());
SwsJcSubscriptionReceiveExample swsJcSubscriptionReceiveExample = new SwsJcSubscriptionReceiveExample();
SwsJcSubscriptionReceiveExample.Criteria criteriaReceive = swsJcSubscriptionReceiveExample.createCriteria();
criteriaReceive.andMessageIdEqualTo(messageId);
criteriaReceive.andReceiveUserEqualTo(userInfo.getRoleCode());
criteriaReceive.andReceiveRoleEqualTo(userInfo.getRoleSelect());
int i = swsJcSubscriptionReceiveMapper.updateByExampleSelective(swsJcSubscriptionReceive, swsJcSubscriptionReceiveExample);
//查询,此时第一次与数据库交互,结果集已经按照一页10条进行分页完毕,假使符合筛选条件的数据有10条,但是结果集swsJcSubscriptionMsgList 只有10条数据,
List<SwsJcSubscriptionReceive> swsJcSubscriptionMsgList = swsJcSubscriptionReceiveMapper.selectByExample(swsJcSubscriptionReceiveExample);
//进一步上述条件下筛选,此时是在10条数据库下筛选,出现问题。
SwsJcSubscriptionMsgExample swsJcSubscriptionMsgExample = new SwsJcSubscriptionMsgExample();
SwsJcSubscriptionMsgExample.Criteria criteriaMsgTitle = swsJcSubscriptionMsgExample.createCriteria();
criteriaMsgTitle.andMessageIdEqualTo(orderNotifyListResponse.getMessageId());
criteriaMsgTitle.andPlatformEqualTo(PlatformEnum.PLATFORM_APP.getPlatform());//app
criteriaMsgTitle.andTheTypeEqualTo(filterPlatformNotifyReq.getTheType());//订单通知
criteriaMsgTitle.andMessageTitleLike("%" + filterPlatformNotifyReq.getKeyWord() + "%");//匹配标题
SwsJcSubscriptionMsgExample.Criteria criteriaMsgName = swsJcSubscriptionMsgExample.createCriteria();
criteriaMsgName.andMessageIdEqualTo(orderNotifyListResponse.getMessageId());
criteriaMsgName.andPlatformEqualTo(PlatformEnum.PLATFORM_APP.getPlatform());//app
criteriaMsgName.andTheTypeEqualTo(filterPlatformNotifyReq.getTheType());//订单通知
criteriaMsgName.andSendUserNameLike("%" + filterPlatformNotifyReq.getKeyWord() + "%");//匹配发送人姓名
swsJcSubscriptionMsgExample.or(criteriaMsgName);//or
List<SwsJcSubscriptionMsg> swsJcSubscriptionMsg = swsJcSubscriptionMsgMapper.selectByExample(swsJcSubscriptionMsgExample);
//
<三>:针对上述问题,可以采取手动分页的结果实现,先查出所有符合条件的数据,然后进行手动分页。如下:
int fromIndex = (filterPlatformNotifyReq.getCurrentPage().intValue() - 1) * filterPlatformNotifyReq.getPageSize().intValue();//开始
int toIndex = fromIndex + filterPlatformNotifyReq.getPageSize().intValue();//结束
if (toIndex > Long.valueOf(filterPlatformNotifyResponseList.size())) {
toIndex = filterPlatformNotifyResponseList.size();//超过list大小
}
List<FilterPlatformNotifyResponse> pageList = new ArrayList<>();
if (fromIndex <= filterPlatformNotifyResponseList.size()) {
//当前点击的页面的有数据,返回pageList
pageList = filterPlatformNotifyResponseList.subList(fromIndex, toIndex);
}else{
//当前点击的页面无数据,返回空pageList
}
//返回pageList 为分页后的数据