Merge pull request #4042 from sunshineflymeat/hutool-0829

Fix issue 4040
This commit is contained in:
Golden Looly
2025-08-29 12:00:13 +08:00
committed by GitHub
2 changed files with 31 additions and 5 deletions

View File

@@ -257,6 +257,17 @@ public class Condition extends CloneSupport<Condition> {
return OPERATOR_IS.equalsIgnoreCase(this.operator);
}
/**
* 是否 IS NOT条件
*
* @return 是否IS NOT条件
* @since 5.8.0
*/
public boolean isOperatorIsNot() {
return OPERATOR_IS_NOT.equalsIgnoreCase(this.operator);
}
/**
* 是否LIKE条件
*
@@ -268,18 +279,24 @@ public class Condition extends CloneSupport<Condition> {
}
/**
* 检查值是否为null如果为null转换为 "IS NULL"形式
* 检查值是否为null如果为null转换为 "IS NULL"或"IS NOT NULL"形式
*
* @return this
*/
public Condition checkValueNull() {
if (null == this.value) {
this.operator = OPERATOR_IS;
// 检查是否为"不等于"操作符(包括!=和<>
if ("!=".equalsIgnoreCase(this.operator) || "<>".equalsIgnoreCase(this.operator)) {
this.operator = OPERATOR_IS_NOT;
} else {
this.operator = OPERATOR_IS;
}
this.value = VALUE_NULL;
}
return this;
}
/**
* 获得between 类型中第二个值
*
@@ -345,8 +362,8 @@ public class Condition extends CloneSupport<Condition> {
// 类似:" (?,?,?)" 或者 " (1,2,3,4)"
buildValuePartForIN(conditionStrBuilder, paramValues);
} else {
if (isPlaceHolder() && false == isOperatorIs()) {
// 使用条件表达式占位符,条件表达式并不适用于 IS NULL
if (isPlaceHolder() && !isOperatorIs() && !isOperatorIsNot()) {
// 使用条件表达式占位符,条件表达式并不适用于 IS NULL 和 IS NOT NULL
conditionStrBuilder.append(" ?");
if (null != paramValues) {
paramValues.add(this.value);
@@ -355,7 +372,7 @@ public class Condition extends CloneSupport<Condition> {
// 直接使用条件值
final String valueStr = String.valueOf(this.value);
conditionStrBuilder.append(" ").append(isOperatorLike() ?
StrUtil.wrap(valueStr, "'") : valueStr);
StrUtil.wrap(valueStr, "'") : valueStr);
}
}

View File

@@ -12,9 +12,18 @@ public class ConditionTest {
Condition conditionNull = new Condition("user", null);
assertEquals("user IS NULL", conditionNull.toString());
Condition conditionNull2 = new Condition("user", "=", (String) null);
assertEquals("user IS NULL", conditionNull2.toString());
Condition conditionNotNull = new Condition("user", "!= null");
assertEquals("user IS NOT NULL", conditionNotNull.toString());
Condition conditionNotNull2 = new Condition("user", "!=", (String) null);
assertEquals("user IS NOT NULL", conditionNotNull2.toString());
Condition conditionNotNull3 = new Condition("user", "<>", (String) null);
assertEquals("user IS NOT NULL", conditionNotNull3.toString());
Condition condition2 = new Condition("user", "= zhangsan");
assertEquals("user = ?", condition2.toString());