Skip to content

Commit fdf99f6

Browse files
committed
通用Example增加了一个exists的参数,当true的时候如果使用的字段不存在会抛出异常,false时不抛出异常,但是不使用该字段的条件。
1 parent 3532353 commit fdf99f6

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

src/main/java/com/github/abel533/entity/Example.java

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class Example {
4141

4242
protected boolean distinct;
4343

44+
protected boolean exists;
45+
4446
protected List<Criteria> oredCriteria;
4547

4648
protected Class<?> entityClass;
@@ -49,7 +51,23 @@ public class Example {
4951
//属性和列对应
5052
protected Map<String, EntityHelper.EntityColumn> propertyMap;
5153

54+
/**
55+
* 默认exists为true
56+
*
57+
* @param entityClass
58+
*/
5259
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;
5371
oredCriteria = new ArrayList<Criteria>();
5472
this.entityClass = entityClass;
5573
table = EntityHelper.getEntityTable(entityClass);
@@ -102,7 +120,7 @@ public Criteria createCriteria() {
102120
}
103121

104122
protected Criteria createCriteriaInternal() {
105-
Criteria criteria = new Criteria(propertyMap);
123+
Criteria criteria = new Criteria(propertyMap, exists);
106124
return criteria;
107125
}
108126

@@ -114,28 +132,39 @@ public void clear() {
114132

115133
protected abstract static class GeneratedCriteria {
116134
protected List<Criterion> criteria;
135+
//字段是否必须存在
136+
protected boolean exists;
117137
//属性和列对应
118138
protected Map<String, EntityHelper.EntityColumn> propertyMap;
119139

120140
protected GeneratedCriteria(Map<String, EntityHelper.EntityColumn> propertyMap) {
141+
this(propertyMap, true);
142+
}
143+
144+
protected GeneratedCriteria(Map<String, EntityHelper.EntityColumn> propertyMap, boolean exists) {
121145
super();
146+
this.exists = exists;
122147
criteria = new ArrayList<Criterion>();
123148
this.propertyMap = propertyMap;
124149
}
125150

126151
private String column(String property) {
127152
if (propertyMap.containsKey(property)) {
128153
return propertyMap.get(property).getColumn();
129-
} else {
154+
} else if (exists) {
130155
throw new RuntimeException("当前实体类不包含名为" + property + "的属性!");
156+
} else {
157+
return null;
131158
}
132159
}
133160

134161
private String property(String property) {
135162
if (propertyMap.containsKey(property)) {
136163
return property;
137-
} else {
164+
} else if (exists) {
138165
throw new RuntimeException("当前实体类不包含名为" + property + "的属性!");
166+
} else {
167+
return null;
139168
}
140169
}
141170

@@ -155,20 +184,29 @@ protected void addCriterion(String condition) {
155184
if (condition == null) {
156185
throw new RuntimeException("Value for condition cannot be null");
157186
}
187+
if (condition.startsWith("null")) {
188+
return;
189+
}
158190
criteria.add(new Criterion(condition));
159191
}
160192

161193
protected void addCriterion(String condition, Object value, String property) {
162194
if (value == null) {
163195
throw new RuntimeException("Value for " + property + " cannot be null");
164196
}
197+
if (property == null) {
198+
return;
199+
}
165200
criteria.add(new Criterion(condition, value));
166201
}
167202

168203
protected void addCriterion(String condition, Object value1, Object value2, String property) {
169204
if (value1 == null || value2 == null) {
170205
throw new RuntimeException("Between values for " + property + " cannot be null");
171206
}
207+
if (property == null) {
208+
return;
209+
}
172210
criteria.add(new Criterion(condition, value1, value2));
173211
}
174212

@@ -244,10 +282,13 @@ public Criteria andNotLike(String property, String value) {
244282
}
245283

246284
public static class Criteria extends GeneratedCriteria {
247-
248285
protected Criteria(Map<String, EntityHelper.EntityColumn> propertyMap) {
249286
super(propertyMap);
250287
}
288+
289+
protected Criteria(Map<String, EntityHelper.EntityColumn> propertyMap, boolean exists) {
290+
super(propertyMap, exists);
291+
}
251292
}
252293

253294
public static class Criterion {

0 commit comments

Comments
 (0)