20
20
import java .util .Map ;
21
21
22
22
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 ;
23
26
import org .apache .ibatis .session .Configuration ;
24
27
25
28
public class ParameterMappingCollector {
@@ -28,15 +31,16 @@ public class ParameterMappingCollector {
28
31
private final List <ParameterMapping > parameterMappings = new ArrayList <>();
29
32
private final Map <String , Object > context ;
30
33
private final Configuration configuration ;
34
+ private final MetaObject metaParameters ;
31
35
32
- private int uid = 0 ;
33
36
private String itemKey ;
34
37
35
38
public ParameterMappingCollector (ParameterMapping [] newParameterMappingSources , Map <String , Object > newContext ,
36
39
Configuration newConfiguration ) {
37
40
this .parameterMappingSources = newParameterMappingSources ;
38
41
this .context = newContext ;
39
42
this .configuration = newConfiguration ;
43
+ this .metaParameters = configuration .newMetaObject (newContext );
40
44
}
41
45
42
46
public void setItemKey (String value ) {
@@ -49,54 +53,37 @@ public String getItemKey() {
49
53
50
54
public String g (int mapping ) {
51
55
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 ));
58
57
return "?" ;
59
58
}
60
59
61
60
public List <ParameterMapping > getParameterMappings () {
62
61
return this .parameterMappings ;
63
62
}
64
63
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 ,
70
67
source .getJavaType ());
71
68
builder .expression (source .getExpression ()).jdbcType (source .getJdbcType ()).jdbcTypeName (source .getJdbcTypeName ())
72
69
.mode (source .getMode ()).numericScale (source .getNumericScale ()).resultMapId (source .getResultMapId ())
73
70
.typeHandler (source .getTypeHandler ());
74
- return builder .build ();
75
- }
76
71
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 );
83
81
} 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 ) );
86
84
}
87
85
}
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 ();
100
87
}
101
88
102
89
}
0 commit comments