Search in sources :

Example 6 with ResultMapping

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

the class MapperBuilderAssistant method buildDiscriminator.

public Discriminator buildDiscriminator(Class<?> resultType, String column, Class<?> javaType, JdbcType jdbcType, Class<? extends TypeHandler<?>> typeHandler, Map<String, String> discriminatorMap) {
    ResultMapping resultMapping = buildResultMapping(resultType, null, column, javaType, jdbcType, null, null, null, null, typeHandler, new ArrayList<ResultFlag>(), null, null, false);
    Map<String, String> namespaceDiscriminatorMap = new HashMap<String, String>();
    for (Map.Entry<String, String> e : discriminatorMap.entrySet()) {
        String resultMap = e.getValue();
        resultMap = applyCurrentNamespace(resultMap, true);
        namespaceDiscriminatorMap.put(e.getKey(), resultMap);
    }
    return new Discriminator.Builder(configuration, resultMapping, namespaceDiscriminatorMap).build();
}
Also used : HashMap(java.util.HashMap) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ResultFlag(org.apache.ibatis.mapping.ResultFlag) HashMap(java.util.HashMap) Map(java.util.Map) ParameterMap(org.apache.ibatis.mapping.ParameterMap) ResultMap(org.apache.ibatis.mapping.ResultMap) Discriminator(org.apache.ibatis.mapping.Discriminator)

Example 7 with ResultMapping

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

the class MapperAnnotationBuilder method applyResults.

private void applyResults(Result[] results, Class<?> resultType, List<ResultMapping> resultMappings) {
    for (Result result : results) {
        List<ResultFlag> flags = new ArrayList<ResultFlag>();
        if (result.id()) {
            flags.add(ResultFlag.ID);
        }
        @SuppressWarnings("unchecked") Class<? extends TypeHandler<?>> typeHandler = (Class<? extends TypeHandler<?>>) ((result.typeHandler() == UnknownTypeHandler.class) ? null : result.typeHandler());
        ResultMapping resultMapping = assistant.buildResultMapping(resultType, nullOrEmpty(result.property()), nullOrEmpty(result.column()), result.javaType() == void.class ? null : result.javaType(), result.jdbcType() == JdbcType.UNDEFINED ? null : result.jdbcType(), hasNestedSelect(result) ? nestedSelectId(result) : null, null, null, null, typeHandler, flags, null, null, isLazy(result));
        resultMappings.add(resultMapping);
    }
}
Also used : ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) ResultFlag(org.apache.ibatis.mapping.ResultFlag) TypeHandler(org.apache.ibatis.type.TypeHandler) UnknownTypeHandler(org.apache.ibatis.type.UnknownTypeHandler) Result(org.apache.ibatis.annotations.Result)

Example 8 with ResultMapping

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

the class DefaultResultSetHandler method createResultObject.

private Object createResultObject(ResultSetWrapper rsw, ResultMap resultMap, List<Class<?>> constructorArgTypes, List<Object> constructorArgs, String columnPrefix) throws SQLException {
    final Class<?> resultType = resultMap.getType();
    final MetaClass metaType = MetaClass.forClass(resultType, reflectorFactory);
    final List<ResultMapping> constructorMappings = resultMap.getConstructorResultMappings();
    if (hasTypeHandlerForResultObject(rsw, resultType)) {
        return createPrimitiveResultObject(rsw, resultMap, columnPrefix);
    } else if (!constructorMappings.isEmpty()) {
        return createParameterizedResultObject(rsw, resultType, constructorMappings, constructorArgTypes, constructorArgs, columnPrefix);
    } else if (resultType.isInterface() || metaType.hasDefaultConstructor()) {
        return objectFactory.create(resultType);
    } else if (shouldApplyAutomaticMappings(resultMap, false)) {
        return createByConstructorSignature(rsw, resultType, constructorArgTypes, constructorArgs, columnPrefix);
    }
    throw new ExecutorException("Do not know how to create an instance of " + resultType);
}
Also used : ExecutorException(org.apache.ibatis.executor.ExecutorException) MetaClass(org.apache.ibatis.reflection.MetaClass) ResultMapping(org.apache.ibatis.mapping.ResultMapping)

Example 9 with ResultMapping

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

the class DefaultResultSetHandler method prepareCompositeKeyParameter.

private Object prepareCompositeKeyParameter(ResultSet rs, ResultMapping resultMapping, Class<?> parameterType, String columnPrefix) throws SQLException {
    final Object parameterObject = instantiateParameterObject(parameterType);
    final MetaObject metaObject = configuration.newMetaObject(parameterObject);
    boolean foundValues = false;
    for (ResultMapping innerResultMapping : resultMapping.getComposites()) {
        final Class<?> propType = metaObject.getSetterType(innerResultMapping.getProperty());
        final TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(propType);
        final Object propValue = typeHandler.getResult(rs, prependPrefix(innerResultMapping.getColumn(), columnPrefix));
        // issue #353 & #560 do not execute nested query if key is null
        if (propValue != null) {
            metaObject.setValue(innerResultMapping.getProperty(), propValue);
            foundValues = true;
        }
    }
    return foundValues ? parameterObject : null;
}
Also used : MetaObject(org.apache.ibatis.reflection.MetaObject) ResultMapping(org.apache.ibatis.mapping.ResultMapping) MetaObject(org.apache.ibatis.reflection.MetaObject)

Example 10 with ResultMapping

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

the class DefaultResultSetHandler method getDiscriminatorValue.

private Object getDiscriminatorValue(ResultSet rs, Discriminator discriminator, String columnPrefix) throws SQLException {
    final ResultMapping resultMapping = discriminator.getResultMapping();
    final TypeHandler<?> typeHandler = resultMapping.getTypeHandler();
    return typeHandler.getResult(rs, prependPrefix(resultMapping.getColumn(), columnPrefix));
}
Also used : ResultMapping(org.apache.ibatis.mapping.ResultMapping)

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