diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java index ff17cd2f..a927ce92 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java @@ -46,9 +46,22 @@ public class QueryCondition implements CloneSupport { //两个条件直接的连接符 protected SqlConnector connector; + /** + * 是否为空条件,默认false + */ + private boolean empty = false; + + protected boolean notEmpty() { + return !empty; + } + + protected QueryCondition setEmpty(boolean empty) { + this.empty = empty; + return this; + } public static QueryCondition createEmpty() { - return new QueryCondition().when(false); + return new QueryCondition().when(false).setEmpty(true); } @@ -111,7 +124,9 @@ public class QueryCondition implements CloneSupport { * @return {@link QueryCondition} */ public QueryCondition when(boolean effective) { - this.effective = effective; + if (notEmpty()) { + this.effective = effective; + } return this; } @@ -123,7 +138,9 @@ public class QueryCondition implements CloneSupport { * @return {@link QueryCondition} */ public QueryCondition when(BooleanSupplier fn) { - this.effective = fn.getAsBoolean(); + if (notEmpty()) { + this.effective = fn.getAsBoolean(); + } return this; } 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 84fc86f9..bfab8f3b 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 @@ -20,7 +20,6 @@ import com.mybatisflex.core.constant.SqlConnector; import com.mybatisflex.core.query.*; import com.mybatisflex.core.util.StringUtil; import org.junit.Test; - import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -173,4 +172,16 @@ public class DynamicConditionTest { QueryColumnBehavior.setIgnoreFunction(Objects::isNull); } + @Test + public void test10() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select() + .from(ACCOUNT) + // 满足忽略规则,但动态条件又是true + .where(ACCOUNT.USER_NAME.eq(null).when(true)); + String sql = queryWrapper.toSQL(); + System.out.println(sql); + assertEquals("SELECT * FROM `tb_account`", sql); + } + }