PageInfo 工具类

package com.jsy.basic.util;


import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class PageInfo<T> implements Serializable {
	private static final long serialVersionUID = 8545991263226528798L;
	
	@ApiModelProperty(value = "每页数据")
	private List<T> records;
	
	@ApiModelProperty(value = "总记录数")
	private long total;
	
	@ApiModelProperty(value = "每页记录数")
	private long size;
	
	@ApiModelProperty(value = "当前页")
	private long current;
	
	@ApiModelProperty(value = "附加数据")
	private Map<String,Object> extra;
	
	public PageInfo() {
		this.records = Collections.emptyList();
		this.total = 0L;
		this.size = 10L;
		this.current = 1L;
		
	}
	
	public PageInfo(long current, long size) {
		this(current, size, 0L);
	}
	
	public PageInfo(long current, long size, long total) {
		this(current, size, total, true);
	}
	
	public PageInfo(long current, long size, boolean isSearchCount) {
		this(current, size, 0L, isSearchCount);
	}
	
	public PageInfo(long current, long size, long total, boolean isSearchCount) {
		this.records = Collections.emptyList();
		this.total = 0L;
		this.size = 10L;
		this.current = 1L;
		if (current > 1L) {
			this.current = current;
		}
		
		this.size = size;
		this.total = total;
	}
	
	
	public List<T> getRecords() {
		return this.records;
	}
	
	public PageInfo<T> setRecords(List<T> records) {
		this.records = records;
		return this;
	}
	
	public long getTotal() {
		return this.total;
	}
	
	public PageInfo<T> setTotal(long total) {
		this.total = total;
		return this;
	}
	
	public long getSize() {
		return this.size;
	}
	
	public PageInfo<T> setSize(long size) {
		this.size = size;
		return this;
	}
	
	public long getCurrent() {
		return this.current;
	}
	
	public PageInfo<T> setCurrent(long current) {
		this.current = current;
		return this;
	}

	public Map<String, Object> getExtra() {
		return extra;
	}

	public void setExtra(Map<String, Object> extra) {
		this.extra = extra;
	}
}

PageInfo 是一个用于辅助处理分页查询结果的工具类,以下是其功能介绍和使用方法: ### 功能介绍 PageInfo 类主要用于封装分页查询的相关信息,方便前端展示和处理分页数据。其提供的常用信息有: - `list`:结果集,即当前页查询到的数据列表。 - `pageNum`:当前页码。 - `pageSize`:当前页面显示的数据条目数。 - `pages`:总页数。 - `total`:数据的总条目数。 - `prePage`:上一页的页码。 - `nextPage`:下一页的页码。 - `isFirstPage`:判断当前页是否为第一页。 - `isLastPage`:判断当前页是否为最后一页。 - `hasPreviousPage`:判断是否有上一页。 - `hasNextPage`:判断是否有下一页。 此外,PageInfo 类还提供了一些其他方法,如 `navigateFirstPage()`、`navigateLastPage()`、`navigatePages()` 等,可根据具体需求选择使用 [^1][^2]。 ### 使用方法 #### 1. 设置分页信息 使用 `PageHelper.startPage` 方法设置分页的位置和每页展示的数据条目数,该方法需要传入两个参数,分别是页数和一页的条目数量。需要注意的是,要和前端沟通参数的传递方式,如果是通过请求体传递参数,还需将参数分离出来 [^2][^3]。 示例代码如下: ```java PageHelper.startPage(1, 10); // 设置当前页码为 1,每页显示 10 条数据 ``` #### 2. 执行查询操作 调用 `xxxMapper.函数()` 进行数据查询,返回的是类型为 `Page` 的一系列 `xxEntity`,查询结果存放在 `page` 容器中 [^3]。 示例代码如下: ```java Page<xxEntity> page = xxxMapper.queryData(); // 执行查询操作 ``` #### 3. 封装分页信息 新定义一个 `PageInfo` 类型的变量 `pageInfo` 用于返回分页信息,在 `new` 的过程中,直接将查询结果 `page` 放在构造函数的括号中,此时的 `pageInfo` 就是需要的返回结果 [^3]。 示例代码如下: ```java PageInfo<xxEntity> pageInfo = new PageInfo<>(page); // 封装分页信息 ``` #### 4. DTO 转 VO 当 DTO 层返回分页数据,需要将 DTO 转成 VO 返回给用户视图时,可以使用以下工具类方法进行转换 [^4]。 ```java public static <VO, DTO> PageInfo<VO> dto2Vo (PageInfo<DTO> source, Function<DTO,VO> convert) { if (source != null) { Page<VO> dest = new Page<>(source.getPageNum(), source.getPageSize()); dest.setTotal(source.getTotal()); PageInfo<VO> result = new PageInfo<>(dest); if (source.getList()!=null && !source.getList().isEmpty()) { ArrayList<VO> voList = new ArrayList<>(); for (DTO dto : source.getList()) { voList.add(convert.apply(dto)); } result.setList(voList); } return result; } return null; } ``` ### 示例代码 ```java import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import java.util.ArrayList; import java.util.function.Function; // 假设 xxEntity 是查询结果的实体类 class xxEntity { // 实体类的属性和方法 } // 假设 xxMapper 是数据访问层的接口 interface xxMapper { Page<xxEntity> queryData(); } public class PageInfoExample { public static void main(String[] args) { // 设置分页信息 PageHelper.startPage(1, 10); // 模拟查询操作 xxMapper mapper = new xxMapper() { @Override public Page<xxEntity> queryData() { // 实际应用中这里会执行数据库查询操作 return null; } }; Page<xxEntity> page = mapper.queryData(); // 封装分页信息 PageInfo<xxEntity> pageInfo = new PageInfo<>(page); // DTO 转 VO 示例 PageInfo<xxEntity> dtoPageInfo = new PageInfo<>(page); PageInfo<xxEntity> voPageInfo = dto2Vo(dtoPageInfo, dto -> { // 实现 DTO 到 VO 的转换逻辑 return dto; }); } public static <VO, DTO> PageInfo<VO> dto2Vo (PageInfo<DTO> source, Function<DTO,VO> convert) { if (source != null) { Page<VO> dest = new Page<>(source.getPageNum(), source.getPageSize()); dest.setTotal(source.getTotal()); PageInfo<VO> result = new PageInfo<>(dest); if (source.getList()!=null && !source.getList().isEmpty()) { ArrayList<VO> voList = new ArrayList<>(); for (DTO dto : source.getList()) { voList.add(convert.apply(dto)); } result.setList(voList); } return result; } return null; } } ``` ### 总结 通过以上步骤,可以使用 PageInfo 工具类方便地实现分页查询和分页信息的封装,同时支持 DTO 到 VO 的转换。在实际应用中,可根据具体需求选择使用 PageInfo 类提供的其他方法。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员小小刘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值