From 40ef2658f75a0d7de941ee19a6b43047ece47558 Mon Sep 17 00:00:00 2001 From: aa Date: Fri, 10 Jan 2025 15:26:47 +0800 Subject: [PATCH 1/3] fix problem of between --- src/main/java/com/fhs/jpa/wrapper/AbstractWrapper.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/fhs/jpa/wrapper/AbstractWrapper.java b/src/main/java/com/fhs/jpa/wrapper/AbstractWrapper.java index 32dd51a..9b59949 100644 --- a/src/main/java/com/fhs/jpa/wrapper/AbstractWrapper.java +++ b/src/main/java/com/fhs/jpa/wrapper/AbstractWrapper.java @@ -109,6 +109,9 @@ public class AbstractWrapper { return criteriaBuilder.isNull(root.get(predicate.getFieldName())); case NOTNULL: return criteriaBuilder.isNotNull(root.get(predicate.getFieldName())); + case BETWEEN: + Object[] objPair = (Object[])predicate.getValue(); + return criteriaBuilder.between(root.get(predicate.getFieldName()), (Comparable)objPair[0], (Comparable)objPair[1]); } return null; } -- Gitee From aca66f8377a3dac13ebd9cec8e115c6c55201083 Mon Sep 17 00:00:00 2001 From: aa Date: Fri, 10 Jan 2025 15:28:34 +0800 Subject: [PATCH 2/3] add support of greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo --- .../com/fhs/jpa/wrapper/AbstractWrapper.java | 11 +- .../fhs/jpa/wrapper/LambdaQueryWrapper.java | 112 +++++++++++++++++ .../com/fhs/jpa/wrapper/QueryWrapper.java | 113 ++++++++++++++++++ 3 files changed, 235 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/fhs/jpa/wrapper/AbstractWrapper.java b/src/main/java/com/fhs/jpa/wrapper/AbstractWrapper.java index 9b59949..e4d67d8 100644 --- a/src/main/java/com/fhs/jpa/wrapper/AbstractWrapper.java +++ b/src/main/java/com/fhs/jpa/wrapper/AbstractWrapper.java @@ -6,6 +6,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.jpa.domain.Specification; import javax.persistence.criteria.*; + import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -109,6 +110,14 @@ public class AbstractWrapper { return criteriaBuilder.isNull(root.get(predicate.getFieldName())); case NOTNULL: return criteriaBuilder.isNotNull(root.get(predicate.getFieldName())); + case GREATERTHAN: + return criteriaBuilder.greaterThan(root.get(predicate.getFieldName()), (Comparable) predicate.getValue()); + case GREATERTHANOREQUALTO: + return criteriaBuilder.greaterThanOrEqualTo(root.get(predicate.getFieldName()), (Comparable) predicate.getValue()); + case LESSTHAN: + return criteriaBuilder.lessThan(root.get(predicate.getFieldName()), (Comparable) predicate.getValue()); + case LESSTHANOREQUALTO: + return criteriaBuilder.lessThanOrEqualTo(root.get(predicate.getFieldName()), (Comparable)predicate.getValue()); case BETWEEN: Object[] objPair = (Object[])predicate.getValue(); return criteriaBuilder.between(root.get(predicate.getFieldName()), (Comparable)objPair[0], (Comparable)objPair[1]); @@ -156,5 +165,5 @@ class TempPredicate { } enum Operator { - EQ, LIKE, NE, GE, GT, LE, LT, IN, NOTLIKE, BETWEEN, NOTIN,ISNULL,NOTNULL; + EQ, LIKE, NE, GE, GT, LE, LT, IN, NOTLIKE, BETWEEN, NOTIN,ISNULL,NOTNULL,GREATERTHAN,GREATERTHANOREQUALTO,LESSTHAN,LESSTHANOREQUALTO; } diff --git a/src/main/java/com/fhs/jpa/wrapper/LambdaQueryWrapper.java b/src/main/java/com/fhs/jpa/wrapper/LambdaQueryWrapper.java index ab151c5..4753430 100644 --- a/src/main/java/com/fhs/jpa/wrapper/LambdaQueryWrapper.java +++ b/src/main/java/com/fhs/jpa/wrapper/LambdaQueryWrapper.java @@ -194,6 +194,118 @@ public class LambdaQueryWrapper extends AbstractWrapper{ return this; } + /** + * > + * + * @param fieldLambda lambda + * @param value 值 + * @return this + */ + public LambdaQueryWrapper greaterThan(SFunction fieldLambda, Comparable value) { + greaterThan(true, fieldLambda, value); + return this; + } + + /** + * > + * + * @param condition 是否需要使用本条件 + * @param fieldLambda lambda + * @param value 值 + * @return this + */ + public LambdaQueryWrapper greaterThan(boolean condition, SFunction fieldLambda, Comparable value) { + if (condition) { + tempPredicates.add(TempPredicate.builder().fieldName(fieldLambda2FieldName(fieldLambda)) + .operator(Operator.GREATERTHAN).value(value).build()); + } + return this; + } + + /** + * >= + * + * @param fieldLambda lambda + * @param value 值 + * @return this + */ + public LambdaQueryWrapper greaterThanOrEqualTo(SFunction fieldLambda, Comparable value) { + greaterThanOrEqualTo(true, fieldLambda, value); + return this; + } + + /** + * >= + * + * @param condition 是否需要使用本条件 + * @param fieldLambda lambda + * @param value 值 + * @return this + */ + public LambdaQueryWrapper greaterThanOrEqualTo(boolean condition, SFunction fieldLambda, Comparable value) { + if (condition) { + tempPredicates.add(TempPredicate.builder().fieldName(fieldLambda2FieldName(fieldLambda)) + .operator(Operator.GREATERTHANOREQUALTO).value(value).build()); + } + return this; + } + + /** + * < + * + * @param fieldLambda lambda + * @param value 值 + * @return this + */ + public LambdaQueryWrapper lessThan(SFunction fieldLambda, Comparable value) { + lessThan(true, fieldLambda, value); + return this; + } + + /** + * < + * + * @param condition 是否需要使用本条件 + * @param fieldLambda lambda + * @param value 值 + * @return this + */ + public LambdaQueryWrapper lessThan(boolean condition, SFunction fieldLambda, Comparable value) { + if (condition) { + tempPredicates.add(TempPredicate.builder().fieldName(fieldLambda2FieldName(fieldLambda)) + .operator(Operator.LESSTHAN).value(value).build()); + } + return this; + } + + /** + * <= + * + * @param fieldLambda lambda + * @param value 值 + * @return this + */ + public LambdaQueryWrapper lessThanOrEqualTo(SFunction fieldLambda, Comparable value) { + lessThanOrEqualTo(true, fieldLambda, value); + return this; + } + + /** + * <= + * + * @param condition 是否需要使用本条件 + * @param fieldLambda lambda + * @param value 值 + * @return this + */ + public LambdaQueryWrapper lessThanOrEqualTo(boolean condition, SFunction fieldLambda, Comparable value) { + if (condition) { + tempPredicates.add(TempPredicate.builder().fieldName(fieldLambda2FieldName(fieldLambda)) + .operator(Operator.LESSTHANOREQUALTO).value(value).build()); + } + return this; + } + /** * like '%xx%' * diff --git a/src/main/java/com/fhs/jpa/wrapper/QueryWrapper.java b/src/main/java/com/fhs/jpa/wrapper/QueryWrapper.java index f2a76d2..ccce977 100644 --- a/src/main/java/com/fhs/jpa/wrapper/QueryWrapper.java +++ b/src/main/java/com/fhs/jpa/wrapper/QueryWrapper.java @@ -176,6 +176,119 @@ public class QueryWrapper extends AbstractWrapper { return this; } + + /** + * > + * + * @param fieldName 字段名 + * @param value 值 + * @return this + */ + public QueryWrapper greaterThan(String fieldName, Comparable value) { + greaterThan(true, fieldName, value); + return this; + } + + /** + * > + * + * @param condition 是否需要使用本条件 + * @param fieldName 字段名 + * @param value 值 + * @return this + */ + public QueryWrapper greaterThan(boolean condition, String fieldName, Comparable value) { + if (condition) { + tempPredicates.add(TempPredicate.builder().fieldName(fieldName) + .operator(Operator.GREATERTHAN).value(value).build()); + } + return this; + } + + /** + * >= + * + * @param fieldName 字段名 + * @param value 值 + * @return this + */ + public QueryWrapper greaterThanOrEqualTo(String fieldName, Comparable value) { + greaterThanOrEqualTo(true, fieldName, value); + return this; + } + + /** + * >= + * + * @param condition 是否需要使用本条件 + * @param fieldName 字段名 + * @param value 值 + * @return this + */ + public QueryWrapper greaterThanOrEqualTo(boolean condition, String fieldName, Comparable value) { + if (condition) { + tempPredicates.add(TempPredicate.builder().fieldName(fieldName) + .operator(Operator.GREATERTHANOREQUALTO).value(value).build()); + } + return this; + } + + /** + * < + * + * @param fieldName 字段名 + * @param value 值 + * @return this + */ + public QueryWrapper lessThan(String fieldName, Comparable value) { + lessThan(true, fieldName, value); + return this; + } + + /** + * < + * + * @param condition 是否需要使用本条件 + * @param fieldName 字段名 + * @param value 值 + * @return this + */ + public QueryWrapper lessThan(boolean condition, String fieldName, Comparable value) { + if (condition) { + tempPredicates.add(TempPredicate.builder().fieldName(fieldName) + .operator(Operator.LESSTHAN).value(value).build()); + } + return this; + } + + /** + * <= + * + * @param fieldName 字段名 + * @param value 值 + * @return this + */ + public QueryWrapper lessThanOrEqualTo(String fieldName, Comparable value) { + lessThanOrEqualTo(true, fieldName, value); + return this; + } + + /** + * <= + * + * @param condition 是否需要使用本条件 + * @param fieldName 字段名 + * @param value 值 + * @return this + */ + public QueryWrapper lessThanOrEqualTo(boolean condition, String fieldName, Comparable value) { + if (condition) { + tempPredicates.add(TempPredicate.builder().fieldName(fieldName) + .operator(Operator.LESSTHANOREQUALTO).value(value).build()); + } + return this; + } + /** * like '%xx%' * -- Gitee From 7b54037cdfa7ed4f512dc9e3c57457962017dbd2 Mon Sep 17 00:00:00 2001 From: aa Date: Fri, 10 Jan 2025 15:30:57 +0800 Subject: [PATCH 3/3] edit version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index db25bfa..0703f62 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ 4.0.0 com.fhs-opensource jpa-query-wrapper - 1.0.3 + 1.0.4 UTF-8 -- Gitee