mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
refactor: 调整部分内容所属模块
1.校验等工具类 crud => core 2.@Query crud => mybatis-plus
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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.data.mybatis.plus.annotation;
|
||||
|
||||
import top.charles7c.continew.starter.data.mybatis.plus.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 {};
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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.data.mybatis.plus.enums;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* 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.data.mybatis.plus.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* 查询类型枚举
|
||||
*
|
||||
* @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;
|
||||
}
|
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
* 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.data.mybatis.plus.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.core.exception.BadRequestException;
|
||||
import top.charles7c.continew.starter.core.util.ReflectUtils;
|
||||
import top.charles7c.continew.starter.core.util.validate.ValidationUtils;
|
||||
import top.charles7c.continew.starter.data.mybatis.plus.annotation.Query;
|
||||
import top.charles7c.continew.starter.data.mybatis.plus.enums.QueryTypeEnum;
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user