mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-10 20:57:18 +08:00
feat(data/mybatis-plus): 新增 QueryIgnore 忽略查询解析注解
This commit is contained in:
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* 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.query;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询解析忽略注解
|
||||||
|
*
|
||||||
|
* @author Charles7c
|
||||||
|
* @since 1.3.0
|
||||||
|
*/
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface QueryIgnore {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取是否忽略查询解析
|
||||||
|
*
|
||||||
|
* @return 是否忽略查询解析
|
||||||
|
*/
|
||||||
|
boolean value() default true;
|
||||||
|
}
|
@@ -30,8 +30,8 @@ import top.charles7c.continew.starter.core.util.validate.ValidationUtils;
|
|||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -96,28 +96,32 @@ public class QueryWrapperHelper {
|
|||||||
* @param <R> 查询数据类型
|
* @param <R> 查询数据类型
|
||||||
* @return QueryWrapper Consumer
|
* @return QueryWrapper Consumer
|
||||||
*/
|
*/
|
||||||
public static <Q, R> List<Consumer<QueryWrapper<R>>> buildWrapperConsumer(Q query, Field field) {
|
private static <Q, R> List<Consumer<QueryWrapper<R>>> buildWrapperConsumer(Q query, Field field) {
|
||||||
List<Consumer<QueryWrapper<R>>> consumers = new ArrayList<>();
|
|
||||||
boolean accessible = field.canAccess(query);
|
boolean accessible = field.canAccess(query);
|
||||||
try {
|
try {
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
// 如果字段值为空,直接返回
|
// 如果字段值为空,直接返回
|
||||||
Object fieldValue = field.get(query);
|
Object fieldValue = field.get(query);
|
||||||
if (ObjectUtil.isEmpty(fieldValue)) {
|
if (ObjectUtil.isEmpty(fieldValue)) {
|
||||||
return consumers;
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
// 设置了 @QueryIgnore 注解,直接忽略
|
||||||
|
QueryIgnore queryIgnoreAnnotation = field.getAnnotation(QueryIgnore.class);
|
||||||
|
if (null != queryIgnoreAnnotation && queryIgnoreAnnotation.value()) {
|
||||||
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
// 建议:数据库表列建议采用下划线连接法命名,程序变量建议采用驼峰法命名
|
// 建议:数据库表列建议采用下划线连接法命名,程序变量建议采用驼峰法命名
|
||||||
String fieldName = field.getName();
|
String fieldName = field.getName();
|
||||||
// 没有 @Query 注解,默认等值查询
|
// 没有 @Query 注解,默认等值查询
|
||||||
Query queryAnnotation = field.getAnnotation(Query.class);
|
Query queryAnnotation = field.getAnnotation(Query.class);
|
||||||
if (Objects.isNull(queryAnnotation)) {
|
if (null == queryAnnotation) {
|
||||||
consumers.add(q -> q.eq(StrUtil.toUnderlineCase(fieldName), fieldValue));
|
return Collections.singletonList(q -> q.eq(StrUtil.toUnderlineCase(fieldName), fieldValue));
|
||||||
return consumers;
|
|
||||||
}
|
}
|
||||||
// 解析单列查询
|
// 解析单列查询
|
||||||
QueryType queryType = queryAnnotation.type();
|
QueryType queryType = queryAnnotation.type();
|
||||||
String[] columns = queryAnnotation.columns();
|
String[] columns = queryAnnotation.columns();
|
||||||
final int columnLength = ArrayUtil.length(columns);
|
final int columnLength = ArrayUtil.length(columns);
|
||||||
|
List<Consumer<QueryWrapper<R>>> consumers = new ArrayList<>(columnLength);
|
||||||
if (columnLength <= 1) {
|
if (columnLength <= 1) {
|
||||||
String columnName = columnLength == 1 ? columns[0] : StrUtil.toUnderlineCase(fieldName);
|
String columnName = columnLength == 1 ? columns[0] : StrUtil.toUnderlineCase(fieldName);
|
||||||
parse(queryType, columnName, fieldValue, consumers);
|
parse(queryType, columnName, fieldValue, consumers);
|
||||||
@@ -127,6 +131,7 @@ public class QueryWrapperHelper {
|
|||||||
for (String column : columns) {
|
for (String column : columns) {
|
||||||
parse(queryType, column, fieldValue, consumers);
|
parse(queryType, column, fieldValue, consumers);
|
||||||
}
|
}
|
||||||
|
return consumers;
|
||||||
} catch (BadRequestException e) {
|
} catch (BadRequestException e) {
|
||||||
log.error("Build query wrapper occurred an validation error: {}. Query: {}, Field: {}.", e
|
log.error("Build query wrapper occurred an validation error: {}. Query: {}, Field: {}.", e
|
||||||
.getMessage(), query, field, e);
|
.getMessage(), query, field, e);
|
||||||
@@ -137,7 +142,7 @@ public class QueryWrapperHelper {
|
|||||||
} finally {
|
} finally {
|
||||||
field.setAccessible(accessible);
|
field.setAccessible(accessible);
|
||||||
}
|
}
|
||||||
return consumers;
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user