feat(data): Query 注解新增多列查询逻辑关系支持(原来仅支持或者,现在也支持并且)

This commit is contained in:
2025-07-22 20:44:46 +08:00
parent 0a9027d91f
commit 3e822c0b84
3 changed files with 57 additions and 1 deletions

View File

@@ -16,6 +16,7 @@
package top.continew.starter.data.annotation;
import top.continew.starter.data.enums.LogicalRelation;
import top.continew.starter.data.enums.QueryType;
import java.lang.annotation.*;
@@ -46,4 +47,9 @@ public @interface Query {
* 查询类型(等值查询、模糊查询、范围查询等)
*/
QueryType type() default QueryType.EQ;
/**
* 多列查询时的逻辑关系(仅当 columns 长度大于 1 时生效)
*/
LogicalRelation logicalRelation() default LogicalRelation.OR;
}

View File

@@ -0,0 +1,36 @@
/*
* 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.continew.starter.data.enums;
/**
* 逻辑关系枚举
*
* @author Charles7c
* @since 2.13.3
*/
public enum LogicalRelation {
/**
* 并且关系
*/
AND,
/**
* 或者关系
*/
OR
}

View File

@@ -32,6 +32,7 @@ import top.continew.starter.core.util.validation.ValidationUtils;
import top.continew.starter.data.annotation.Query;
import top.continew.starter.data.annotation.QueryIgnore;
import top.continew.starter.data.enums.QueryType;
import top.continew.starter.data.enums.LogicalRelation;
import java.lang.reflect.Field;
import java.util.ArrayList;
@@ -174,8 +175,21 @@ public class QueryWrapperHelper {
return consumers;
}
// 解析多列查询
LogicalRelation logicalRelation = queryAnnotation.logicalRelation();
List<Consumer<QueryWrapper<R>>> columnConsumers = new ArrayList<>();
for (String column : columns) {
parse(queryType, column, fieldValue, consumers);
parse(queryType, column, fieldValue, columnConsumers);
}
if (logicalRelation == LogicalRelation.AND) {
if (!columnConsumers.isEmpty()) {
consumers.add(q -> {
columnConsumers.get(0).accept(q);
columnConsumers.subList(1, columnConsumers.size()).forEach(q::and);
});
}
} else {
consumers.addAll(columnConsumers);
}
return consumers;
} catch (BadRequestException e) {