Search in sources :

Example 21 with ResultMapping

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

the class MapperAnnotationBuilder method applyConstructorArgs.

private void applyConstructorArgs(Arg[] args, Class<?> resultType, List<ResultMapping> resultMappings) {
    for (Arg arg : args) {
        List<ResultFlag> flags = new ArrayList<ResultFlag>();
        flags.add(ResultFlag.CONSTRUCTOR);
        if (arg.id()) {
            flags.add(ResultFlag.ID);
        }
        @SuppressWarnings("unchecked") Class<? extends TypeHandler<?>> typeHandler = (Class<? extends TypeHandler<?>>) (arg.typeHandler() == UnknownTypeHandler.class ? null : arg.typeHandler());
        ResultMapping resultMapping = assistant.buildResultMapping(resultType, nullOrEmpty(arg.name()), nullOrEmpty(arg.column()), arg.javaType() == void.class ? null : arg.javaType(), arg.jdbcType() == JdbcType.UNDEFINED ? null : arg.jdbcType(), nullOrEmpty(arg.select()), nullOrEmpty(arg.resultMap()), null, null, typeHandler, flags, null, null, false);
        resultMappings.add(resultMapping);
    }
}
Also used : UnknownTypeHandler(org.apache.ibatis.type.UnknownTypeHandler) ResultMapping(org.apache.ibatis.mapping.ResultMapping) Arg(org.apache.ibatis.annotations.Arg) ArrayList(java.util.ArrayList) ResultFlag(org.apache.ibatis.mapping.ResultFlag) TypeHandler(org.apache.ibatis.type.TypeHandler) UnknownTypeHandler(org.apache.ibatis.type.UnknownTypeHandler)

Example 22 with ResultMapping

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

the class MapperAnnotationBuilder method createDiscriminatorResultMaps.

private void createDiscriminatorResultMaps(String resultMapId, Class<?> resultType, TypeDiscriminator discriminator) {
    if (discriminator != null) {
        for (Case c : discriminator.cases()) {
            String caseResultMapId = resultMapId + "-" + c.value();
            List<ResultMapping> resultMappings = new ArrayList<ResultMapping>();
            // issue #136
            applyConstructorArgs(c.constructArgs(), resultType, resultMappings);
            applyResults(c.results(), resultType, resultMappings);
            // TODO add AutoMappingBehaviour
            assistant.addResultMap(caseResultMapId, c.type(), resultMapId, null, resultMappings, null);
        }
    }
}
Also used : ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) Case(org.apache.ibatis.annotations.Case)

Example 23 with ResultMapping

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

the class XMLMapperBuilder method buildResultMappingFromContext.

private ResultMapping buildResultMappingFromContext(XNode context, Class<?> resultType, List<ResultFlag> flags) throws Exception {
    String property;
    if (flags.contains(ResultFlag.CONSTRUCTOR)) {
        property = context.getStringAttribute("name");
    } else {
        property = context.getStringAttribute("property");
    }
    String column = context.getStringAttribute("column");
    String javaType = context.getStringAttribute("javaType");
    String jdbcType = context.getStringAttribute("jdbcType");
    String nestedSelect = context.getStringAttribute("select");
    String nestedResultMap = context.getStringAttribute("resultMap", processNestedResultMappings(context, Collections.<ResultMapping>emptyList()));
    String notNullColumn = context.getStringAttribute("notNullColumn");
    String columnPrefix = context.getStringAttribute("columnPrefix");
    String typeHandler = context.getStringAttribute("typeHandler");
    String resultSet = context.getStringAttribute("resultSet");
    String foreignColumn = context.getStringAttribute("foreignColumn");
    boolean lazy = "lazy".equals(context.getStringAttribute("fetchType", configuration.isLazyLoadingEnabled() ? "lazy" : "eager"));
    Class<?> javaTypeClass = resolveClass(javaType);
    @SuppressWarnings("unchecked") Class<? extends TypeHandler<?>> typeHandlerClass = (Class<? extends TypeHandler<?>>) resolveClass(typeHandler);
    JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
    return builderAssistant.buildResultMapping(resultType, property, column, javaTypeClass, jdbcTypeEnum, nestedSelect, nestedResultMap, notNullColumn, columnPrefix, typeHandlerClass, flags, resultSet, foreignColumn, lazy);
}
Also used : ResultMapping(org.apache.ibatis.mapping.ResultMapping) JdbcType(org.apache.ibatis.type.JdbcType) TypeHandler(org.apache.ibatis.type.TypeHandler)

Example 24 with ResultMapping

use of org.apache.ibatis.mapping.ResultMapping 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 25 with ResultMapping

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

the class MapperBuilderAssistant method parseCompositeColumnName.

private List<ResultMapping> parseCompositeColumnName(String columnName) {
    List<ResultMapping> composites = new ArrayList<ResultMapping>();
    if (columnName != null && (columnName.indexOf('=') > -1 || columnName.indexOf(',') > -1)) {
        StringTokenizer parser = new StringTokenizer(columnName, "{}=, ", false);
        while (parser.hasMoreTokens()) {
            String property = parser.nextToken();
            String column = parser.nextToken();
            ResultMapping complexResultMapping = new ResultMapping.Builder(configuration, property, column, configuration.getTypeHandlerRegistry().getUnknownTypeHandler()).build();
            composites.add(complexResultMapping);
        }
    }
    return composites;
}
Also used : StringTokenizer(java.util.StringTokenizer) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList)

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