Search in sources :

Example 1 with UnknownTypeHandler

use of org.apache.ibatis.type.UnknownTypeHandler in project mybatis-3 by mybatis.

the class ResultSetWrapper method getTypeHandler.

/**
   * Gets the type handler to use when reading the result set.
   * Tries to get from the TypeHandlerRegistry by searching for the property type.
   * If not found it gets the column JDBC type and tries to get a handler for it.
   * 
   * @param propertyType
   * @param columnName
   * @return
   */
public TypeHandler<?> getTypeHandler(Class<?> propertyType, String columnName) {
    TypeHandler<?> handler = null;
    Map<Class<?>, TypeHandler<?>> columnHandlers = typeHandlerMap.get(columnName);
    if (columnHandlers == null) {
        columnHandlers = new HashMap<Class<?>, TypeHandler<?>>();
        typeHandlerMap.put(columnName, columnHandlers);
    } else {
        handler = columnHandlers.get(propertyType);
    }
    if (handler == null) {
        JdbcType jdbcType = getJdbcType(columnName);
        handler = typeHandlerRegistry.getTypeHandler(propertyType, jdbcType);
        // See issue #59 comment 10
        if (handler == null || handler instanceof UnknownTypeHandler) {
            final int index = columnNames.indexOf(columnName);
            final Class<?> javaType = resolveClass(classNames.get(index));
            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);
            }
        }
        if (handler == null || handler instanceof UnknownTypeHandler) {
            handler = new ObjectTypeHandler();
        }
        columnHandlers.put(propertyType, handler);
    }
    return handler;
}
Also used : ObjectTypeHandler(org.apache.ibatis.type.ObjectTypeHandler) UnknownTypeHandler(org.apache.ibatis.type.UnknownTypeHandler) JdbcType(org.apache.ibatis.type.JdbcType) UnknownTypeHandler(org.apache.ibatis.type.UnknownTypeHandler) TypeHandler(org.apache.ibatis.type.TypeHandler) ObjectTypeHandler(org.apache.ibatis.type.ObjectTypeHandler)

Aggregations

JdbcType (org.apache.ibatis.type.JdbcType)1 ObjectTypeHandler (org.apache.ibatis.type.ObjectTypeHandler)1 TypeHandler (org.apache.ibatis.type.TypeHandler)1 UnknownTypeHandler (org.apache.ibatis.type.UnknownTypeHandler)1