PageHelper的使用方式

作者: gavin 分类: Java,Java 基础 发布时间: 2022-02-11 07:36

Step 1 => 添加依赖

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.13</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Step 2 => 创建翻页实体类(req)

public class PageReq {
    private int page=1;

    private int size=10;

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

}

Step 3 => 在需要带翻页功能的实体类上做关系继承

public class CitiaoReq extends PageReq{}

Step 4 => service层代码

Page page = PageHelper.startPage(req.getPage(),req.getSize());
citiaoExample.setOrderByClause("rand()"); //随机排序
List<Citiao> list = citiaoMapper.selectByExample(citiaoExample);
LOG.info("总行数:{}", page.getTotal());
LOG.info("总页数:{}", page.getPages());