在SpringBoot中使用PageHelper快速实现分页
一.添加依赖及配置
<!-- 若是 pom.xml 文件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
# 若是application.yml文件
pagehelper:
helper-dialect: mysql
params: count=countSql
reasonable: true
support-methods-arguments: true
二.实体类
@Getter
@Setter
@Entity
public class User {
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer age;
}
三.Mapper
@Mapper
public interface UserMapper {
@Select("SELECT * FROM USER")
Page<User> getUserList();
}
四.Controller
@PostMapping (value = "/searchh")
@ResponseBody
public Page<User> getall(@RequestParam(value = "page", required = false, defaultValue = "1") int page, @RequestParam(value = "pageSize", required = false, defaultValue = "2") int pageSize){
PageHelper.startPage(page,pageSize);
Page<User> userlist = userMapper.getUserList();
return userlist;
}
五.验证
数据库里是20条数据,我用postman 发的参数是: page=1,pageSize=5,即显示第1页,总共分为5页,显示如下: