Skip to content

Commit 3f2fd74

Browse files
committed
Taking benefit of mybatis/mybatis-3#2670
Should fix mybatis#14 and mybatis#15
1 parent d992ee4 commit 3f2fd74

File tree

1 file changed

+21
-34
lines changed

1 file changed

+21
-34
lines changed

src/main/java/org/mybatis/scripting/velocity/ParameterMappingCollector.java

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import java.util.Map;
2121

2222
import org.apache.ibatis.mapping.ParameterMapping;
23+
import org.apache.ibatis.mapping.ParameterMode;
24+
import org.apache.ibatis.reflection.MetaObject;
25+
import org.apache.ibatis.reflection.property.PropertyTokenizer;
2326
import org.apache.ibatis.session.Configuration;
2427

2528
public class ParameterMappingCollector {
@@ -28,15 +31,16 @@ public class ParameterMappingCollector {
2831
private final List<ParameterMapping> parameterMappings = new ArrayList<>();
2932
private final Map<String, Object> context;
3033
private final Configuration configuration;
34+
private final MetaObject metaParameters;
3135

32-
private int uid = 0;
3336
private String itemKey;
3437

3538
public ParameterMappingCollector(ParameterMapping[] newParameterMappingSources, Map<String, Object> newContext,
3639
Configuration newConfiguration) {
3740
this.parameterMappingSources = newParameterMappingSources;
3841
this.context = newContext;
3942
this.configuration = newConfiguration;
43+
this.metaParameters = configuration.newMetaObject(newContext);
4044
}
4145

4246
public void setItemKey(String value) {
@@ -49,54 +53,37 @@ public String getItemKey() {
4953

5054
public String g(int mapping) {
5155
ParameterMapping parameterMapping = this.parameterMappingSources[mapping];
52-
PropertyInfo vi = getPropertyInfo(parameterMapping.getProperty());
53-
if (vi.isIterable) {
54-
parameterMapping = itemize(parameterMapping, vi);
55-
this.context.put(vi.root, this.context.get(this.itemKey));
56-
}
57-
this.parameterMappings.add(parameterMapping);
56+
this.parameterMappings.add(mappingWithValue(parameterMapping));
5857
return "?";
5958
}
6059

6160
public List<ParameterMapping> getParameterMappings() {
6261
return this.parameterMappings;
6362
}
6463

65-
private ParameterMapping itemize(ParameterMapping source, PropertyInfo var) {
66-
StringBuilder sb = new StringBuilder().append("_RPTITEM_").append(this.uid++);
67-
var.root = sb.toString();
68-
String propertyName = sb.append(var.path).toString();
69-
ParameterMapping.Builder builder = new ParameterMapping.Builder(this.configuration, propertyName,
64+
private ParameterMapping mappingWithValue(ParameterMapping source) {
65+
String property = source.getProperty();
66+
ParameterMapping.Builder builder = new ParameterMapping.Builder(this.configuration, property,
7067
source.getJavaType());
7168
builder.expression(source.getExpression()).jdbcType(source.getJdbcType()).jdbcTypeName(source.getJdbcTypeName())
7269
.mode(source.getMode()).numericScale(source.getNumericScale()).resultMapId(source.getResultMapId())
7370
.typeHandler(source.getTypeHandler());
74-
return builder.build();
75-
}
7671

77-
private PropertyInfo getPropertyInfo(String name) {
78-
PropertyInfo i = new PropertyInfo();
79-
if (name != null) {
80-
int p = name.indexOf('.');
81-
if (p == -1) {
82-
i.root = name;
72+
PropertyTokenizer propertyTokenizer = new PropertyTokenizer(property);
73+
Object parameterObject = context.get(SQLScriptSource.PARAMETER_OBJECT_KEY);
74+
if (!ParameterMode.OUT.equals(source.getMode())) {
75+
if (metaParameters.hasGetter(propertyTokenizer.getName())) {
76+
builder.value(metaParameters.getValue(property));
77+
} else if (parameterObject == null) {
78+
builder.value(null);
79+
} else if (configuration.getTypeHandlerRegistry().hasTypeHandler(parameterObject.getClass())) {
80+
builder.value(parameterObject);
8381
} else {
84-
i.root = name.substring(0, p);
85-
i.path = name.substring(p);
82+
MetaObject metaObject = configuration.newMetaObject(parameterObject);
83+
builder.value(metaObject.getValue(property));
8684
}
8785
}
88-
i.isIterable = this.itemKey != null && this.itemKey.equals(i.root);
89-
return i;
90-
}
91-
92-
static class PropertyInfo {
93-
boolean isIterable = false;
94-
String root = "";
95-
String path = "";
96-
97-
public PropertyInfo() {
98-
// Prevent synthetic access
99-
}
86+
return builder.build();
10087
}
10188

10289
}

0 commit comments

Comments
 (0)