@@ -43,6 +43,8 @@ public class Example {
43
43
44
44
protected boolean exists ;
45
45
46
+ protected boolean notNull ;
47
+
46
48
protected Set <String > selectColumns ;
47
49
48
50
protected List <Criteria > oredCriteria ;
@@ -63,13 +65,25 @@ public Example(Class<?> entityClass) {
63
65
}
64
66
65
67
/**
66
- * 带exists参数的构造方法
68
+ * 带exists参数的构造方法,默认notNull为false,允许为空
67
69
*
68
70
* @param entityClass
69
71
* @param exists - true时,如果字段不存在就抛出异常,false时,如果不存在就不使用该字段的条件
70
72
*/
71
73
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 ) {
72
85
this .exists = exists ;
86
+ this .notNull = notNull ;
73
87
oredCriteria = new ArrayList <Criteria >();
74
88
this .entityClass = entityClass ;
75
89
table = EntityHelper .getEntityTable (entityClass );
@@ -146,7 +160,7 @@ public Criteria createCriteria() {
146
160
}
147
161
148
162
protected Criteria createCriteriaInternal () {
149
- Criteria criteria = new Criteria (propertyMap , exists );
163
+ Criteria criteria = new Criteria (propertyMap , exists , notNull );
150
164
return criteria ;
151
165
}
152
166
@@ -160,16 +174,15 @@ protected abstract static class GeneratedCriteria {
160
174
protected List <Criterion > criteria ;
161
175
//字段是否必须存在
162
176
protected boolean exists ;
177
+ //值是否不能为空
178
+ protected boolean notNull ;
163
179
//属性和列对应
164
180
protected Map <String , EntityColumn > propertyMap ;
165
181
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 ) {
171
183
super ();
172
184
this .exists = exists ;
185
+ this .notNull = notNull ;
173
186
criteria = new ArrayList <Criterion >();
174
187
this .propertyMap = propertyMap ;
175
188
}
@@ -218,7 +231,11 @@ protected void addCriterion(String condition) {
218
231
219
232
protected void addCriterion (String condition , Object value , String property ) {
220
233
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
+ }
222
239
}
223
240
if (property == null ) {
224
241
return ;
@@ -228,7 +245,11 @@ protected void addCriterion(String condition, Object value, String property) {
228
245
229
246
protected void addCriterion (String condition , Object value1 , Object value2 , String property ) {
230
247
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
+ }
232
253
}
233
254
if (property == null ) {
234
255
return ;
@@ -384,12 +405,9 @@ public Criteria andEqualTo(Object param) {
384
405
}
385
406
386
407
public static class Criteria extends GeneratedCriteria {
387
- protected Criteria (Map <String , EntityColumn > propertyMap ) {
388
- super (propertyMap );
389
- }
390
408
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 );
393
411
}
394
412
}
395
413
0 commit comments