diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/query/QueryIgnore.java b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/query/QueryIgnore.java new file mode 100644 index 00000000..69d654c9 --- /dev/null +++ b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/query/QueryIgnore.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * 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 + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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; +} diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/query/QueryWrapperHelper.java b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/query/QueryWrapperHelper.java index 9ec6792a..919af292 100644 --- a/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/query/QueryWrapperHelper.java +++ b/continew-starter-data/continew-starter-data-mybatis-plus/src/main/java/top/charles7c/continew/starter/data/mybatis/plus/query/QueryWrapperHelper.java @@ -30,8 +30,8 @@ import top.charles7c.continew.starter.core.util.validate.ValidationUtils; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import java.util.Objects; import java.util.function.Consumer; /** @@ -96,28 +96,32 @@ public class QueryWrapperHelper { * @param 查询数据类型 * @return QueryWrapper Consumer */ - public static List>> buildWrapperConsumer(Q query, Field field) { - List>> consumers = new ArrayList<>(); + private static List>> buildWrapperConsumer(Q query, Field field) { boolean accessible = field.canAccess(query); try { field.setAccessible(true); // 如果字段值为空,直接返回 Object fieldValue = field.get(query); 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(); // 没有 @Query 注解,默认等值查询 Query queryAnnotation = field.getAnnotation(Query.class); - if (Objects.isNull(queryAnnotation)) { - consumers.add(q -> q.eq(StrUtil.toUnderlineCase(fieldName), fieldValue)); - return consumers; + if (null == queryAnnotation) { + return Collections.singletonList(q -> q.eq(StrUtil.toUnderlineCase(fieldName), fieldValue)); } // 解析单列查询 QueryType queryType = queryAnnotation.type(); String[] columns = queryAnnotation.columns(); final int columnLength = ArrayUtil.length(columns); + List>> consumers = new ArrayList<>(columnLength); if (columnLength <= 1) { String columnName = columnLength == 1 ? columns[0] : StrUtil.toUnderlineCase(fieldName); parse(queryType, columnName, fieldValue, consumers); @@ -127,6 +131,7 @@ public class QueryWrapperHelper { for (String column : columns) { parse(queryType, column, fieldValue, consumers); } + return consumers; } catch (BadRequestException e) { log.error("Build query wrapper occurred an validation error: {}. Query: {}, Field: {}.", e .getMessage(), query, field, e); @@ -137,7 +142,7 @@ public class QueryWrapperHelper { } finally { field.setAccessible(accessible); } - return consumers; + return Collections.emptyList(); } /**