@@ -41,6 +41,8 @@ public class Example {
41
41
42
42
protected boolean distinct ;
43
43
44
+ protected boolean exists ;
45
+
44
46
protected List <Criteria > oredCriteria ;
45
47
46
48
protected Class <?> entityClass ;
@@ -49,7 +51,23 @@ public class Example {
49
51
//属性和列对应
50
52
protected Map <String , EntityHelper .EntityColumn > propertyMap ;
51
53
54
+ /**
55
+ * 默认exists为true
56
+ *
57
+ * @param entityClass
58
+ */
52
59
public Example (Class <?> entityClass ) {
60
+ this (entityClass , true );
61
+ }
62
+
63
+ /**
64
+ * 带exists参数的构造方法
65
+ *
66
+ * @param entityClass
67
+ * @param exists - true时,如果字段不存在就抛出异常,false时,如果不存在就不使用该字段的条件
68
+ */
69
+ public Example (Class <?> entityClass , boolean exists ) {
70
+ this .exists = exists ;
53
71
oredCriteria = new ArrayList <Criteria >();
54
72
this .entityClass = entityClass ;
55
73
table = EntityHelper .getEntityTable (entityClass );
@@ -102,7 +120,7 @@ public Criteria createCriteria() {
102
120
}
103
121
104
122
protected Criteria createCriteriaInternal () {
105
- Criteria criteria = new Criteria (propertyMap );
123
+ Criteria criteria = new Criteria (propertyMap , exists );
106
124
return criteria ;
107
125
}
108
126
@@ -114,28 +132,39 @@ public void clear() {
114
132
115
133
protected abstract static class GeneratedCriteria {
116
134
protected List <Criterion > criteria ;
135
+ //字段是否必须存在
136
+ protected boolean exists ;
117
137
//属性和列对应
118
138
protected Map <String , EntityHelper .EntityColumn > propertyMap ;
119
139
120
140
protected GeneratedCriteria (Map <String , EntityHelper .EntityColumn > propertyMap ) {
141
+ this (propertyMap , true );
142
+ }
143
+
144
+ protected GeneratedCriteria (Map <String , EntityHelper .EntityColumn > propertyMap , boolean exists ) {
121
145
super ();
146
+ this .exists = exists ;
122
147
criteria = new ArrayList <Criterion >();
123
148
this .propertyMap = propertyMap ;
124
149
}
125
150
126
151
private String column (String property ) {
127
152
if (propertyMap .containsKey (property )) {
128
153
return propertyMap .get (property ).getColumn ();
129
- } else {
154
+ } else if ( exists ) {
130
155
throw new RuntimeException ("当前实体类不包含名为" + property + "的属性!" );
156
+ } else {
157
+ return null ;
131
158
}
132
159
}
133
160
134
161
private String property (String property ) {
135
162
if (propertyMap .containsKey (property )) {
136
163
return property ;
137
- } else {
164
+ } else if ( exists ) {
138
165
throw new RuntimeException ("当前实体类不包含名为" + property + "的属性!" );
166
+ } else {
167
+ return null ;
139
168
}
140
169
}
141
170
@@ -155,20 +184,29 @@ protected void addCriterion(String condition) {
155
184
if (condition == null ) {
156
185
throw new RuntimeException ("Value for condition cannot be null" );
157
186
}
187
+ if (condition .startsWith ("null" )) {
188
+ return ;
189
+ }
158
190
criteria .add (new Criterion (condition ));
159
191
}
160
192
161
193
protected void addCriterion (String condition , Object value , String property ) {
162
194
if (value == null ) {
163
195
throw new RuntimeException ("Value for " + property + " cannot be null" );
164
196
}
197
+ if (property == null ) {
198
+ return ;
199
+ }
165
200
criteria .add (new Criterion (condition , value ));
166
201
}
167
202
168
203
protected void addCriterion (String condition , Object value1 , Object value2 , String property ) {
169
204
if (value1 == null || value2 == null ) {
170
205
throw new RuntimeException ("Between values for " + property + " cannot be null" );
171
206
}
207
+ if (property == null ) {
208
+ return ;
209
+ }
172
210
criteria .add (new Criterion (condition , value1 , value2 ));
173
211
}
174
212
@@ -244,10 +282,13 @@ public Criteria andNotLike(String property, String value) {
244
282
}
245
283
246
284
public static class Criteria extends GeneratedCriteria {
247
-
248
285
protected Criteria (Map <String , EntityHelper .EntityColumn > propertyMap ) {
249
286
super (propertyMap );
250
287
}
288
+
289
+ protected Criteria (Map <String , EntityHelper .EntityColumn > propertyMap , boolean exists ) {
290
+ super (propertyMap , exists );
291
+ }
251
292
}
252
293
253
294
public static class Criterion {
0 commit comments