Skip to content

Commit 7e2c812

Browse files
committed
精简根据主键查询时的where条件。
1 parent 6415a1e commit 7e2c812

File tree

2 files changed

+24
-37
lines changed

2 files changed

+24
-37
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424

2525
package com.github.abel533.entity;
2626

27+
import com.github.abel533.mapperhelper.EntityHelper;
2728
import com.github.abel533.mapperhelper.MapperTemplate;
2829
import org.apache.ibatis.jdbc.SQL;
2930
import org.apache.ibatis.reflection.MetaObject;
3031

3132
import java.util.List;
3233
import java.util.Map;
34+
import java.util.Set;
3335

3436
/**
3537
* 基础类
@@ -110,14 +112,29 @@ protected MetaObject getExample(Map<String, Object> params) {
110112
}
111113
//根据Example的结构,通过判断是否包含某些属性来判断条件是否为合法的example类型
112114
MetaObject example = MapperTemplate.forObject(result);
113-
if(example.hasGetter("orderByClause")
114-
&&example.hasGetter("oredCriteria")
115-
&&example.hasGetter("distinct")){
115+
if (example.hasGetter("orderByClause")
116+
&& example.hasGetter("oredCriteria")
117+
&& example.hasGetter("distinct")) {
116118
return example;
117119
}
118120
throw new IllegalArgumentException("Example参数不是合法的Mybatis Example对象!");
119121
}
120122

123+
/**
124+
* 根据主键查询
125+
*
126+
* @param sql
127+
* @param metaObject
128+
* @param columns
129+
* @param suffix
130+
*/
131+
protected void applyWherePk(SQL sql, MetaObject metaObject, Set<EntityHelper.EntityColumn> columns, String suffix) {
132+
for (EntityHelper.EntityColumn column : columns) {
133+
notNullKeyProperty(column.getProperty(), metaObject.getValue(column.getProperty()));
134+
sql.WHERE(column.getColumn() + "=#{" + (suffix != null ? suffix + "." : "") + column.getProperty() + "}");
135+
}
136+
}
137+
121138
/**
122139
* Example条件
123140
*/

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

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,7 @@ public String selectByPrimaryKey(final Map<String, Object> params) {
158158
notNullKeyProperty(column.getProperty(), entity);
159159
WHERE(column.getColumn() + "=#{key}");
160160
} else {
161-
MetaObject metaObject = MapperTemplate.forObject(entity);
162-
for (EntityHelper.EntityColumn column : entityTable.getEntityClassPKColumns()) {
163-
Object value = metaObject.getValue(column.getProperty());
164-
notNullKeyProperty(column.getProperty(), value);
165-
WHERE(column.getColumn() + "=#{key." + column.getProperty() + "}");
166-
}
161+
applyWherePk(this, MapperTemplate.forObject(entity), entityTable.getEntityClassPKColumns(), "key");
167162
}
168163
}}.toString();
169164
}
@@ -176,7 +171,6 @@ public String selectByPrimaryKey(final Map<String, Object> params) {
176171
*/
177172
public String insert(final Map<String, Object> params) {
178173
return new SQL() {{
179-
Object entity = getEntity(params);
180174
Class<?> entityClass = getEntityClass(params);
181175
EntityHelper.EntityTable entityTable = EntityHelper.getEntityTable(entityClass);
182176
INSERT_INTO(entityTable.getName());
@@ -259,12 +253,7 @@ public String deleteByPrimaryKey(final Map<String, Object> params) {
259253
notNullKeyProperty(column.getProperty(), entity);
260254
WHERE(column.getColumn() + "=#{key}");
261255
} else {
262-
MetaObject metaObject = MapperTemplate.forObject(entity);
263-
for (EntityHelper.EntityColumn column : entityTable.getEntityClassPKColumns()) {
264-
Object value = metaObject.getValue(column.getProperty());
265-
notNullKeyProperty(column.getProperty(), value);
266-
WHERE(column.getColumn() + "=#{key." + column.getProperty() + "}");
267-
}
256+
applyWherePk(this, MapperTemplate.forObject(entity), entityTable.getEntityClassPKColumns(), "key");
268257
}
269258
}}.toString();
270259
}
@@ -288,17 +277,7 @@ public String updateByPrimaryKey(final Map<String, Object> params) {
288277
SET(column.getColumn() + "=#{record." + column.getProperty() + "}");
289278
}
290279
}
291-
if (entityTable.getEntityClassPKColumns().size() == 1) {
292-
EntityHelper.EntityColumn column = entityTable.getEntityClassPKColumns().iterator().next();
293-
notNullKeyProperty(column.getProperty(), metaObject.getValue(column.getProperty()));
294-
WHERE(column.getColumn() + "=#{record." + column.getProperty() + "}");
295-
} else {
296-
for (EntityHelper.EntityColumn column : entityTable.getEntityClassPKColumns()) {
297-
Object value = metaObject.getValue(column.getProperty());
298-
notNullKeyProperty(column.getProperty(), value);
299-
WHERE(column.getColumn() + "=#{record." + column.getProperty() + "}");
300-
}
301-
}
280+
applyWherePk(this, metaObject, entityTable.getEntityClassPKColumns(), "record");
302281
}}.toString();
303282
}
304283

@@ -322,16 +301,7 @@ public String updateByPrimaryKeySelective(final Map<String, Object> params) {
322301
SET(column.getColumn() + "=#{record." + column.getProperty() + "}");
323302
}
324303
}
325-
if (entityTable.getEntityClassPKColumns().size() == 1) {
326-
EntityHelper.EntityColumn column = entityTable.getEntityClassPKColumns().iterator().next();
327-
notNullKeyProperty(column.getProperty(), metaObject.getValue(column.getProperty()));
328-
WHERE(column.getColumn() + "=#{record." + column.getProperty() + "}");
329-
} else {
330-
for (EntityHelper.EntityColumn column : entityTable.getEntityClassPKColumns()) {
331-
notNullKeyProperty(column.getProperty(), metaObject.getValue(column.getProperty()));
332-
WHERE(column.getColumn() + "=#{record." + column.getProperty() + "}");
333-
}
334-
}
304+
applyWherePk(this, metaObject, entityTable.getEntityClassPKColumns(), "record");
335305
}}.toString();
336306
}
337307

0 commit comments

Comments
 (0)