public class DefaultResultSetHandler implements ResultSetHandler {
// 延迟加载的标记
private static final Object DEFERRED = new Object();
private final Executor executor;
private final Configuration configuration;
private final MappedStatement mappedStatement;
// 内存分页用
private final RowBounds rowBounds;
// 参数处理器
private final ParameterHandler parameterHandler;
// 结果处理器(默认为null)
private final ResultHandler<?> resultHandler;
private final BoundSql boundSql;
// Cached Automappings
// 自动缓存的映射
private final Map<String, List<UnMappedColumnAutoMapping>> autoMappingsCache = new HashMap<>();
// temporary marking flag that indicate using constructor mapping (use field to reduce memory usage)
// 映射结果集时映射的对象是否要走构造器映射(<constructor>标签)
private boolean useConstructorMappings;
}
private ResultSetWrapper getFirstResultSet(Statement stmt) throws SQLException {
// 通常调用该方法就可以获取到ResultSet了
ResultSet rs = stmt.getResultSet();
// 但使用存储过程时,需要像迭代器一样先检查再获取
while (rs == null) {
// move forward to get the first resultset in case the driver
// doesn't return the resultset as the first result (HSQLDB 2.1)
if (stmt.getMoreResults()) {
rs = stmt.getResultSet();
} else {
if (stmt.getUpdateCount() == -1) {
// no more results. Must be no resultset
break;
}
}
}
return rs != null ? new ResultSetWrapper(rs, configuration) : null;
}
public ResultSetWrapper(ResultSet rs, Configuration configuration) throws SQLException {
this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
this.resultSet = rs;
final ResultSetMetaData metaData = rs.getMetaData();
final int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
columnNames.add(configuration.isUseColumnLabel() ? metaData.getColumnLabel(i) : metaData.getColumnName(i));
jdbcTypes.add(JdbcType.forCode(metaData.getColumnType(i)));
classNames.add(metaData.getColumnClassName(i));
}
}
private final List<String> columnNames = new ArrayList<>();
private final List<String> classNames = new ArrayList<>();
private final List<JdbcType> jdbcTypes = new ArrayList<>();
// 注意这是个双层Map
// 外层Map的key是属性名,内层的key是属性类型,value是处理该类型的TypeHandler实现类
// 换言之,这个typeHandlerMap的结构是 key-key-value
private final Map<String, Map<Class<?>, TypeHandler<?>>> typeHandlerMap = new HashMap<>();
public TypeHandler<?> getTypeHandler(Class<?> propertyType, String columnName) {
TypeHandler<?> handler = null;
// 先检查当前传入的属性是否有处理过
Map<Class<?>, TypeHandler<?>> columnHandlers = typeHandlerMap.get(columnName);
if (columnHandlers == null) {
// 没有的话,初始化一个内层的Map
columnHandlers = new HashMap<>();
typeHandlerMap.put(columnName, columnHandlers);
} else {
// 初始化过Map,那就查一下Map中有没有存TypeHandler
handler = columnHandlers.get(propertyType);
}
// 没有初始化过具体的TypeHandler
if (handler == null) {
JdbcType jdbcType = getJdbcType(columnName);
// 问TypeHandlerRegistry能不能搞定当前这个属性的类型
handler = typeHandlerRegistry.getTypeHandler(propertyType, jdbcType);
// 如果TypeHandlerRegistry还是搞不定,那就再尝试别的办法
if (handler == null || handler instanceof UnknownTypeHandler) {
// 根据ResultSet中提供的信息,获取到jdbc认为的类型
final int index = columnNames.indexOf(columnName);
final Class<?> javaType = resolveClass(classNames.get(index));
// 再问一次TypeHandlerRegistry能不能搞定
if (javaType != null && jdbcType != null) {
handler = typeHandlerRegistry.getTypeHandler(javaType, jdbcType);
} else if (javaType != null) {
handler = typeHandlerRegistry.getTypeHandler(javaType);
} else if (jdbcType != null) {
handler = typeHandlerRegistry.getTypeHandler(jdbcType);
}
}
// 如果还是搞不定,那就只能封装Object了
if (handler == null || handler instanceof UnknownTypeHandler) {
handler = new ObjectTypeHandler();
}
columnHandlers.put(propertyType, handler);
}
return handler;
}