Skip to content

Commit 18fd7d9

Browse files
committed
Example构造方法增加notNull参数,默认false,允许值为null,值为null的时候不加入到条件中。
1 parent 1bc107d commit 18fd7d9

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src/main/java/tk/mybatis/mapper/entity/Condition.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ public Condition(Class<?> entityClass) {
3838
public Condition(Class<?> entityClass, boolean exists) {
3939
super(entityClass, exists);
4040
}
41+
42+
public Condition(Class<?> entityClass, boolean exists, boolean notNull) {
43+
super(entityClass, exists, notNull);
44+
}
4145
}

src/main/java/tk/mybatis/mapper/entity/Example.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class Example {
4343

4444
protected boolean exists;
4545

46+
protected boolean notNull;
47+
4648
protected Set<String> selectColumns;
4749

4850
protected List<Criteria> oredCriteria;
@@ -63,13 +65,25 @@ public Example(Class<?> entityClass) {
6365
}
6466

6567
/**
66-
* 带exists参数的构造方法
68+
* 带exists参数的构造方法,默认notNull为false,允许为空
6769
*
6870
* @param entityClass
6971
* @param exists - true时,如果字段不存在就抛出异常,false时,如果不存在就不使用该字段的条件
7072
*/
7173
public Example(Class<?> entityClass, boolean exists) {
74+
this(entityClass, exists, false);
75+
}
76+
77+
/**
78+
* 带exists参数的构造方法
79+
*
80+
* @param entityClass
81+
* @param exists - true时,如果字段不存在就抛出异常,false时,如果不存在就不使用该字段的条件
82+
* @param notNull - true时,如果值为空,就会抛出异常,false时,如果为空就不使用该字段的条件
83+
*/
84+
public Example(Class<?> entityClass, boolean exists, boolean notNull) {
7285
this.exists = exists;
86+
this.notNull = notNull;
7387
oredCriteria = new ArrayList<Criteria>();
7488
this.entityClass = entityClass;
7589
table = EntityHelper.getEntityTable(entityClass);
@@ -146,7 +160,7 @@ public Criteria createCriteria() {
146160
}
147161

148162
protected Criteria createCriteriaInternal() {
149-
Criteria criteria = new Criteria(propertyMap, exists);
163+
Criteria criteria = new Criteria(propertyMap, exists, notNull);
150164
return criteria;
151165
}
152166

@@ -160,16 +174,15 @@ protected abstract static class GeneratedCriteria {
160174
protected List<Criterion> criteria;
161175
//字段是否必须存在
162176
protected boolean exists;
177+
//值是否不能为空
178+
protected boolean notNull;
163179
//属性和列对应
164180
protected Map<String, EntityColumn> propertyMap;
165181

166-
protected GeneratedCriteria(Map<String, EntityColumn> propertyMap) {
167-
this(propertyMap, true);
168-
}
169-
170-
protected GeneratedCriteria(Map<String, EntityColumn> propertyMap, boolean exists) {
182+
protected GeneratedCriteria(Map<String, EntityColumn> propertyMap, boolean exists, boolean notNull) {
171183
super();
172184
this.exists = exists;
185+
this.notNull = notNull;
173186
criteria = new ArrayList<Criterion>();
174187
this.propertyMap = propertyMap;
175188
}
@@ -218,7 +231,11 @@ protected void addCriterion(String condition) {
218231

219232
protected void addCriterion(String condition, Object value, String property) {
220233
if (value == null) {
221-
throw new RuntimeException("Value for " + property + " cannot be null");
234+
if (notNull) {
235+
throw new RuntimeException("Value for " + property + " cannot be null");
236+
} else {
237+
return;
238+
}
222239
}
223240
if (property == null) {
224241
return;
@@ -228,7 +245,11 @@ protected void addCriterion(String condition, Object value, String property) {
228245

229246
protected void addCriterion(String condition, Object value1, Object value2, String property) {
230247
if (value1 == null || value2 == null) {
231-
throw new RuntimeException("Between values for " + property + " cannot be null");
248+
if (notNull) {
249+
throw new RuntimeException("Between values for " + property + " cannot be null");
250+
} else {
251+
return;
252+
}
232253
}
233254
if (property == null) {
234255
return;
@@ -384,12 +405,9 @@ public Criteria andEqualTo(Object param) {
384405
}
385406

386407
public static class Criteria extends GeneratedCriteria {
387-
protected Criteria(Map<String, EntityColumn> propertyMap) {
388-
super(propertyMap);
389-
}
390408

391-
protected Criteria(Map<String, EntityColumn> propertyMap, boolean exists) {
392-
super(propertyMap, exists);
409+
protected Criteria(Map<String, EntityColumn> propertyMap, boolean exists, boolean notNull) {
410+
super(propertyMap, exists, notNull);
393411
}
394412
}
395413

0 commit comments

Comments
 (0)