mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
fix-bug:#I86T6H 当条件满足忽略规则,when又设置为true,NPE异常
This commit is contained in:
parent
83a22fa5b0
commit
6ffb2b23a1
@ -46,9 +46,22 @@ public class QueryCondition implements CloneSupport<QueryCondition> {
|
|||||||
//两个条件直接的连接符
|
//两个条件直接的连接符
|
||||||
protected SqlConnector connector;
|
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() {
|
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<QueryCondition> {
|
|||||||
* @return {@link QueryCondition}
|
* @return {@link QueryCondition}
|
||||||
*/
|
*/
|
||||||
public QueryCondition when(boolean effective) {
|
public QueryCondition when(boolean effective) {
|
||||||
this.effective = effective;
|
if (notEmpty()) {
|
||||||
|
this.effective = effective;
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,7 +138,9 @@ public class QueryCondition implements CloneSupport<QueryCondition> {
|
|||||||
* @return {@link QueryCondition}
|
* @return {@link QueryCondition}
|
||||||
*/
|
*/
|
||||||
public QueryCondition when(BooleanSupplier fn) {
|
public QueryCondition when(BooleanSupplier fn) {
|
||||||
this.effective = fn.getAsBoolean();
|
if (notEmpty()) {
|
||||||
|
this.effective = fn.getAsBoolean();
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -20,7 +20,6 @@ import com.mybatisflex.core.constant.SqlConnector;
|
|||||||
import com.mybatisflex.core.query.*;
|
import com.mybatisflex.core.query.*;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -173,4 +172,16 @@ public class DynamicConditionTest {
|
|||||||
QueryColumnBehavior.setIgnoreFunction(Objects::isNull);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user