refactor: 调整部分内容所属模块

1.校验等工具类 crud => core
2.@Query crud => mybatis-plus
This commit is contained in:
2023-12-05 20:57:34 +08:00
parent af351e04b9
commit 083bc7b38a
12 changed files with 22 additions and 24 deletions

View File

@@ -1,52 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.annotation;
import top.charles7c.continew.starter.extension.crud.enums.QueryTypeEnum;
import java.lang.annotation.*;
/**
* 查询注解
*
* @author Zheng Jie<a href="https://gitee.com/elunez/eladmin">ELADMIN</a>
* @author Charles7c
* @since 1.0.0
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Query {
/**
* 属性名(默认和使用该注解的属性的名称一致)
*/
String property() default "";
/**
* 查询类型(等值查询、模糊查询、范围查询等)
*/
QueryTypeEnum type() default QueryTypeEnum.EQUAL;
/**
* 多属性模糊查询,仅支持 String 类型属性
* <p>
* 例如:@Query(blurry = {"username", "email"}) 表示根据用户名和邮箱模糊查询
* </p>
*/
String[] blurry() default {};
}

View File

@@ -34,14 +34,14 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import top.charles7c.continew.starter.core.util.ExceptionUtils;
import top.charles7c.continew.starter.core.util.ReflectUtils;
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.data.mybatis.plus.util.QueryHelper;
import top.charles7c.continew.starter.extension.crud.annotation.TreeField;
import top.charles7c.continew.starter.extension.crud.model.query.PageQuery;
import top.charles7c.continew.starter.extension.crud.model.query.SortQuery;
import top.charles7c.continew.starter.extension.crud.model.resp.PageDataResp;
import top.charles7c.continew.starter.extension.crud.util.QueryHelper;
import top.charles7c.continew.starter.extension.crud.util.ReflectUtils;
import top.charles7c.continew.starter.extension.crud.util.TreeUtils;
import top.charles7c.continew.starter.extension.crud.util.validate.CheckUtils;
import top.charles7c.continew.starter.file.excel.util.ExcelUtils;
import java.lang.reflect.Field;

View File

@@ -1,47 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.base;
import com.baomidou.mybatisplus.annotation.IEnum;
import java.io.Serializable;
/**
* 枚举接口
*
* @param <T> value 类型
* @author Charles7c
* @since 1.0.0
*/
public interface IBaseEnum<T extends Serializable> extends IEnum<T> {
/**
* 枚举描述
*
* @return 枚举描述
*/
String getDescription();
/**
* 颜色
*
* @return 颜色
*/
default String getColor() {
return null;
}
}

View File

@@ -1,93 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.enums;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import top.charles7c.continew.starter.extension.crud.base.IBaseEnum;
/**
* 查询类型枚举
*
* @author Charles7c
* @since 1.0.0
*/
@Getter
@RequiredArgsConstructor
public enum QueryTypeEnum implements IBaseEnum<Integer> {
/**
* 等值查询例如WHERE `age` = 18
*/
EQUAL(1, "="),
/**
* 非等值查询例如WHERE `age` != 18
*/
NOT_EQUAL(2, "!="),
/**
* 大于查询例如WHERE `age` > 18
*/
GREATER_THAN(3, ">"),
/**
* 小于查询例如WHERE `age` < 18
*/
LESS_THAN(4, "<"),
/**
* 大于等于查询例如WHERE `age` >= 18
*/
GREATER_THAN_OR_EQUAL(5, ">="),
/**
* 小于等于查询例如WHERE `age` <= 18
*/
LESS_THAN_OR_EQUAL(6, "<="),
/**
* 范围查询例如WHERE `age` BETWEEN 10 AND 18
*/
BETWEEN(7, "BETWEEN"),
/**
* 左模糊查询例如WHERE `nickname` LIKE '%s'
*/
LEFT_LIKE(8, "LIKE '%s'"),
/**
* 中模糊查询例如WHERE `nickname` LIKE '%s%'
*/
INNER_LIKE(9, "LIKE '%s%'"),
/**
* 右模糊查询例如WHERE `nickname` LIKE 's%'
*/
RIGHT_LIKE(10, "LIKE 's%'"),
/**
* 包含查询例如WHERE `age` IN (10, 20, 30)
*/
IN(11, "IN"),
/**
* 不包含查询例如WHERE `age` NOT IN (20, 30)
*/
NOT_IN(12, "NOT IN"),
/**
* 空查询例如WHERE `email` IS NULL
*/
IS_NULL(13, "IS NULL"),
/**
* 非空查询例如WHERE `email` IS NOT NULL
*/
IS_NOT_NULL(14, "IS NOT NULL"),
;
private final Integer value;
private final String description;
}

View File

@@ -1,34 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.exception;
import lombok.NoArgsConstructor;
import top.charles7c.continew.starter.core.exception.BaseException;
/**
* 自定义验证异常-错误请求
*
* @author Charles7c
* @since 1.0.0
*/
@NoArgsConstructor
public class BadRequestException extends BaseException {
public BadRequestException(String message) {
super(message);
}
}

View File

@@ -1,34 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.exception;
import lombok.NoArgsConstructor;
import top.charles7c.continew.starter.core.exception.BaseException;
/**
* 业务异常
*
* @author Charles7c
* @since 1.0.0
*/
@NoArgsConstructor
public class BusinessException extends BaseException {
public BusinessException(String message) {
super(message);
}
}

View File

@@ -1,161 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.util;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import top.charles7c.continew.starter.extension.crud.annotation.Query;
import top.charles7c.continew.starter.extension.crud.enums.QueryTypeEnum;
import top.charles7c.continew.starter.extension.crud.exception.BadRequestException;
import top.charles7c.continew.starter.extension.crud.util.validate.ValidationUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* 查询助手
*
* @author Zheng Jie<a href="https://gitee.com/elunez/eladmin">ELADMIN</a>
* @author Charles7c
* @since 1.0.0
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class QueryHelper {
/**
* 根据查询条件构建 MyBatis Plus 查询条件封装对象
*
* @param query 查询条件
* @param <Q> 查询条件数据类型
* @param <R> 查询数据类型
* @return MyBatis Plus 查询条件封装对象
*/
public static <Q, R> QueryWrapper<R> build(Q query) {
QueryWrapper<R> queryWrapper = new QueryWrapper<>();
// 没有查询条件,直接返回
if (null == query) {
return queryWrapper;
}
// 获取查询条件中所有的字段
List<Field> fieldList = ReflectUtils.getNonStaticFields(query.getClass());
fieldList.forEach(field -> buildQuery(query, field, queryWrapper));
return queryWrapper;
}
/**
* 构建 MyBatis Plus 查询条件封装对象
*
* @param query 查询条件
* @param field 字段
* @param queryWrapper MyBatis Plus 查询条件封装对象
* @param <Q> 查询条件数据类型
* @param <R> 查询数据类型
*/
private static <Q, R> void buildQuery(Q query, Field field, QueryWrapper<R> queryWrapper) {
boolean accessible = field.canAccess(query);
try {
field.setAccessible(true);
// 没有 @Query直接返回
Query queryAnnotation = field.getAnnotation(Query.class);
if (null == queryAnnotation) {
return;
}
// 如果字段值为空,直接返回
Object fieldValue = field.get(query);
if (ObjectUtil.isEmpty(fieldValue)) {
return;
}
// 解析查询条件
parse(queryAnnotation, field.getName(), fieldValue, queryWrapper);
} catch (BadRequestException e) {
log.error("Build query occurred an validation error: {}. Query: {}, Field: {}.", e.getMessage(), query,
field, e);
throw e;
} catch (Exception e) {
log.error("Build query occurred an error: {}. Query: {}, Field: {}.", e.getMessage(), query, field, e);
} finally {
field.setAccessible(accessible);
}
}
/**
* 解析查询条件
*
* @param queryAnnotation 查询注解
* @param fieldName 字段名
* @param fieldValue 字段值
* @param queryWrapper MyBatis Plus 查询条件封装对象
* @param <R> 查询数据类型
*/
private static <R> void parse(Query queryAnnotation, String fieldName, Object fieldValue,
QueryWrapper<R> queryWrapper) {
// 解析多属性模糊查询
// 如果设置了多属性模糊查询,分割属性进行条件拼接
String[] blurryPropertyArr = queryAnnotation.blurry();
if (ArrayUtil.isNotEmpty(blurryPropertyArr)) {
queryWrapper.and(wrapper -> {
for (String blurryProperty : blurryPropertyArr) {
wrapper.or().like(StrUtil.toUnderlineCase(blurryProperty), fieldValue);
}
});
return;
}
// 解析单个属性查询
// 如果没有单独指定属性名,就和使用该注解的属性的名称一致
// 注意:数据库规范中列采用下划线连接法命名,程序规范中变量采用驼峰法命名
String property = queryAnnotation.property();
String columnName = StrUtil.toUnderlineCase(StrUtil.blankToDefault(property, fieldName));
QueryTypeEnum queryType = queryAnnotation.type();
switch (queryType) {
case EQUAL -> queryWrapper.eq(columnName, fieldValue);
case NOT_EQUAL -> queryWrapper.ne(columnName, fieldValue);
case GREATER_THAN -> queryWrapper.gt(columnName, fieldValue);
case LESS_THAN -> queryWrapper.lt(columnName, fieldValue);
case GREATER_THAN_OR_EQUAL -> queryWrapper.ge(columnName, fieldValue);
case LESS_THAN_OR_EQUAL -> queryWrapper.le(columnName, fieldValue);
case BETWEEN -> {
List<Object> between = new ArrayList<>((List<Object>) fieldValue);
ValidationUtils.throwIf(between.size() != 2, "[{}] 必须是一个范围", fieldName);
queryWrapper.between(columnName, between.get(0), between.get(1));
}
case LEFT_LIKE -> queryWrapper.likeLeft(columnName, fieldValue);
case INNER_LIKE -> queryWrapper.like(columnName, fieldValue);
case RIGHT_LIKE -> queryWrapper.likeRight(columnName, fieldValue);
case IN -> {
ValidationUtils.throwIfEmpty(fieldValue, "[{}] 不能为空", fieldName);
queryWrapper.in(columnName, (List<Object>) fieldValue);
}
case NOT_IN -> {
ValidationUtils.throwIfEmpty(fieldValue, "[{}] 不能为空", fieldName);
queryWrapper.notIn(columnName, (List<Object>) fieldValue);
}
case IS_NULL -> queryWrapper.isNull(columnName);
case IS_NOT_NULL -> queryWrapper.isNotNull(columnName);
default -> throw new IllegalArgumentException(String.format("暂不支持 [%s] 查询类型", queryType));
}
}
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.util;
import cn.hutool.core.util.ReflectUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* 反射工具类
*
* @author Charles7c
* @since 1.0.0
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ReflectUtils {
/**
* 获得一个类中所有非静态字段名列表,包括其父类中的字段<br>
* 如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。
*
* @param beanClass 类
* @return 非静态字段名列表
* @throws SecurityException 安全检查异常
*/
public static List<String> getNonStaticFieldsName(Class<?> beanClass) throws SecurityException {
List<Field> nonStaticFields = getNonStaticFields(beanClass);
return nonStaticFields.stream().map(Field::getName).collect(Collectors.toList());
}
/**
* 获得一个类中所有非静态字段列表,包括其父类中的字段<br>
* 如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。
*
* @param beanClass 类
* @return 非静态字段列表
* @throws SecurityException 安全检查异常
*/
public static List<Field> getNonStaticFields(Class<?> beanClass) throws SecurityException {
Field[] fields = ReflectUtil.getFields(beanClass);
return Arrays.stream(fields).filter(f -> !Modifier.isStatic(f.getModifiers())).collect(Collectors.toList());
}
}

View File

@@ -24,8 +24,8 @@ import cn.hutool.core.lang.tree.parser.NodeParser;
import cn.hutool.core.util.ReflectUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.extension.crud.annotation.TreeField;
import top.charles7c.continew.starter.extension.crud.util.validate.CheckUtils;
import java.util.ArrayList;
import java.util.List;

View File

@@ -1,204 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.util.validate;
import cn.hutool.core.util.StrUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import top.charles7c.continew.starter.core.constant.StringConstants;
import top.charles7c.continew.starter.extension.crud.exception.BusinessException;
import java.util.function.BooleanSupplier;
/**
* 业务参数校验工具类(抛出 500 ServiceException
*
* @author Charles7c
* @see BusinessException
* @since 1.0.0
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CheckUtils extends Validator {
private static final Class<BusinessException> EXCEPTION_TYPE = BusinessException.class;
/**
* 如果不存在,抛出异常
*
* @param obj 被检测的对象
* @param entityName 实体名
* @param fieldName 字段名
* @param fieldValue 字段值
*/
public static void throwIfNotExists(Object obj, String entityName, String fieldName, Object fieldValue) {
String message = String.format("%s 为 [%s] 的 %s 记录已不存在", fieldName, fieldValue,
StrUtil.replace(entityName, "DO", StringConstants.EMPTY));
throwIfNull(obj, message, EXCEPTION_TYPE);
}
/**
* 如果为空,抛出异常
*
* @param obj 被检测的对象
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfNull(Object obj, String template, Object... params) {
throwIfNull(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果不为空,抛出异常
*
* @param obj 被检测的对象
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfNotNull(Object obj, String template, Object... params) {
throwIfNotNull(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果存在,抛出异常
*
* @param obj 被检测的对象
* @param entityName 实体名
* @param fieldName 字段名
* @param fieldValue 字段值
*/
public static void throwIfExists(Object obj, String entityName, String fieldName, Object fieldValue) {
String message = String.format("%s 为 [%s] 的 %s 记录已存在", fieldName, fieldValue, entityName);
throwIfNotNull(obj, message, EXCEPTION_TYPE);
}
/**
* 如果为空,抛出异常
*
* @param obj 被检测的对象
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfEmpty(Object obj, String template, Object... params) {
throwIfEmpty(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果不为空,抛出异常
*
* @param obj 被检测的对象
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfNotEmpty(Object obj, String template, Object... params) {
throwIfNotEmpty(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果为空,抛出异常
*
* @param str 被检测的字符串
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfBlank(CharSequence str, String template, Object... params) {
throwIfBlank(str, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果不为空,抛出异常
*
* @param str 被检测的字符串
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfNotBlank(CharSequence str, String template, Object... params) {
throwIfNotBlank(str, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果相同,抛出异常
*
* @param obj1 要比较的对象1
* @param obj2 要比较的对象2
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfEqual(Object obj1, Object obj2, String template, Object... params) {
throwIfEqual(obj1, obj2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果不相同,抛出异常
*
* @param obj1 要比较的对象1
* @param obj2 要比较的对象2
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfNotEqual(Object obj1, Object obj2, String template, Object... params) {
throwIfNotEqual(obj1, obj2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果相同,抛出异常(不区分大小写)
*
* @param str1 要比较的字符串1
* @param str2 要比较的字符串2
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfEqualIgnoreCase(CharSequence str1, CharSequence str2, String template, Object... params) {
throwIfEqualIgnoreCase(str1, str2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果不相同,抛出异常(不区分大小写)
*
* @param str1 要比较的字符串1
* @param str2 要比较的字符串2
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfNotEqualIgnoreCase(CharSequence str1, CharSequence str2, String template,
Object... params) {
throwIfNotEqualIgnoreCase(str1, str2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果条件成立,抛出异常
*
* @param condition 条件
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIf(boolean condition, String template, Object... params) {
throwIf(condition, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果条件成立,抛出异常
*
* @param conditionSupplier 条件
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIf(BooleanSupplier conditionSupplier, String template, Object... params) {
throwIf(conditionSupplier, StrUtil.format(template, params), EXCEPTION_TYPE);
}
}

View File

@@ -1,176 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.util.validate;
import cn.hutool.core.util.StrUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import top.charles7c.continew.starter.extension.crud.exception.BadRequestException;
import java.util.function.BooleanSupplier;
/**
* 基本参数校验工具类(抛出 400 BadRequestException
*
* @author Charles7c
* @see BadRequestException
* @since 1.0.0
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ValidationUtils extends Validator {
private static final Class<BadRequestException> EXCEPTION_TYPE = BadRequestException.class;
/**
* 如果为空,抛出异常
*
* @param obj 被检测的对象
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfNull(Object obj, String template, Object... params) {
throwIfNull(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果不为空,抛出异常
*
* @param obj 被检测的对象
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfNotNull(Object obj, String template, Object... params) {
throwIfNotNull(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果为空,抛出异常
*
* @param obj 被检测的对象
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfEmpty(Object obj, String template, Object... params) {
throwIfEmpty(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果不为空,抛出异常
*
* @param obj 被检测的对象
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfNotEmpty(Object obj, String template, Object... params) {
throwIfNotEmpty(obj, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果为空,抛出异常
*
* @param str 被检测的字符串
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfBlank(CharSequence str, String template, Object... params) {
throwIfBlank(str, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果不为空,抛出异常
*
* @param str 被检测的字符串
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfNotBlank(CharSequence str, String template, Object... params) {
throwIfNotBlank(str, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果相同,抛出异常
*
* @param obj1 要比较的对象1
* @param obj2 要比较的对象2
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfEqual(Object obj1, Object obj2, String template, Object... params) {
throwIfEqual(obj1, obj2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果不相同,抛出异常
*
* @param obj1 要比较的对象1
* @param obj2 要比较的对象2
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfNotEqual(Object obj1, Object obj2, String template, Object... params) {
throwIfNotEqual(obj1, obj2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果相同,抛出异常(不区分大小写)
*
* @param str1 要比较的字符串1
* @param str2 要比较的字符串2
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfEqualIgnoreCase(CharSequence str1, CharSequence str2, String template, Object... params) {
throwIfEqualIgnoreCase(str1, str2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果不相同,抛出异常(不区分大小写)
*
* @param str1 要比较的字符串1
* @param str2 要比较的字符串2
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIfNotEqualIgnoreCase(CharSequence str1, CharSequence str2, String template,
Object... params) {
throwIfNotEqualIgnoreCase(str1, str2, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果条件成立,抛出异常
*
* @param condition 条件
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIf(boolean condition, String template, Object... params) {
throwIf(condition, StrUtil.format(template, params), EXCEPTION_TYPE);
}
/**
* 如果条件成立,抛出异常
*
* @param conditionSupplier 条件
* @param template 异常信息模板,被替换的部分用 {} 表示,如果模板为 null返回 "null"
* @param params 参数值
*/
public static void throwIf(BooleanSupplier conditionSupplier, String template, Object... params) {
throwIf(conditionSupplier, StrUtil.format(template, params), EXCEPTION_TYPE);
}
}

View File

@@ -1,186 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.util.validate;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.function.BooleanSupplier;
/**
* 校验器
*
* @author Charles7c
* @since 1.0.0
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Validator {
/**
* 如果为空,抛出异常
*
* @param obj 被检测的对象
* @param message 错误信息
* @param exceptionType 异常类型
*/
protected static void throwIfNull(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
throwIf(null == obj, message, exceptionType);
}
/**
* 如果不为空,抛出异常
*
* @param obj 被检测的对象
* @param message 错误信息
* @param exceptionType 异常类型
*/
protected static void throwIfNotNull(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
throwIf(null != obj, message, exceptionType);
}
/**
* 如果为空,抛出异常
*
* @param obj 被检测的对象
* @param message 错误信息
* @param exceptionType 异常类型
*/
protected static void throwIfEmpty(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
throwIf(ObjectUtil.isEmpty(obj), message, exceptionType);
}
/**
* 如果不为空,抛出异常
*
* @param obj 被检测的对象
* @param message 错误信息
* @param exceptionType 异常类型
*/
protected static void throwIfNotEmpty(Object obj, String message, Class<? extends RuntimeException> exceptionType) {
throwIf(ObjectUtil.isNotEmpty(obj), message, exceptionType);
}
/**
* 如果为空,抛出异常
*
* @param str 被检测的字符串
* @param message 错误信息
* @param exceptionType 异常类型
*/
protected static void throwIfBlank(CharSequence str, String message,
Class<? extends RuntimeException> exceptionType) {
throwIf(StrUtil.isBlank(str), message, exceptionType);
}
/**
* 如果不为空,抛出异常
*
* @param str 被检测的字符串
* @param message 错误信息
* @param exceptionType 异常类型
*/
protected static void throwIfNotBlank(CharSequence str, String message,
Class<? extends RuntimeException> exceptionType) {
throwIf(StrUtil.isNotBlank(str), message, exceptionType);
}
/**
* 如果相同,抛出异常
*
* @param obj1 要比较的对象1
* @param obj2 要比较的对象2
* @param message 错误信息
* @param exceptionType 异常类型
*/
protected static void throwIfEqual(Object obj1, Object obj2, String message,
Class<? extends RuntimeException> exceptionType) {
throwIf(ObjectUtil.equal(obj1, obj2), message, exceptionType);
}
/**
* 如果不相同,抛出异常
*
* @param obj1 要比较的对象1
* @param obj2 要比较的对象2
* @param message 错误信息
* @param exceptionType 异常类型
*/
protected static void throwIfNotEqual(Object obj1, Object obj2, String message,
Class<? extends RuntimeException> exceptionType) {
throwIf(ObjectUtil.notEqual(obj1, obj2), message, exceptionType);
}
/**
* 如果相同,抛出异常(不区分大小写)
*
* @param str1 要比较的字符串1
* @param str2 要比较的字符串2
* @param message 错误信息
* @param exceptionType 异常类型
*/
protected static void throwIfEqualIgnoreCase(CharSequence str1, CharSequence str2, String message,
Class<? extends RuntimeException> exceptionType) {
throwIf(StrUtil.equalsIgnoreCase(str1, str2), message, exceptionType);
}
/**
* 如果不相同,抛出异常(不区分大小写)
*
* @param str1 要比较的字符串1
* @param str2 要比较的字符串2
* @param message 错误信息
* @param exceptionType 异常类型
*/
protected static void throwIfNotEqualIgnoreCase(CharSequence str1, CharSequence str2, String message,
Class<? extends RuntimeException> exceptionType) {
throwIf(!StrUtil.equalsIgnoreCase(str1, str2), message, exceptionType);
}
/**
* 如果条件成立,抛出异常
*
* @param condition 条件
* @param message 错误信息
* @param exceptionType 异常类型
*/
protected static void throwIf(boolean condition, String message, Class<? extends RuntimeException> exceptionType) {
if (condition) {
log.error(message);
throw ReflectUtil.newInstance(exceptionType, message);
}
}
/**
* 如果条件成立,抛出异常
*
* @param conditionSupplier 条件
* @param message 错误信息
* @param exceptionType 异常类型
*/
protected static void throwIf(BooleanSupplier conditionSupplier, String message,
Class<? extends RuntimeException> exceptionType) {
if (null != conditionSupplier && conditionSupplier.getAsBoolean()) {
log.error(message);
throw ReflectUtil.newInstance(exceptionType, message);
}
}
}