将 PageDTO 和 PagingAndSortingQueryParams 添加到 plusone-commons。

This commit is contained in:
zhouxy108 2022-11-08 13:52:44 +08:00
parent 0d5eef24e3
commit 7eea5ed7e0
2 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package xyz.zhouxy.plusone.util;
import java.util.List;
import lombok.Setter;
import lombok.ToString;
/**
* 返回分页查询的结果
*
* @param <T> 内容列表的元素类型
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
* @see PagingAndSortingQueryParams
*/
@ToString
@Setter
public class PageDTO<T> {
private Long total;
private List<T> content;
private PageDTO(List<T> content, Long total) {
this.content = content;
this.total = total;
}
public static <T> PageDTO<T> of(List<T> content, Long total) {
return new PageDTO<>(content, total);
}
public Long getTotal() {
return total;
}
public List<T> getContent() {
return content;
}
}

View File

@ -0,0 +1,53 @@
package xyz.zhouxy.plusone.util;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import lombok.Setter;
/**
* 分页排序查询参数
*
* <p>
* 根据传入的 {@code size} {@code pageNum}
* 提供 {@code getOffset} 方法计算 SQL 语句中 {@code offset} 的值
* </p>
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
* @see PageDTO
*/
@Setter
public class PagingAndSortingQueryParams {
protected String orderBy;
protected Integer size;
protected Long pageNum;
private final List<String> sortableColNames;
public PagingAndSortingQueryParams() {
sortableColNames = Collections.emptyList();
}
public PagingAndSortingQueryParams(String... sortableColNames) {
this.sortableColNames = Arrays.asList(sortableColNames);
}
public String getOrderBy() {
return orderBy != null && sortableColNames.contains(orderBy) ? orderBy : null;
}
public int getSize() {
return this.size != null ? this.size : 15;
}
public long getPageNum() {
return this.pageNum != null ? this.pageNum : 1;
}
public long getOffset() {
return (getPageNum() - 1) * getSize();
}
}