springboot+mybatis分页插件(pagehelper)

pom依赖:

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

定义分页实体类:

package com.ds.entity;

import com.github.pagehelper.PageHelper;

import java.io.Serializable;

public class SqlPage implements Serializable {

    private Integer index;
    private Integer num;

    public SqlPage() {
    }

    public SqlPage(Integer index, Integer num) {
        this.index = index==null?1:index;
        this.num = num==null?10:num;
        PageHelper.startPage(this.num,this.index);//初始化分页数据
    }

    public SqlPage(String index, String num) {
        this.index = index==null?1:Integer.valueOf(index);
        this.num = num==null?10:Integer.valueOf(num);
        PageHelper.startPage(this.num,this.index);//初始化分页数据
    }

    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }
}

使用分页:

 @RequestMapping("/getAllCzyb")
    public RespBean getAllCzyb(@RequestBody SqlPage sqlPage){
        new SqlPage(sqlPage.getIndex(),sqlPage.getNum());
        return RespBean.sucess("",this.czybService.getAllCzyb());
    }

注意:分页插件不需要单独的写方法使用,只需要在执行sql,之前执行PageHelper.startPage(this.num,this.index);//初始化分页数据就可以。

application.yml文件配置

#分页插件
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

猜你喜欢

转载自blog.csdn.net/Denial_learn/article/details/113841690