Skip to content

Commit e29242b

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

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
lines changed
Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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,36 @@ 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,
70-
source.getJavaType());
64+
private ParameterMapping mappingWithValue(ParameterMapping source) {
65+
String property = source.getProperty();
66+
ParameterMapping.Builder builder = new ParameterMapping.Builder(this.configuration, property, source.getJavaType());
7167
builder.expression(source.getExpression()).jdbcType(source.getJdbcType()).jdbcTypeName(source.getJdbcTypeName())
7268
.mode(source.getMode()).numericScale(source.getNumericScale()).resultMapId(source.getResultMapId())
7369
.typeHandler(source.getTypeHandler());
74-
return builder.build();
75-
}
7670

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;
71+
PropertyTokenizer propertyTokenizer = new PropertyTokenizer(property);
72+
Object parameterObject = context.get(SQLScriptSource.PARAMETER_OBJECT_KEY);
73+
if (!ParameterMode.OUT.equals(source.getMode())) {
74+
if (metaParameters.hasGetter(propertyTokenizer.getName())) {
75+
builder.value(metaParameters.getValue(property));
76+
} else if (parameterObject == null) {
77+
builder.value(null);
78+
} else if (configuration.getTypeHandlerRegistry().hasTypeHandler(parameterObject.getClass())) {
79+
builder.value(parameterObject);
8380
} else {
84-
i.root = name.substring(0, p);
85-
i.path = name.substring(p);
81+
MetaObject metaObject = configuration.newMetaObject(parameterObject);
82+
builder.value(metaObject.getValue(property));
8683
}
8784
}
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-
}
85+
return builder.build();
10086
}
10187

10288
}

0 commit comments

Comments
 (0)