Search in sources :

Example 16 with ResultMapping

use of org.apache.ibatis.mapping.ResultMapping in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method createParameterizedResultObject.

Object createParameterizedResultObject(ResultSetWrapper rsw, Class<?> resultType, List<ResultMapping> constructorMappings, List<Class<?>> constructorArgTypes, List<Object> constructorArgs, String columnPrefix) {
    boolean foundValues = false;
    for (ResultMapping constructorMapping : constructorMappings) {
        final Class<?> parameterType = constructorMapping.getJavaType();
        final String column = constructorMapping.getColumn();
        final Object value;
        try {
            if (constructorMapping.getNestedQueryId() != null) {
                value = getNestedQueryConstructorValue(rsw.getResultSet(), constructorMapping, columnPrefix);
            } else if (constructorMapping.getNestedResultMapId() != null) {
                final ResultMap resultMap = configuration.getResultMap(constructorMapping.getNestedResultMapId());
                value = getRowValue(rsw, resultMap);
            } else {
                final TypeHandler<?> typeHandler = constructorMapping.getTypeHandler();
                value = typeHandler.getResult(rsw.getResultSet(), prependPrefix(column, columnPrefix));
            }
        } catch (ResultMapException e) {
            throw new ExecutorException("Could not process result for mapping: " + constructorMapping, e);
        } catch (SQLException e) {
            throw new ExecutorException("Could not process result for mapping: " + constructorMapping, e);
        }
        constructorArgTypes.add(parameterType);
        constructorArgs.add(value);
        foundValues = value != null || foundValues;
    }
    return foundValues ? objectFactory.create(resultType, constructorArgTypes, constructorArgs) : null;
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) ExecutorException(org.apache.ibatis.executor.ExecutorException) SQLException(java.sql.SQLException) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ResultMapException(org.apache.ibatis.executor.result.ResultMapException) MetaObject(org.apache.ibatis.reflection.MetaObject) TypeHandler(org.apache.ibatis.type.TypeHandler)

Example 17 with ResultMapping

use of org.apache.ibatis.mapping.ResultMapping in project mybatis-3 by mybatis.

the class XMLMapperBuilder method resultMapElement.

private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> additionalResultMappings) throws Exception {
    ErrorContext.instance().activity("processing " + resultMapNode.getValueBasedIdentifier());
    String id = resultMapNode.getStringAttribute("id", resultMapNode.getValueBasedIdentifier());
    String type = resultMapNode.getStringAttribute("type", resultMapNode.getStringAttribute("ofType", resultMapNode.getStringAttribute("resultType", resultMapNode.getStringAttribute("javaType"))));
    String extend = resultMapNode.getStringAttribute("extends");
    Boolean autoMapping = resultMapNode.getBooleanAttribute("autoMapping");
    Class<?> typeClass = resolveClass(type);
    Discriminator discriminator = null;
    List<ResultMapping> resultMappings = new ArrayList<ResultMapping>();
    resultMappings.addAll(additionalResultMappings);
    List<XNode> resultChildren = resultMapNode.getChildren();
    for (XNode resultChild : resultChildren) {
        if ("constructor".equals(resultChild.getName())) {
            processConstructorElement(resultChild, typeClass, resultMappings);
        } else if ("discriminator".equals(resultChild.getName())) {
            discriminator = processDiscriminatorElement(resultChild, typeClass, resultMappings);
        } else {
            List<ResultFlag> flags = new ArrayList<ResultFlag>();
            if ("id".equals(resultChild.getName())) {
                flags.add(ResultFlag.ID);
            }
            resultMappings.add(buildResultMappingFromContext(resultChild, typeClass, flags));
        }
    }
    ResultMapResolver resultMapResolver = new ResultMapResolver(builderAssistant, id, typeClass, extend, discriminator, resultMappings, autoMapping);
    try {
        return resultMapResolver.resolve();
    } catch (IncompleteElementException e) {
        configuration.addIncompleteResultMap(resultMapResolver);
        throw e;
    }
}
Also used : XNode(org.apache.ibatis.parsing.XNode) ArrayList(java.util.ArrayList) Discriminator(org.apache.ibatis.mapping.Discriminator) IncompleteElementException(org.apache.ibatis.builder.IncompleteElementException) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) List(java.util.List) ResultFlag(org.apache.ibatis.mapping.ResultFlag) ResultMapResolver(org.apache.ibatis.builder.ResultMapResolver)

Example 18 with ResultMapping

use of org.apache.ibatis.mapping.ResultMapping in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method applyPropertyMappings.

//
// PROPERTY MAPPINGS
//
private boolean applyPropertyMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, ResultLoaderMap lazyLoader, String columnPrefix) throws SQLException {
    final List<String> mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix);
    boolean foundValues = false;
    final List<ResultMapping> propertyMappings = resultMap.getPropertyResultMappings();
    for (ResultMapping propertyMapping : propertyMappings) {
        String column = prependPrefix(propertyMapping.getColumn(), columnPrefix);
        if (propertyMapping.getNestedResultMapId() != null) {
            // the user added a column attribute to a nested result map, ignore it
            column = null;
        }
        if (propertyMapping.isCompositeResult() || (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH))) || propertyMapping.getResultSet() != null) {
            Object value = getPropertyMappingValue(rsw.getResultSet(), metaObject, propertyMapping, lazyLoader, columnPrefix);
            // issue #541 make property optional
            final String property = propertyMapping.getProperty();
            if (property == null) {
                continue;
            } else if (value == DEFERED) {
                foundValues = true;
                continue;
            }
            if (value != null) {
                foundValues = true;
            }
            if (value != null || (configuration.isCallSettersOnNulls() && !metaObject.getSetterType(property).isPrimitive())) {
                // gcode issue #377, call setter on nulls (value is not 'found')
                metaObject.setValue(property, value);
            }
        }
    }
    return foundValues;
}
Also used : ResultMapping(org.apache.ibatis.mapping.ResultMapping) MetaObject(org.apache.ibatis.reflection.MetaObject)

Example 19 with ResultMapping

use of org.apache.ibatis.mapping.ResultMapping in project mybatis-3 by mybatis.

the class DefaultResultSetHandler method handleResultSets.

//
// HANDLE RESULT SETS
//
@Override
public List<Object> handleResultSets(Statement stmt) throws SQLException {
    ErrorContext.instance().activity("handling results").object(mappedStatement.getId());
    final List<Object> multipleResults = new ArrayList<Object>();
    int resultSetCount = 0;
    ResultSetWrapper rsw = getFirstResultSet(stmt);
    List<ResultMap> resultMaps = mappedStatement.getResultMaps();
    int resultMapCount = resultMaps.size();
    validateResultMapsCount(rsw, resultMapCount);
    while (rsw != null && resultMapCount > resultSetCount) {
        ResultMap resultMap = resultMaps.get(resultSetCount);
        handleResultSet(rsw, resultMap, multipleResults, null);
        rsw = getNextResultSet(stmt);
        cleanUpAfterHandlingResultSet();
        resultSetCount++;
    }
    String[] resultSets = mappedStatement.getResultSets();
    if (resultSets != null) {
        while (rsw != null && resultSetCount < resultSets.length) {
            ResultMapping parentMapping = nextResultMaps.get(resultSets[resultSetCount]);
            if (parentMapping != null) {
                String nestedResultMapId = parentMapping.getNestedResultMapId();
                ResultMap resultMap = configuration.getResultMap(nestedResultMapId);
                handleResultSet(rsw, resultMap, null, parentMapping);
            }
            rsw = getNextResultSet(stmt);
            cleanUpAfterHandlingResultSet();
            resultSetCount++;
        }
    }
    return collapseSingleResultList(multipleResults);
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) MetaObject(org.apache.ibatis.reflection.MetaObject)

Example 20 with ResultMapping

use of org.apache.ibatis.mapping.ResultMapping in project mybatis-3 by mybatis.

the class MapperAnnotationBuilder method applyResultMap.

private void applyResultMap(String resultMapId, Class<?> returnType, Arg[] args, Result[] results, TypeDiscriminator discriminator) {
    List<ResultMapping> resultMappings = new ArrayList<ResultMapping>();
    applyConstructorArgs(args, returnType, resultMappings);
    applyResults(results, returnType, resultMappings);
    Discriminator disc = applyDiscriminator(resultMapId, returnType, discriminator);
    // TODO add AutoMappingBehaviour
    assistant.addResultMap(resultMapId, returnType, null, disc, resultMappings, null);
    createDiscriminatorResultMaps(resultMapId, returnType, discriminator);
}
Also used : ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) TypeDiscriminator(org.apache.ibatis.annotations.TypeDiscriminator) Discriminator(org.apache.ibatis.mapping.Discriminator)

Aggregations

ResultMapping (org.apache.ibatis.mapping.ResultMapping)29 ArrayList (java.util.ArrayList)17 ResultMap (org.apache.ibatis.mapping.ResultMap)11 ResultFlag (org.apache.ibatis.mapping.ResultFlag)9 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)7 MappedStatement (org.apache.ibatis.mapping.MappedStatement)7 MetaObject (org.apache.ibatis.reflection.MetaObject)7 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)7 ParameterMap (org.apache.ibatis.mapping.ParameterMap)6 TypeHandler (org.apache.ibatis.type.TypeHandler)6 ExecutorException (org.apache.ibatis.executor.ExecutorException)5 SqlSource (org.apache.ibatis.mapping.SqlSource)5 DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)5 List (java.util.List)4 Blog (org.apache.ibatis.domain.blog.Blog)4 SQLException (java.sql.SQLException)3 Date (java.util.Date)3 CacheKey (org.apache.ibatis.cache.CacheKey)3 Author (org.apache.ibatis.domain.blog.Author)3 Comment (org.apache.ibatis.domain.blog.Comment)3