20
20
import java .lang .reflect .Type ;
21
21
import java .sql .CallableStatement ;
22
22
import java .sql .ResultSet ;
23
- import java .sql .ResultSetMetaData ;
24
23
import java .sql .SQLException ;
25
24
import java .sql .Statement ;
26
25
import java .text .MessageFormat ;
68
67
import org .apache .ibatis .session .ResultHandler ;
69
68
import org .apache .ibatis .session .RowBounds ;
70
69
import org .apache .ibatis .type .JdbcType ;
71
- import org .apache .ibatis .type .TypeException ;
70
+ import org .apache .ibatis .type .ObjectTypeHandler ;
72
71
import org .apache .ibatis .type .TypeHandler ;
73
72
import org .apache .ibatis .type .TypeHandlerRegistry ;
74
73
@@ -110,8 +109,6 @@ public class DefaultResultSetHandler implements ResultSetHandler {
110
109
private final Map <String , List <UnMappedColumnAutoMapping >> autoMappingsCache = new HashMap <>();
111
110
private final Map <String , List <String >> constructorAutoMappingColumns = new HashMap <>();
112
111
113
- private final Map <CacheKey , TypeHandler <?>> typeHandlerCache = new HashMap <>();
114
-
115
112
// temporary marking flag that indicate using constructor mapping (use field to reduce memory usage)
116
113
private boolean useConstructorMappings ;
117
114
@@ -157,7 +154,6 @@ public void handleOutputParameters(CallableStatement cs) throws SQLException {
157
154
final Object parameterObject = parameterHandler .getParameterObject ();
158
155
final MetaObject metaParam = configuration .newMetaObject (parameterObject );
159
156
final List <ParameterMapping > parameterMappings = boundSql .getParameterMappings ();
160
- ResultSetMetaData rsmd = null ;
161
157
for (int i = 0 ; i < parameterMappings .size (); i ++) {
162
158
final ParameterMapping parameterMapping = parameterMappings .get (i );
163
159
if (parameterMapping .getMode () == ParameterMode .OUT || parameterMapping .getMode () == ParameterMode .INOUT ) {
@@ -167,18 +163,18 @@ public void handleOutputParameters(CallableStatement cs) throws SQLException {
167
163
final String property = parameterMapping .getProperty ();
168
164
TypeHandler <?> typeHandler = parameterMapping .getTypeHandler ();
169
165
if (typeHandler == null ) {
170
- Class <?> javaType = parameterMapping .getJavaType ();
166
+ Type javaType = parameterMapping .getJavaType ();
171
167
if (javaType == null || javaType == Object .class ) {
172
- metaParam .getGenericSetterType (property );
168
+ javaType = metaParam .getGenericSetterType (property ). getKey ( );
173
169
}
174
170
JdbcType jdbcType = parameterMapping .getJdbcType ();
175
- if (jdbcType == null ) {
176
- if (rsmd == null ) {
177
- rsmd = cs .getMetaData ();
171
+ typeHandler = typeHandlerRegistry .resolve (parameterObject .getClass (), javaType , jdbcType , null );
172
+ if (typeHandler == null ) {
173
+ typeHandler = typeHandlerRegistry .getTypeHandler (jdbcType );
174
+ if (typeHandler == null ) {
175
+ typeHandler = ObjectTypeHandler .INSTANCE ;
178
176
}
179
- jdbcType = JdbcType .forCode (rsmd .getColumnType (i + 1 ));
180
177
}
181
- typeHandler = typeHandlerRegistry .resolve (parameterObject .getClass (), javaType , jdbcType , null );
182
178
}
183
179
metaParam .setValue (property , typeHandler .getResult (cs , i + 1 ));
184
180
}
@@ -586,7 +582,6 @@ private boolean applyPropertyMappings(ResultSetWrapper rsw, ResultMap resultMap,
586
582
private Object getPropertyMappingValue (ResultSetWrapper rsw , MetaObject metaResultObject ,
587
583
ResultMapping propertyMapping , ResultLoaderMap lazyLoader , String columnPrefix ) throws SQLException {
588
584
final ResultSet rs = rsw .getResultSet ();
589
- final String property = propertyMapping .getProperty ();
590
585
if (propertyMapping .getNestedQueryId () != null ) {
591
586
return getNestedQueryMappingValue (rsw , metaResultObject , propertyMapping , lazyLoader , columnPrefix );
592
587
}
@@ -602,7 +597,9 @@ private Object getPropertyMappingValue(ResultSetWrapper rsw, MetaObject metaResu
602
597
final String column = prependPrefix (propertyMapping .getColumn (), columnPrefix );
603
598
TypeHandler <?> typeHandler = propertyMapping .getTypeHandler ();
604
599
if (typeHandler == null ) {
605
- typeHandler = resolvePropertyTypeHandler (rsw , metaResultObject , property , column );
600
+ final String property = propertyMapping .getProperty ();
601
+ final Type javaType = property == null ? null : metaResultObject .getGenericSetterType (property ).getKey ();
602
+ typeHandler = rsw .getTypeHandler (javaType , column );
606
603
}
607
604
return typeHandler .getResult (rs , column );
608
605
}
@@ -621,32 +618,6 @@ private List<Object> getNestedCursorValue(ResultSetWrapper rsw, ResultMapping pr
621
618
return results ;
622
619
}
623
620
624
- private TypeHandler <?> resolvePropertyTypeHandler (ResultSetWrapper rsw , MetaObject metaResultObject ,
625
- final String property , final String column ) {
626
- CacheKey typeHandlerCacheKey = new CacheKey ();
627
- Class <?> metaResultObjectClass = metaResultObject .getOriginalObject ().getClass ();
628
- typeHandlerCacheKey .update (metaResultObjectClass );
629
- typeHandlerCacheKey .update (column );
630
- typeHandlerCacheKey .update (property );
631
- return typeHandlerCache .computeIfAbsent (typeHandlerCacheKey , k -> {
632
- final JdbcType jdbcType = rsw .getJdbcType (column );
633
- final TypeHandler <?> th ;
634
- if (property == null ) {
635
- th = typeHandlerRegistry .getTypeHandler (jdbcType );
636
- } else {
637
- Type classToHandle = metaResultObject .getGenericSetterType (property ).getKey ();
638
- th = configuration .getTypeHandlerRegistry ().resolve (metaResultObjectClass , classToHandle , jdbcType , null );
639
- if (th == null ) {
640
- throw new TypeException (
641
- "No usable type handler found for mapping the result of column '" + column + "' to property '" + property
642
- + "'. It was either not specified and/or could not be found for the javaType (" + classToHandle
643
- + ") : jdbcType (" + jdbcType + ") combination." );
644
- }
645
- }
646
- return th ;
647
- });
648
- }
649
-
650
621
private List <UnMappedColumnAutoMapping > createAutomaticMappings (ResultSetWrapper rsw , ResultMap resultMap ,
651
622
MetaObject metaObject , String columnPrefix ) throws SQLException {
652
623
final String mapKey = resultMap .getId () + ":" + columnPrefix ;
@@ -675,9 +646,7 @@ private List<UnMappedColumnAutoMapping> createAutomaticMappings(ResultSetWrapper
675
646
continue ;
676
647
}
677
648
final Type propertyType = metaObject .getGenericSetterType (property ).getKey ();
678
- Class <?> metaObjectClass = metaObject .getOriginalObject ().getClass ();
679
- TypeHandler <?> typeHandler = configuration .getTypeHandlerRegistry ().resolve (metaObjectClass , propertyType ,
680
- rsw .getJdbcType (columnName ), null );
649
+ TypeHandler <?> typeHandler = rsw .getTypeHandler (propertyType , columnName );
681
650
if (typeHandler != null ) {
682
651
autoMapping .add (new UnMappedColumnAutoMapping (columnName , property , typeHandler ,
683
652
propertyType instanceof Class && ((Class <?>) propertyType ).isPrimitive ()));
@@ -1089,7 +1058,7 @@ private Object prepareSimpleKeyParameter(ResultSetWrapper rsw, ResultMapping res
1089
1058
String columnPrefix ) throws SQLException {
1090
1059
// parameterType is ignored in this case
1091
1060
final String columnName = prependPrefix (resultMapping .getColumn (), columnPrefix );
1092
- final TypeHandler <?> typeHandler = rsw .getTypeHandler (parameterType , columnName );
1061
+ final TypeHandler <?> typeHandler = rsw .getTypeHandler (null , columnName );
1093
1062
return typeHandler .getResult (rsw .getResultSet (), columnName );
1094
1063
}
1095
1064
@@ -1101,8 +1070,8 @@ private Object prepareCompositeKeyParameter(ResultSetWrapper rsw, ResultMapping
1101
1070
boolean foundValues = false ;
1102
1071
for (ResultMapping innerResultMapping : resultMapping .getComposites ()) {
1103
1072
final String columnName = prependPrefix (innerResultMapping .getColumn (), columnPrefix );
1104
- final TypeHandler <?> typeHandler = resolvePropertyTypeHandler ( rsw , metaObject , innerResultMapping . getColumn (),
1105
- columnPrefix );
1073
+ final TypeHandler <?> typeHandler = rsw
1074
+ . getTypeHandler ( metaObject . getGenericSetterType ( innerResultMapping . getProperty ()). getKey (), columnName );
1106
1075
final Object propValue = typeHandler .getResult (rsw .getResultSet (), columnName );
1107
1076
// issue #353 & #560 do not execute nested query if key is null
1108
1077
if (propValue != null ) {
0 commit comments