From 1340e3b8c65ca17707db1d0338e86f45ec99672a Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 8 Mar 2024 12:50:07 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=E6=9B=B4=E5=A4=9A=20?= =?UTF-8?q?hasCondition=20=E6=B5=8B=E8=AF=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/core/query/QueryWrapper.java | 12 +----- .../coretest/DynamicConditionTest.java | 37 +++++++++++++------ 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java index 33673e51..5788516b 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java @@ -100,16 +100,8 @@ public class QueryWrapper extends BaseQueryWrapper { * @return {@code true} 包含条件,{@code false} 不包含条件。 */ public boolean hasCondition() { - // 无任何条件 - if (whereQueryCondition == null) { - return false; - } - // 第一个条件有效 - if (whereQueryCondition.checkEffective()) { - return true; - } - // 第一个条件无效时,查询之后是否有生效的条件 - return whereQueryCondition.getNextEffectiveCondition() != null; + QueryCondition c; + return (c = whereQueryCondition) != null && (c.checkEffective() || c.getNextEffectiveCondition() != null); } @SuppressWarnings("unchecked") diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java index b2530b0d..c655d1a1 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java @@ -319,24 +319,39 @@ public class DynamicConditionTest { @Test public void testHasCondition() { - QueryWrapper q1 = QueryWrapper.create(); - QueryWrapper q2 = QueryWrapper.create() + QueryWrapper queryWrapper = QueryWrapper.create(); + assertFalse(queryWrapper.hasCondition()); + + queryWrapper = QueryWrapper.create() .where(ACCOUNT.ID.eq(1)); - QueryWrapper q3 = QueryWrapper.create() + assertTrue(queryWrapper.hasCondition()); + + queryWrapper = QueryWrapper.create() + .where(ACCOUNT.ID.eq(1).and(ACCOUNT.AGE.eq(18))); + assertTrue(queryWrapper.hasCondition()); + + queryWrapper = QueryWrapper.create() + .where(ACCOUNT.ID.eq(1, false).and(ACCOUNT.AGE.eq(18))); + assertTrue(queryWrapper.hasCondition()); + + queryWrapper = QueryWrapper.create() + .where(ACCOUNT.ID.eq(1, false).and(ACCOUNT.AGE.eq(18, false))); + assertFalse(queryWrapper.hasCondition()); + + queryWrapper = QueryWrapper.create() .where(ACCOUNT.ID.eq(1, false)); - QueryWrapper q4 = QueryWrapper.create() + assertFalse(queryWrapper.hasCondition()); + + queryWrapper = QueryWrapper.create() .where(ACCOUNT.ID.eq(1, false)) .and(ACCOUNT.AGE.eq(18, false)); - QueryWrapper q5 = QueryWrapper.create() + assertFalse(queryWrapper.hasCondition()); + + queryWrapper = QueryWrapper.create() .where(ACCOUNT.ID.eq(1, false)) .and(ACCOUNT.AGE.eq(18)) .or(ACCOUNT.IS_DELETE.eq(0, false)); - - assertFalse(q1.hasCondition()); - assertTrue(q2.hasCondition()); - assertFalse(q3.hasCondition()); - assertFalse(q4.hasCondition()); - assertTrue(q5.hasCondition()); + assertTrue(queryWrapper.hasCondition()); } }