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

@@ -0,0 +1,33 @@
/*
* 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.core.exception;
import lombok.NoArgsConstructor;
/**
* 自定义验证异常-错误请求
*
* @author Charles7c
* @since 1.0.0
*/
@NoArgsConstructor
public class BadRequestException extends BaseException {
public BadRequestException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,33 @@
/*
* 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.core.exception;
import lombok.NoArgsConstructor;
/**
* 业务异常
*
* @author Charles7c
* @since 1.0.0
*/
@NoArgsConstructor
public class BusinessException extends BaseException {
public BusinessException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,63 @@
/*
* 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.core.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

@@ -0,0 +1,204 @@
/*
* 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.core.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.core.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

@@ -0,0 +1,176 @@
/*
* 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.core.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.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

@@ -0,0 +1,186 @@
/*
* 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.core.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);
}
}
}