PageHelper 分頁無效的坑。。。

1.問題

阿里巴巴Java開發手冊:java

1.1.PageHelper先開啓分頁,後對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;
}

能夠分頁,可是數據量錯誤,total始終等於每頁數據量,即pageSizegit

1.2.先對list數據進行操做,後開啓分頁

@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();
        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);
    }
    PageHelper.startPage(pageNo,pageSize);
    PageInfo<HdQueryVo> pageViewInfo = new PageInfo<>(hdQueryVos);

    return pageViewInfo;
}

數據能夠查詢出來,總數total也正確,可是分頁功能失效。(這份阿里 Java 開發手冊能夠關注公衆號Java核心技術,回覆:手冊)面試

2.緣由

PageHelper中startPage開啓分頁方法只對後面的sql查詢起做用spring

1.1 錯誤緣由是提早開啓分頁後,對list操做,即PageInfo pageViewInfo = new PageInfo<>(hdQueryVos);sql

/**
* 包裝Page對象
*
* @param list
*/
public PageInfo(List list) {
    this(list, 8);
}

只是把list轉爲PageInfo對象,不影響前面分頁數據的操做api

1.2 錯誤緣由是先對list操做後,開啓翻頁後沒有sql語句intellij-idea

即sql語句沒有參與分頁查詢app

3.解決方案

直接對分頁後的PageInfo對象中的數據進行操做ide

  • 對list集合操做,先取出PageInfo裏的list集合數據,再對數據進行相關操做
  • 將操做完後的list集合再次存到PageInfo裏,進行return

@Override
public PageInfo<HdQueryVo> getRecordsByView(int pageNo, int pageSize) {
    PageInfo<HdQueryVo> source = PageHelper.startPage(pageNo, pageSize).doSelectPageInfo(()->{
        actionMapper.getActionByView();
    });
    // 須要轉換的對象
    PageInfo<HdQueryVo> target = new PageInfo<>();
    // 複製分頁屬性
    BeanUtils.copyProperties(source, target);
    // 對查詢的list進行下一步操做,好比類型轉換後
    List<HdQueryVo> collect = source.getList().stream().collect(Collectors.toList());
    List<HdQueryVo> hdQueryVos = new ArrayList<>();

    for (HdQueryVo hdQueryVo : collect) {
        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
    target.setList(hdQueryVos);

    return target;
}

參考資料:工具

https://apidoc.gitee.com/free...

https://blog.csdn.net/shangle...

原文連接:https://blog.csdn.net/qq_3508...

版權聲明:本文爲CSDN博主「He Ain't Hero」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連接及本聲明。

近期熱文推薦:

1.600+ 道 Java面試題及答案整理(2021最新版)

2.終於靠開源項目弄到 IntelliJ IDEA 激活碼了,真香!

3.阿里 Mock 工具正式開源,幹掉市面上全部 Mock 工具!

4.Spring Cloud 2020.0.0 正式發佈,全新顛覆性版本!

5.《Java開發手冊(嵩山版)》最新發布,速速下載!

以爲不錯,別忘了隨手點贊+轉發哦!

相關文章
相關標籤/搜索