基于mybatis-plus分页封装
This commit is contained in:
parent
9db1311cf9
commit
28e15fe975
@ -1,11 +1,13 @@
|
|||||||
package cn.axzo.framework.autoconfigure.validation;
|
package cn.axzo.framework.autoconfigure.validation;
|
||||||
|
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
|
||||||
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
|
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
|
||||||
|
import org.springframework.boot.validation.beanvalidation.MethodValidationExcludeFilter;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
@ -28,7 +30,10 @@ public class MethodValidationAutoConfiguration {
|
|||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public MethodValidationPostProcessor methodValidationPostProcessor(Environment environment, Validator validator) {
|
public MethodValidationPostProcessor methodValidationPostProcessor(Environment environment, Validator validator) {
|
||||||
val processor = ValidationAutoConfiguration.methodValidationPostProcessor(environment, validator, null);
|
MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
|
||||||
|
boolean proxyTargetClass = (Boolean)environment.getProperty("spring.aop.proxy-target-class", Boolean.class, true);
|
||||||
|
processor.setProxyTargetClass(proxyTargetClass);
|
||||||
|
processor.setValidator(validator);
|
||||||
processor.setBeforeExistingAdvisors(true);
|
processor.setBeforeExistingAdvisors(true);
|
||||||
return processor;
|
return processor;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,8 +11,8 @@ cn.axzo.framework.autoconfigure.data.IdAutoConfiguration,\
|
|||||||
cn.axzo.framework.autoconfigure.web.cors.CorsAutoConfiguration,\
|
cn.axzo.framework.autoconfigure.web.cors.CorsAutoConfiguration,\
|
||||||
cn.axzo.framework.autoconfigure.web.PageWebAutoConfiguration,\
|
cn.axzo.framework.autoconfigure.web.PageWebAutoConfiguration,\
|
||||||
cn.axzo.framework.autoconfigure.web.exception.ExceptionHandlerAutoConfiguration,\
|
cn.axzo.framework.autoconfigure.web.exception.ExceptionHandlerAutoConfiguration,\
|
||||||
# cn.axzo.framework.autoconfigure.web.swagger.SwaggerAutoConfiguration,\
|
|
||||||
cn.axzo.framework.autoconfigure.validation.SpringValidatorAutoConfiguration,\
|
cn.axzo.framework.autoconfigure.validation.SpringValidatorAutoConfiguration,\
|
||||||
cn.axzo.framework.autoconfigure.web.advice.BodyAdviceAutoConfiguration,\
|
cn.axzo.framework.autoconfigure.web.advice.BodyAdviceAutoConfiguration,\
|
||||||
cn.axzo.framework.autoconfigure.web.FilterAutoConfiguration,\
|
cn.axzo.framework.autoconfigure.web.FilterAutoConfiguration,\
|
||||||
cn.axzo.framework.autoconfigure.web.context.WebMvcAwareAutoConfiguration
|
cn.axzo.framework.autoconfigure.web.context.WebMvcAwareAutoConfiguration
|
||||||
|
# cn.axzo.framework.autoconfigure.web.swagger.SwaggerAutoConfiguration,\
|
||||||
|
|||||||
@ -97,5 +97,10 @@
|
|||||||
<artifactId>mybatis-plus-core</artifactId>
|
<artifactId>mybatis-plus-core</artifactId>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-extension</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
@ -1,6 +1,5 @@
|
|||||||
package cn.axzo.framework.domain.page;
|
package cn.axzo.framework.domain.page;
|
||||||
|
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|||||||
@ -0,0 +1,49 @@
|
|||||||
|
package cn.axzo.framework.domain.page;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liyong.tian
|
||||||
|
* @Date: 2022/10/28
|
||||||
|
* @Description: 基于mybatis-plus封装分页请求类
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class PageQO {
|
||||||
|
|
||||||
|
@ApiModelProperty("当前页")
|
||||||
|
private Long page = 1L;
|
||||||
|
|
||||||
|
@ApiModelProperty("每页大小")
|
||||||
|
private Long pageSize = 10L;
|
||||||
|
|
||||||
|
//排序字段
|
||||||
|
private String sortType;
|
||||||
|
//排序类型,(desc 降序,asc 升序)
|
||||||
|
private String sortDirection;
|
||||||
|
|
||||||
|
//是否查询详情
|
||||||
|
private boolean needDetail = true;
|
||||||
|
|
||||||
|
public PageQO() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> Page<T> toPage() {
|
||||||
|
if (this.page != null && this.page == -1L) {
|
||||||
|
this.page = 1L;
|
||||||
|
this.pageSize = 9999L;
|
||||||
|
return new Page(this.page, this.pageSize);
|
||||||
|
} else {
|
||||||
|
if (this.page == null || this.page == 0L) {
|
||||||
|
this.page = 1L;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.pageSize == null || this.pageSize == 0L) {
|
||||||
|
this.pageSize = 20L;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Page(this.page, this.pageSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
package cn.axzo.framework.domain.page;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: liyong.tian
|
||||||
|
* @Date: 2022/10/28
|
||||||
|
* @Description: 基于mybatis-plus封装分页返回类
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Builder
|
||||||
|
public class PageResp<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 页码(从第1页开始)
|
||||||
|
*/
|
||||||
|
private Long page;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 每页条数
|
||||||
|
*/
|
||||||
|
private Long pageSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 总记录数
|
||||||
|
*/
|
||||||
|
private Long totalCount;
|
||||||
|
|
||||||
|
private List<T> list;
|
||||||
|
|
||||||
|
public static <T> PageResp<T> zero(Long pageNum, Long pageSize) {
|
||||||
|
return new PageResp<T>(pageNum, pageSize, 0L, new ArrayList<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> PageResp<T> list(Long page, Long pageSize, Long totalCount, List<T> data) {
|
||||||
|
if (CollectionUtils.isEmpty(data)) {
|
||||||
|
return zero(page, pageSize);
|
||||||
|
}
|
||||||
|
return new PageResp<T>(page, pageSize, totalCount, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> PageResp<T> list(IPage<?> page, List<T> data) {
|
||||||
|
if (CollectionUtils.isEmpty(data)) {
|
||||||
|
return zero(page.getCurrent(), page.getSize());
|
||||||
|
}
|
||||||
|
return new PageResp<T>(page.getCurrent(), page.getSize(), page.getTotal(), data);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -2,6 +2,7 @@ package cn.axzo.framework.domain.web.result;
|
|||||||
|
|
||||||
import cn.axzo.framework.domain.page.Page;
|
import cn.axzo.framework.domain.page.Page;
|
||||||
import cn.axzo.framework.domain.page.PageImpl;
|
import cn.axzo.framework.domain.page.PageImpl;
|
||||||
|
import cn.axzo.framework.domain.page.PageResp;
|
||||||
import cn.axzo.framework.domain.page.PageVerbose;
|
import cn.axzo.framework.domain.page.PageVerbose;
|
||||||
import cn.axzo.framework.domain.page.Pageable;
|
import cn.axzo.framework.domain.page.Pageable;
|
||||||
import cn.axzo.framework.domain.web.code.IRespCode;
|
import cn.axzo.framework.domain.web.code.IRespCode;
|
||||||
@ -83,6 +84,10 @@ public class ApiPageResult<E> extends ApiCoreResult<List<E>> {
|
|||||||
return ok(page.getRecords(), page.getTotal(), (int)page.getCurrent(), (int)page.getSize());
|
return ok(page.getRecords(), page.getTotal(), (int)page.getCurrent(), (int)page.getSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <E> ApiPageResult<E> ok(PageResp<E> page) {
|
||||||
|
return ok(page.getList(), page.getTotalCount(), page.getPage().intValue(), page.getPageSize().intValue());
|
||||||
|
}
|
||||||
|
|
||||||
public static <E> ApiPageResult<E> ok(List<E> data, Long total) {
|
public static <E> ApiPageResult<E> ok(List<E> data, Long total) {
|
||||||
return build(total, SUCCESS.getRespCode(), SUCCESS.getMessage(), data,
|
return build(total, SUCCESS.getRespCode(), SUCCESS.getMessage(), data,
|
||||||
null, null, null);
|
null, null, null);
|
||||||
|
|||||||
@ -1,22 +1,22 @@
|
|||||||
# Assuming a custom constraint annotation @FractionMax
|
# Assuming a custom constraint annotation @FractionMax
|
||||||
FractionMaxConstraintValidator
|
cn.axzo.framework.validator.constraintvalidators.FractionMaxConstraintValidator
|
||||||
BigFractionMaxConstraintValidator
|
cn.axzo.framework.validator.constraintvalidators.BigFractionMaxConstraintValidator
|
||||||
|
|
||||||
# Assuming a custom constraint annotation @FractionMin
|
# Assuming a custom constraint annotation @FractionMin
|
||||||
FractionMinConstraintValidator
|
cn.axzo.framework.validator.constraintvalidators.FractionMinConstraintValidator
|
||||||
BigFractionMinConstraintValidator
|
cn.axzo.framework.validator.constraintvalidators.BigFractionMinConstraintValidator
|
||||||
|
|
||||||
# Assuming a custom constraint annotation @PeriodMax
|
# Assuming a custom constraint annotation @PeriodMax
|
||||||
PeriodMaxConstraintValidator
|
cn.axzo.framework.validator.constraintvalidators.PeriodMaxConstraintValidator
|
||||||
|
|
||||||
# Assuming a custom constraint annotation @PeriodMin
|
# Assuming a custom constraint annotation @PeriodMin
|
||||||
PeriodMinConstraintValidator
|
cn.axzo.framework.validator.constraintvalidators.PeriodMinConstraintValidator
|
||||||
|
|
||||||
# Assuming a custom constraint annotation @SizeIn
|
# Assuming a custom constraint annotation @SizeIn
|
||||||
SizeInConstraintValidator
|
cn.axzo.framework.validator.constraintvalidators.SizeInConstraintValidator
|
||||||
|
|
||||||
# Assuming a custom constraint annotation @ASCII
|
# Assuming a custom constraint annotation @ASCII
|
||||||
ASCIIConstraintValidator
|
cn.axzo.framework.validator.constraintvalidators.ASCIIConstraintValidator
|
||||||
|
|
||||||
# Assuming a custom constraint annotation @UTF8
|
# Assuming a custom constraint annotation @UTF8
|
||||||
UTF8ConstraintValidator
|
cn.axzo.framework.validator.constraintvalidators.UTF8ConstraintValidator
|
||||||
@ -1,5 +1,6 @@
|
|||||||
package cn.axzo.framework.web.http;
|
package cn.axzo.framework.web.http;
|
||||||
|
|
||||||
|
import cn.axzo.framework.domain.page.PageResp;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.github.pagehelper.PageInfo;
|
import com.github.pagehelper.PageInfo;
|
||||||
import cn.axzo.framework.core.net.Nets;
|
import cn.axzo.framework.core.net.Nets;
|
||||||
@ -57,11 +58,11 @@ public class ApiPageEntity<E> extends ApiCoreEntity<List<E>, ApiPageResult<E>> {
|
|||||||
return status(HttpStatus.OK).ok(page);
|
return status(HttpStatus.OK).ok(page);
|
||||||
}
|
}
|
||||||
|
|
||||||
// public static <E> ApiPageEntity<E> ok(PageInfo<E> pageInfo) {
|
public static <E> ApiPageEntity<E> ok(PageInfo<E> pageInfo) {
|
||||||
// return status(HttpStatus.OK).ok(pageInfo);
|
return status(HttpStatus.OK).ok(pageInfo);
|
||||||
// }
|
}
|
||||||
|
|
||||||
public static <E> ApiPageEntity<E> ok(IPage<E> page) {
|
public static <E> ApiPageEntity<E> ok(PageResp<E> page) {
|
||||||
return status(HttpStatus.OK).ok(page);
|
return status(HttpStatus.OK).ok(page);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,7 +146,7 @@ public class ApiPageEntity<E> extends ApiCoreEntity<List<E>, ApiPageResult<E>> {
|
|||||||
public <E> ApiPageEntity<E> ok(PageInfo<E> pageInfo) {
|
public <E> ApiPageEntity<E> ok(PageInfo<E> pageInfo) {
|
||||||
return wrapper(ApiPageResult.ok(pageInfo));
|
return wrapper(ApiPageResult.ok(pageInfo));
|
||||||
}
|
}
|
||||||
public <E> ApiPageEntity<E> ok(IPage<E> page) {
|
public <E> ApiPageEntity<E> ok(PageResp<E> page) {
|
||||||
return wrapper(ApiPageResult.ok(page));
|
return wrapper(ApiPageResult.ok(page));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user