请求实体
/*** @author suran* @description 分页实体* @create 2023/11/29 9:31*/
public class PageQuery implements Serializable {private Integer pageNum = 1;private Integer page = 1;private Integer pageSize = 20;public PageQuery() {}public static PageQuery of(int pageNum, int pageSize) {return (new PageQuery()).setPageNum(pageNum).setPage(pageNum).setPageSize(pageSize);}public PageQuery setPageNum(Integer pageNum) {this.pageNum = pageNum;this.page = pageNum;return this;}public PageQuery setPage(Integer page) {this.page = page;this.pageNum = page;return this;}public Integer getPageNum() {return this.pageNum;}public Integer getPage() {return this.page;}public void validate() {this.pageNum = this.pageNum == null ? 1 : this.pageNum;this.page = this.page == null ? 1 : this.page;this.pageSize = this.pageSize == null ? 0 : this.pageSize;this.pageNum = Math.max(this.pageNum, 1);this.page = Math.max(this.page, 1);int finalPage = Math.max(this.pageNum, this.page);this.pageNum = finalPage;this.page = finalPage;this.pageSize = this.pageSize < 1 ? 20 : this.pageSize;}public Integer getOffset() {return (this.pageNum - 1) * this.pageSize;}public String toString() {return "PageQuery{pageNum=" + this.pageNum + ", pageSize=" + this.pageSize + '}';}public Integer getPageSize() {return this.pageSize;}public PageQuery setPageSize(Integer pageSize) {this.pageSize = pageSize;return this;}
}使用时,让原有类继承即可
/*** @author suran* @description 分页实体* @create 2023/11/29 9:31*/public class A extends PageQuery {}
返回实体
/*** @author suran* @description 分页实体* @create 2023/11/29 9:31*/
public class PageVo<T> implements Serializable {private int pageNum;private int page;private int pageSize;private int total;private int totalCount;private int totalPage;private int totalPageCount;private List<T> list;public PageVo() {}public static <T> PageVo<T> of(PageQuery pageQuery) {PageVo<T> emptyPage = new PageVo();emptyPage.setPageNum(pageQuery.getPageNum());emptyPage.setPage(pageQuery.getPageNum());emptyPage.setPageSize(pageQuery.getPageSize());emptyPage.setList(Collections.emptyList());return emptyPage;}public static <T> PageVo<T> of(PageQuery pageQuery, int total, List<T> list) {PageVo<T> vo = new PageVo();vo.setTotal(total);vo.setTotalCount(total);vo.setPageNum(pageQuery.getPageNum());vo.setPage(pageQuery.getPageNum());int pageSize = pageQuery.getPageSize();vo.setPageSize(pageSize);vo.setTotalPage((int)Math.ceil((double)total / (double)pageSize));vo.setTotalPageCount(vo.getTotalPage());vo.setList(list == null ? Collections.emptyList() : list);return vo;}public static <T> PageVo<T> of(PageTo<T> pageTo) {PageVo<T> vo = new PageVo();return vo.setTotal(pageTo.getTotal()).setTotalCount(pageTo.getTotal()).setPageNum(pageTo.getPageNum()).setPage(pageTo.getPageNum()).setPageSize(pageTo.getPageSize()).setTotalPage(pageTo.getTotalPage()).setTotalPageCount(pageTo.getTotalPage()).setList(pageTo.getList() == null ? Collections.emptyList() : pageTo.getList());}public static <T, R> PageVo<T> of(PageTo<R> pageTo, Function<R, T> mapFun) {PageVo<T> vo = new PageVo();return vo.setTotal(pageTo.getTotal()).setTotalCount(pageTo.getTotal()).setPageNum(pageTo.getPageNum()).setPage(pageTo.getPageNum()).setPageSize(pageTo.getPageSize()).setTotalPage(pageTo.getTotalPage()).setTotalPageCount(pageTo.getTotalPage()).setList(pageTo.getList() == null ? Collections.emptyList() : Funs.map(pageTo.getList(), mapFun));}public int getPageNum() {return this.pageNum;}public int getPage() {return this.page;}public int getPageSize() {return this.pageSize;}public int getTotal() {return this.total;}public int getTotalCount() {return this.totalCount;}public int getTotalPage() {return this.totalPage;}public int getTotalPageCount() {return this.totalPageCount;}public List<T> getList() {return this.list;}public PageVo<T> setPageNum(int pageNum) {this.pageNum = pageNum;return this;}public PageVo<T> setPage(int page) {this.page = page;return this;}public PageVo<T> setPageSize(int pageSize) {this.pageSize = pageSize;return this;}public PageVo<T> setTotal(int total) {this.total = total;return this;}public PageVo<T> setTotalCount(int totalCount) {this.totalCount = totalCount;return this;}public PageVo<T> setTotalPage(int totalPage) {this.totalPage = totalPage;return this;}public PageVo<T> setTotalPageCount(int totalPageCount) {this.totalPageCount = totalPageCount;return this;}public PageVo<T> setList(List<T> list) {this.list = list;return this;}
}