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
本文介绍了如何在Spring Boot项目中利用PageHelper插件进行MyBatis的动态分页。通过引入PageHelper及其相关依赖,配合简单的实体类SqlPage,可以在执行SQL前初始化分页数据。在application.yml中配置分页插件参数,无需额外的分页方法,即可实现便捷的分页查询功能。
884

被折叠的 条评论
为什么被折叠?



