介绍
在项目开发中,一般查询列表都需要分页展示
二,实现过程和示例代码
(1)Maven引包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
(2)代码实现
- Controller层
@GetMapping("testpage/{size}/{page}")
public Result<PageInfo<User>> userList(@PathVariable("page") Integer page,
@PathVariable("size") Integer size){
PageInfo<User> pageInfo = userService.findByPage(page, size);
return ResultUtil.success(pageInfo);
}
- Service层
@Override
public PageInfo<User> findByPage(Integer page ,Integer size){
PageHelper.startPage(page, size);
List<User> userList = userMapper.findAll();
PageInfo<User> pageInfo = new PageInfo<>(userList);
return pageInfo;
}
- Mapper层
@Select("select * from user")
List<User> findAll();