概述
上文中已经介绍了Spring和MyBatis的整合,在上文的基础上我们加入了PageHelper这个插件,来实现MyBatis列表查询的分页效果
PageHelper是啥
PageHelper是一个强大实用的 MyBatis 分页插件,可以帮助我们快速的实现MyBatis分页功能,对MyBatis毫无侵入,只是对MyBatis的执行进行了拦截来实现分页效果。
**PageHelper 开源仓库 *
https://github.com/pagehelper/Mybatis-PageHelper
开始使用PageHelper
1.在pom.xml文件中加入PageHelper依赖,并在yml配置文件中加入分页配置
<!-- pagehelper 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
yml配置
# PageHelper分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
- 创建工具包com.zhlab.demo.utils、com.zhlab.demo.utils.page,并创建相关类
- 页请求参数类:PageRequest.java
- 分页结果返回类:PageResult.java
- 分页工具类:PageUtil.java
/************************PageRequest.java************************/
package com.zhlab.demo.utils.page;
/**
* @ClassName PageRequest
* @Description //分页请求参数类
* @Author singleZhang
* @Email 405780096@qq.com
* @Date 2020/10/31 0031 上午 11:00
**/
public class PageRequest {
/**
* 当前页码
*/
private int pageNum;
/**
* 每页数量
*/
private int pageSize;
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
/************************PageResult.java************************/
package com.zhlab.demo.utils.page;
import java.util.List;
/**
* @ClassName PageResult
* @Description //分页结果返回类
* @Author singleZhang
* @Email 405780096@qq.com
* @Date 2020/10/31 0031 上午 11:00
**/
public class PageResult {
/**
* 当前页码
*/
private int pageNum;
/**
* 每页数量
*/
private int pageSize;
/**
* 记录总数
*/
private long totalSize;
/**
* 页码总数
*/
private int totalPages;
/**
* 返回的各类数据模型
*/
private List<?> content;
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public long getTotalSize() {
return totalSize;
}
public void setTotalSize(long totalSize) {
this.totalSize = totalSize;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public List<?> getContent() {
return content;
}
public void setContent(List<?> content) {
this.content = content;
}
}
/************************PageUtil.java************************/
package com.zhlab.demo.utils;
import com.github.pagehelper.PageInfo;
import com.zhlab.demo.utils.page.PageResult;
/**
* @ClassName PageUtil
* @Description //分页工具类
* @Author singleZhang
* @Email 405780096@qq.com
* @Date 2020/10/31 0031 上午 11:03
**/
public class PageUtil {
/**
* 将分页信息封装到统一的接口
* @param pageInfo
* @return
*/
public static PageResult getPageResult(PageInfo<?> pageInfo) {
PageResult pageResult = new PageResult();
pageResult.setPageNum(pageInfo.getPageNum());
pageResult.setPageSize(pageInfo.getPageSize());
pageResult.setTotalSize(pageInfo.getTotal());
pageResult.setTotalPages(pageInfo.getPages());
pageResult.setContent(pageInfo.getList());
return pageResult;
}
}
- 在接口层UserController.java中新增分页测试方法findPage(PageRequest pageRequest)
package com.zhlab.demo.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhlab.demo.model.SysAdminUser;
import com.zhlab.demo.service.SysAdminUserService;
import com.zhlab.demo.utils.PageUtil;
import com.zhlab.demo.utils.page.PageRequest;
import com.zhlab.demo.utils.page.PageResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @ClassName UserController
* @Description //用户接口层
* @Author singleZhang
* @Email 405780096@qq.com
* @Date 2020/10/31 0031 上午 9:43
**/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
SysAdminUserService sysAdminUserService;
/* 方法注解 */
@ApiOperation(value = "方法名:用户列表", notes = "获取用户列表")
@GetMapping("/list")
public List<SysAdminUser> list(){
List<SysAdminUser> list = sysAdminUserService.findAll();
return list;
}
@ApiOperation(value = "方法名:用户列表分页", notes = "获取分页用户列表")
@PostMapping(value="/findPage")
public PageResult findPage(@RequestBody PageRequest pageRequest) {
startPage(pageRequest); //开启分页,pageHelp会自动拦截mybatis的sql
List<SysAdminUser> list = sysAdminUserService.findAll();
return PageUtil.getPageResult(new PageInfo<>(list));
}
/**
* 启动分页
* */
private void startPage(PageRequest pageRequest){
PageHelper.startPage(pageRequest.getPageNum() == 0?1:pageRequest.getPageNum(),
pageRequest.getPageSize()==0?5:pageRequest.getPageSize());
}
}
-
完事了?对!已经完事了,就这么简单,来看看执行效果吧
接口 -
输入参数,调试接口
返回数据
总结
PageHelper的整合还是比较简单的,如果想更深入的了解,它还可以玩出花来,以后慢慢研究,加油打工人!
网友评论