当前位置:网站首页>PageHelper还能结合Lambda表达式实现简洁的分页封装
PageHelper还能结合Lambda表达式实现简洁的分页封装
2022-07-25 17:42:00 【一恍过去】
1、引入POM
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
2、封装PageBean
创建查询分页类BaseQueryParam,其需要传递的查询参数类,可以直接进行继承,代码如下:
@Data
public class BaseQueryParam {
/** * 当前页码 */
private Integer pageNum;
/** * 每页数量 */
private Integer pageSize;
}
封装新的PageBean,利用泛型封装统一的请求方法与响应分页信息参数,代码如下:
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
public class NewPageBean<T> {
private Long total;
private List<T> list;
private MyPage pageInfo;
/** * @param select 调用Mapper接口的结果 * @param param 请求参数 * @param tClass 响应的Class对象 * @param <T> * @return */
public static <T> NewPageBean<T> generatePage(ISelect select, BaseQueryParam param, Class<T> tClass) {
Integer current = param.getPageNum();
Integer pageSize = param.getPageSize();
List<T> list = PageHelper
.startPage(current == null ? 1 : current, pageSize == null ? 10 : pageSize)
.doSelectPage(select);
return new NewPageBean<>(list);
}
public NewPageBean(List<T> list) {
PageInfo<T> pageInfo = new PageInfo<>(list);
this.total = pageInfo.getTotal();
this.list = list;
MyPage page = new MyPage();
page.setTotalPage(pageInfo.getPages());
page.setPageNum(pageInfo.getPageNum());
page.setPageSize(pageInfo.getPageSize());
page.setPreviousPage(pageInfo.getPrePage());
page.setNextPage(pageInfo.getNextPage());
this.pageInfo = page;
}
public long getNum() {
return this.total;
}
public void setNum(Long total) {
this.total = total;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public MyPage getPageInfo() {
return pageInfo;
}
public void setPageInfo(MyPage pageInfo) {
this.pageInfo = pageInfo;
}
static class MyPage {
private int pageNum;
private int pageSize;
private int totalPage;
private int nextPage;
private int previousPage;
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 int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public int getPreviousPage() {
return previousPage;
}
public void setPreviousPage(int previousPage) {
this.previousPage = previousPage;
}
}
}
3、用法
3.1 定义Mapper
@Mapper
public interface TaskMapper {
List<Task> list(BaseQueryParam param);
}
3.2 定义Mapping
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.TaskMapper">
<select id="list" parameterType="com.lhz.controller.page.BaseQueryParam" resultMap="BaseResultMap">
select
id,name,create_by,create_time,update_by,update_time,remark
from sys_task
</select>
</mapper>
3.3 具体实现
此处不写servce,层,方便演示直接在Controller中调用Mapper
@RestController
public class TaskController {
@Resource
private TaskMapper taskMapper;
@GetMapping("test")
public Object test(BaseQueryParam param) {
/** * BaseQueryParam 就是查询的参数 * Task 为响应的实体 */
return NewPageBean.generatePage(() -> taskMapper.list(param), param, Task.class);
}
}
4、测试
测试url:http://localhost:9090/demo/test?pageNum=2&pageSize=2
SQL预览:
返回结果:
{
"total":3,
"list":[
{
"id":"1",
"name":"定时任务一",
"createBy":"1",
"createTime":1601178428000,
"updateBy":"1",
"updateTime":1601178428000,
"remark":""
},
{
"id":"2",
"name":"定时任务二",
"createBy":"1",
"createTime":1601178546000,
"updateBy":"1",
"updateTime":1601178546000,
"remark":""
}
],
"pageInfo":{
"pageNum":1,
"pageSize":2,
"totalPage":2,
"nextPage":2,
"previousPage":0
},
"num":3
}
边栏推荐
- Which one of the electronic products has a longer service life??
- OSPF --- open shortest priority path protocol
- Automated test Po design model
- 02. Add two numbers
- Take you to a preliminary understanding of multiparty secure computing (MPC)
- 我也是醉了,Eureka 延迟注册还有这个坑!
- 理财有保本产品吗?
- 生成扩散模型漫谈:DDPM = 贝叶斯 + 去噪
- HCIP第一天实验
- Go language context control function execution timeout return
猜你喜欢

食品安全 | 八问八答带你重新认识小龙虾!这样吃才对!

超越 ConvNeXt、RepLKNet | 看 51×51 卷积核如何破万卷!

Three dimensional function display of gray image

An article about ultrasonic humidifier

8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇

面试官:说说 log.Fatal 和 panic 的区别

With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development

我也是醉了,Eureka 延迟注册还有这个坑!

Stm32 paj7620u2 gesture recognition module (IIC communication) program source code explanation

"Digital security" alert NFT's seven Scams
随机推荐
关于flickr的数据集笔记
go接口变量的类型断言
I'm also drunk. Eureka delayed registration and this pit!
go channel简单笔记
无聊发文吐槽工作生活
Calculation date or date formatting
我们被一个 kong 的性能 bug 折腾了一个通宵
吴恩达机器学习编程作业无法暂停pause问题解决
I2C通信——时序图
Crawler framework crawler
接口自动化测试Postman+Newman+Jenkins
基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
Redis源码与设计剖析 -- 16.AOF持久化机制
栈的顺序存储结构,链式存储结构及实现
【PHP伪协议】源码读取、文件读写、任意php命令执行
Three dimensional function display of gray image
更新|3DCAT实时云渲染 v2.1.2版本全新发布
Enumeration classes and magic values
Interface automation test postman+newman+jenkins
03.无重复字符的最长子串