feat(extension/crud): PageQuery 和 SortQuery 完善带参构造方法

This commit is contained in:
2025-05-14 20:19:56 +08:00
parent 3bbd04add2
commit 70f8fc01c0
2 changed files with 172 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Min;
import org.hibernate.validator.constraints.Range; import org.hibernate.validator.constraints.Range;
import org.springdoc.core.annotations.ParameterObject; import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.domain.Sort;
import java.io.Serial; import java.io.Serial;
@@ -35,10 +36,12 @@ public class PageQuery extends SortQuery {
@Serial @Serial
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** /**
* 默认页码1 * 默认页码1
*/ */
private static final int DEFAULT_PAGE = 1; private static final int DEFAULT_PAGE = 1;
/** /**
* 默认每页条数10 * 默认每页条数10
*/ */
@@ -58,6 +61,123 @@ public class PageQuery extends SortQuery {
@Range(min = 1, max = 1000, message = "每页条数(取值范围 {min}-{max}") @Range(min = 1, max = 1000, message = "每页条数(取值范围 {min}-{max}")
private Integer size = DEFAULT_SIZE; private Integer size = DEFAULT_SIZE;
public PageQuery() {
}
/**
* 构造方法
*
* <p>
* 示例:{@code new PageQuery(1, 10)}
* </p>
*
* @param page 页码
* @param size 每页条数
* @since 2.12.0
*/
public PageQuery(Integer page, Integer size) {
this.page = page;
this.size = size;
}
/**
* 构造方法
*
* <p>
* 示例:{@code new PageQuery(1, 10, "createTime,desc", "name,asc")}
* </p>
*
* @param page 页码
* @param size 每页条数
* @param sort 排序
* @since 2.12.0
*/
public PageQuery(Integer page, Integer size, String... sort) {
super(sort);
this.page = page;
this.size = size;
}
/**
* 构造方法
*
* <p>
* 示例:{@code new PageQuery("createTime,desc", "name,asc")}
* </p>
*
* @param sort 排序
* @since 2.12.0
*/
public PageQuery(String... sort) {
super(sort);
}
/**
* 构造方法
*
* <p>
* 示例:{@code new PageQuery("createTime", Sort.Direction.DESC)}
* </p>
*
* @param field 字段
* @param direction 排序方向
* @since 2.12.0
*/
public PageQuery(String field, Sort.Direction direction) {
super(field, direction);
}
/**
* 构造方法
*
* <p>
* 示例:{@code new PageQuery(Sort.by(Sort.Direction.DESC, "createTime"))}
* </p>
*
* @param sort 排序
* @since 2.12.0
*/
public PageQuery(Sort sort) {
super(sort);
}
/**
* 构造方法
*
* <p>
* 示例:{@code new PageQuery(1, 10, "createTime", Sort.Direction.DESC)}
* </p>
*
* @param page 页码
* @param size 每页条数
* @param field 字段
* @param direction 排序方向
* @since 2.12.0
*/
public PageQuery(Integer page, Integer size, String field, Sort.Direction direction) {
super(field, direction);
this.page = page;
this.size = size;
}
/**
* 构造方法
*
* <p>
* 示例:{@code new PageQuery(1, 10, Sort.by(Sort.Direction.DESC, "createTime"))}
* </p>
*
* @param page 页码
* @param size 每页条数
* @param sort 排序
* @since 2.12.0
*/
public PageQuery(Integer page, Integer size, Sort sort) {
super(sort);
this.page = page;
this.size = size;
}
public Integer getPage() { public Integer getPage() {
return page; return page;
} }

View File

@@ -47,6 +47,58 @@ public class SortQuery implements Serializable {
@Schema(description = "排序条件", example = "createTime,desc") @Schema(description = "排序条件", example = "createTime,desc")
private String[] sort; private String[] sort;
public SortQuery() {
}
/**
* 构造方法
*
* <p>
* 示例:{@code new SortQuery("createTime,desc", "name,asc")}
* </p>
*
* @param sort 排序条件
* @since 2.12.0
*/
public SortQuery(String... sort) {
this.sort = sort;
}
/**
* 构造方法
*
* <p>
* 示例:{@code new SortQuery("createTime", Sort.Direction.DESC)}
* </p>
*
* @param field 字段
* @param direction 排序方向
* @since 2.12.0
*/
public SortQuery(String field, Sort.Direction direction) {
this(field + StringConstants.COMMA + direction.name().toLowerCase());
}
/**
* 构造方法
*
* <p>
* 示例:{@code new SortQuery(Sort.by(Sort.Order.desc("createTime")))}
* </p>
*
* @param sort 排序条件
* @since 2.12.0
*/
public SortQuery(Sort sort) {
if (sort == null || sort.isUnsorted()) {
this.sort = null;
return;
}
this.sort = sort.stream()
.map(order -> order.getProperty() + StringConstants.COMMA + order.getDirection().name().toLowerCase())
.toArray(String[]::new);
}
/** /**
* 解析排序条件为 Spring 分页排序实体 * 解析排序条件为 Spring 分页排序实体
* *