Search in sources :

Example 11 with ResultMap

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

the class MapperBuilderAssistant method addResultMap.

public ResultMap addResultMap(String id, Class<?> type, String extend, Discriminator discriminator, List<ResultMapping> resultMappings, Boolean autoMapping) {
    id = applyCurrentNamespace(id, false);
    extend = applyCurrentNamespace(extend, true);
    if (extend != null) {
        if (!configuration.hasResultMap(extend)) {
            throw new IncompleteElementException("Could not find a parent resultmap with id '" + extend + "'");
        }
        ResultMap resultMap = configuration.getResultMap(extend);
        List<ResultMapping> extendedResultMappings = new ArrayList<ResultMapping>(resultMap.getResultMappings());
        extendedResultMappings.removeAll(resultMappings);
        // Remove parent constructor if this resultMap declares a constructor.
        boolean declaresConstructor = false;
        for (ResultMapping resultMapping : resultMappings) {
            if (resultMapping.getFlags().contains(ResultFlag.CONSTRUCTOR)) {
                declaresConstructor = true;
                break;
            }
        }
        if (declaresConstructor) {
            Iterator<ResultMapping> extendedResultMappingsIter = extendedResultMappings.iterator();
            while (extendedResultMappingsIter.hasNext()) {
                if (extendedResultMappingsIter.next().getFlags().contains(ResultFlag.CONSTRUCTOR)) {
                    extendedResultMappingsIter.remove();
                }
            }
        }
        resultMappings.addAll(extendedResultMappings);
    }
    ResultMap resultMap = new ResultMap.Builder(configuration, id, type, resultMappings, autoMapping).discriminator(discriminator).build();
    configuration.addResultMap(resultMap);
    return resultMap;
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) ResultMapping(org.apache.ibatis.mapping.ResultMapping) CacheBuilder(org.apache.ibatis.mapping.CacheBuilder) ArrayList(java.util.ArrayList)

Example 12 with ResultMap

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

the class DefaultResultSetHandler method handleRowValuesForSimpleResultMap.

private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
    DefaultResultContext<Object> resultContext = new DefaultResultContext<Object>();
    skipRows(rsw.getResultSet(), rowBounds);
    while (shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) {
        ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, null);
        Object rowValue = getRowValue(rsw, discriminatedResultMap);
        storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
    }
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) MetaObject(org.apache.ibatis.reflection.MetaObject) DefaultResultContext(org.apache.ibatis.executor.result.DefaultResultContext)

Example 13 with ResultMap

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

the class DefaultResultSetHandler method handleRefCursorOutputParameter.

private void handleRefCursorOutputParameter(ResultSet rs, ParameterMapping parameterMapping, MetaObject metaParam) throws SQLException {
    if (rs == null) {
        return;
    }
    try {
        final String resultMapId = parameterMapping.getResultMapId();
        final ResultMap resultMap = configuration.getResultMap(resultMapId);
        final DefaultResultHandler resultHandler = new DefaultResultHandler(objectFactory);
        final ResultSetWrapper rsw = new ResultSetWrapper(rs, configuration);
        handleRowValues(rsw, resultMap, resultHandler, new RowBounds(), null);
        metaParam.setValue(parameterMapping.getProperty(), resultHandler.getResultList());
    } finally {
        // issue #228 (close resultsets)
        closeResultSet(rs);
    }
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) RowBounds(org.apache.ibatis.session.RowBounds) DefaultResultHandler(org.apache.ibatis.executor.result.DefaultResultHandler)

Example 14 with ResultMap

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

the class DefaultResultSetHandler method createRowKeyForMappedProperties.

private void createRowKeyForMappedProperties(ResultMap resultMap, ResultSetWrapper rsw, CacheKey cacheKey, List<ResultMapping> resultMappings, String columnPrefix) throws SQLException {
    for (ResultMapping resultMapping : resultMappings) {
        if (resultMapping.getNestedResultMapId() != null && resultMapping.getResultSet() == null) {
            // Issue #392
            final ResultMap nestedResultMap = configuration.getResultMap(resultMapping.getNestedResultMapId());
            createRowKeyForMappedProperties(nestedResultMap, rsw, cacheKey, nestedResultMap.getConstructorResultMappings(), prependPrefix(resultMapping.getColumnPrefix(), columnPrefix));
        } else if (resultMapping.getNestedQueryId() == null) {
            final String column = prependPrefix(resultMapping.getColumn(), columnPrefix);
            final TypeHandler<?> th = resultMapping.getTypeHandler();
            List<String> mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix);
            // Issue #114
            if (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH))) {
                final Object value = th.getResult(rsw.getResultSet(), column);
                if (value != null || configuration.isReturnInstanceForEmptyRow()) {
                    cacheKey.update(column);
                    cacheKey.update(value);
                }
            }
        }
    }
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) List(java.util.List) MetaObject(org.apache.ibatis.reflection.MetaObject) TypeHandler(org.apache.ibatis.type.TypeHandler)

Example 15 with ResultMap

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

the class DefaultResultSetHandler method handleCursorResultSets.

@Override
public <E> Cursor<E> handleCursorResultSets(Statement stmt) throws SQLException {
    ErrorContext.instance().activity("handling cursor results").object(mappedStatement.getId());
    ResultSetWrapper rsw = getFirstResultSet(stmt);
    List<ResultMap> resultMaps = mappedStatement.getResultMaps();
    int resultMapCount = resultMaps.size();
    validateResultMapsCount(rsw, resultMapCount);
    if (resultMapCount != 1) {
        throw new ExecutorException("Cursor results cannot be mapped to multiple resultMaps");
    }
    ResultMap resultMap = resultMaps.get(0);
    return new DefaultCursor<E>(this, resultMap, rsw, rowBounds);
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) ExecutorException(org.apache.ibatis.executor.ExecutorException) DefaultCursor(org.apache.ibatis.cursor.defaults.DefaultCursor)

Aggregations

ResultMap (org.apache.ibatis.mapping.ResultMap)22 ArrayList (java.util.ArrayList)15 MappedStatement (org.apache.ibatis.mapping.MappedStatement)12 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)11 ResultMapping (org.apache.ibatis.mapping.ResultMapping)11 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)11 ParameterMap (org.apache.ibatis.mapping.ParameterMap)9 Author (org.apache.ibatis.domain.blog.Author)7 ResultFlag (org.apache.ibatis.mapping.ResultFlag)7 Section (org.apache.ibatis.domain.blog.Section)6 MetaObject (org.apache.ibatis.reflection.MetaObject)6 DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)6 SqlSource (org.apache.ibatis.mapping.SqlSource)5 Blog (org.apache.ibatis.domain.blog.Blog)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Comment (org.apache.ibatis.domain.blog.Comment)3 Post (org.apache.ibatis.domain.blog.Post)3 Tag (org.apache.ibatis.domain.blog.Tag)3