关于java:PageHelper-分页无效的坑

44次阅读

共计 4717 个字符,预计需要花费 12 分钟才能阅读完成。

1. 问题

阿里巴巴 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 始终等于每页数据量,即 pageSize

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 查问起作用

1.1 谬误起因是提前开启分页后,对 list 操作,即 PageInfo pageViewInfo = new PageInfo<>(hdQueryVos);

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

只是把 list 转为 PageInfo 对象,不影响后面分页数据的操作

1.2 谬误起因是先对 list 操作后,开启翻页后没有 sql 语句

即 sql 语句没有参加分页查问

3. 解决方案

间接对分页后的 PageInfo 对象中的数据进行操作

  • 对 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 开发手册(嵩山版)》最新公布,速速下载!

感觉不错,别忘了顺手点赞 + 转发哦!

正文完
 0