zoukankan      html  css  js  c++  java
  • PageHelper分页插件的使用注意点

    PageHelper是一种分页插件,下面介绍在实际业务的不同情况下使用需要注意的地方。

    1. 依赖导入

    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>5.1.4</version>
    </dependency>
    

    2. 分页插件的使用

    2.1 对查询语句的结果进行分页

    一般情况下是使用PageHelper插件对查询语句的结果进行分页,具体实现如下:

    PageHelper.startPage(pageNum, pageSize);
    Page<TestDemo> testDemoPageData = (Page<TestDemo>) testDemoMapper.selectByExample(testDemoExample);
    List<TestDemo> testDemoResults = testDemoPageData.getResult();
    

    2.2 对已存在的列表进行分页

    然而,实际业务中可能需要对多表查询的结果进行聚合,然后存储到List集合中,这种情况下则需要进行手动分页。

    // 已有的List集合:List<TestDemo> profiles
    Page<TestDemo> page = new Page<>(pageNum, pageSize);
    page.setTotal(profiles.size());
    int startIndex = (pageNum - 1) * pageSize;
    int endIndex = Math.min(startIndex + pageSize, profiles.size());
    page.addAll(profiles.subList(startIndex, endIndex));
    List<TestDemo> testDemoResults = page.getResult();
    

    3. 总结

    在实际业务中使用PageHelper分页插件要根据具体需求具体使用。

  • 相关阅读:
    mybatis
    Spring原理
    JS 之继承
    HTTP协议简介2
    JS 之原型,实例,构造函数之间的关系
    HTTP协议简介1
    freemarker语法简介
    CSS 动画之十-图片+图片信息展示
    JS实现颜色值的转换
    抓包工具charles的使用
  • 原文地址:https://www.cnblogs.com/chiaki/p/14417285.html
Copyright © 2011-2022 走看看