zoukankan      html  css  js  c++  java
  • mybatis分页插件 PageHelper 的简单使用

    PageHelper 是 mybatis 框架中一个分页插件,是用于帮助我们从数据库中查询分页的数据。使用步骤如下:

    1. 添加 pom 依赖

    <!--mybatis分页插件-->
    <dependency>
      <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.7</version> </dependency>

    2. 直接调用方法

    调用方法: PageHelper.startPage(int pageNum, int pageSize); 该方法接收两个参数,其中 pageNum 为当前页码数,pageSize 为每页的条数。

    这个方法放在 Controller 层,必须要放在查询所有条数的代码的上一行。否则不起作用。

    例如下面的代码:
    @GetMapping("/getPageDocsByState")
    public Result<List<Document>> getAllPublishedDocs(Integer state,
    Integer currentPage,
    Integer pageSize){
    Result<List<Document>> result = new Result<>();
    if (state != null) {
    // mybatis 分页插件,必须放在查询所有的上一行
    PageHelper.startPage(currentPage, pageSize);
    List<Document> list = docService.findAllDocsByState(state);
    result.setData(list);
    }else {
    result.setMsg("error");
    }

    return result;
    }
    这行代码: List<Document> list = docService.findAllDocsByState(state); 是查询符合条件的所有记录,加上 PageHelper.startPage() 后每次返回的将是一页数据,而不是全部。
  • 相关阅读:
    urllib.request.urlretrieve()
    python2.X与python3.X爬虫常用的模块变化对应
    .net 发布程序时出现“类型ASP.global_asax同时存在于...”错误的解决办法
    批量引用iconfont字体图标到项目
    动态设置bootstrapswitch状态
    MD5加密过时方法替换
    SQL语句
    PHP中的闭包
    算法复杂度
    快速排序
  • 原文地址:https://www.cnblogs.com/luler/p/14120805.html
Copyright © 2011-2022 走看看