forked from plusone/plusone-commons
将 PageDTO 和 PagingAndSortingQueryParams 添加到 plusone-commons。
This commit is contained in:
parent
0d5eef24e3
commit
7eea5ed7e0
40
src/main/java/xyz/zhouxy/plusone/util/PageDTO.java
Normal file
40
src/main/java/xyz/zhouxy/plusone/util/PageDTO.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user