Search in sources :

Example 1 with ResultFlag

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

use of org.apache.ibatis.mapping.ResultFlag in project jeesuite-libs by vakinge.

the class GetByPrimaryKeyBuilder method getResultMap.

/**
 * 生成当前实体的resultMap对象
 *
 * @param configuration
 * @return
 */
public static ResultMap getResultMap(Configuration configuration, Class<?> entityClass) {
    List<ResultMapping> resultMappings = new ArrayList<ResultMapping>();
    Set<ColumnMapper> entityClassColumns = EntityHelper.getEntityMapper(entityClass).getColumnsMapper();
    for (ColumnMapper entityColumn : entityClassColumns) {
        ResultMapping.Builder builder = new ResultMapping.Builder(configuration, entityColumn.getProperty(), entityColumn.getColumn(), entityColumn.getJavaType());
        if (entityColumn.getJdbcType() != null) {
            builder.jdbcType(entityColumn.getJdbcType());
        }
        List<ResultFlag> flags = new ArrayList<ResultFlag>();
        if (entityColumn.isId()) {
            flags.add(ResultFlag.ID);
        }
        builder.flags(flags);
        builder.lazy(false);
        resultMappings.add(builder.build());
    }
    ResultMap.Builder builder = new ResultMap.Builder(configuration, "BaseResultMap", entityClass, resultMappings, true);
    return builder.build();
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) ResultFlag(org.apache.ibatis.mapping.ResultFlag) ColumnMapper(com.jeesuite.mybatis.crud.helper.ColumnMapper)

Example 3 with ResultFlag

use of org.apache.ibatis.mapping.ResultFlag 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<>();
        if (result.id()) {
            flags.add(ResultFlag.ID);
        }
        @SuppressWarnings("unchecked") Class<? extends TypeHandler<?>> typeHandler = (Class<? extends TypeHandler<?>>) ((result.typeHandler() == UnknownTypeHandler.class) ? null : result.typeHandler());
        boolean hasNestedResultMap = hasNestedResultMap(result);
        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, hasNestedResultMap ? nestedResultMapId(result) : null, null, hasNestedResultMap ? findColumnPrefix(result) : 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 4 with ResultFlag

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

the class ExecutorTestHelper method prepareComplexSelectBlogMappedStatement.

static MappedStatement prepareComplexSelectBlogMappedStatement(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    final SqlSource sqlSource = new StaticSqlSource(config, "SELECT b.id, b.author_id, b.title, a.username, a.password, a.email, a.bio" + " FROM blog b" + " INNER JOIN author a ON b.author_id = a.id" + " WHERE b.id = ?");
    final ParameterMap parameterMap = new ParameterMap.Builder(config, "defaultParameterMap", int.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(int.class)).build());
        }
    }).build();
    final ResultMap resultMap = new ResultMap.Builder(config, "defaultResultMap", Blog.class, new ArrayList<ResultMapping>() {

        {
            add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)).flags(new ArrayList<ResultFlag>() {

                {
                    add(ResultFlag.ID);
                }
            }).build());
            add(new ResultMapping.Builder(config, "title", "title", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "author.id", "author_id", registry.getTypeHandler(int.class)).build());
            add(new ResultMapping.Builder(config, "author.username", "username", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "author.password", "password", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "author.email", "email", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "author.bio", "bio", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "posts", "id", registry.getTypeHandler(int.class)).javaType(List.class).nestedQueryId("selectPostsForBlog").build());
        }
    }).build();
    return new MappedStatement.Builder(config, "selectBlogById", sqlSource, SqlCommandType.SELECT).parameterMap(parameterMap).resultMaps(new ArrayList<ResultMap>() {

        {
            add(resultMap);
        }
    }).build();
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) SqlSource(org.apache.ibatis.mapping.SqlSource) ResultMap(org.apache.ibatis.mapping.ResultMap) ArrayList(java.util.ArrayList) ParameterMap(org.apache.ibatis.mapping.ParameterMap) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) List(java.util.List) MappedStatement(org.apache.ibatis.mapping.MappedStatement) ResultFlag(org.apache.ibatis.mapping.ResultFlag) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) Blog(org.apache.ibatis.domain.blog.Blog)

Example 5 with ResultFlag

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

the class ExecutorTestHelper method prepareSelectPostWithBlogByAuthorMappedStatement.

static MappedStatement prepareSelectPostWithBlogByAuthorMappedStatement(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    final SqlSource sqlSource = new StaticSqlSource(config, "SELECT p.id, p.created_on, p.blog_id, p.author_id, p.section, p.subject, p.body, pt.tag_id," + " t.name as tag_name, c.id as comment_id, c.name as comment_name, c.comment" + " FROM post p" + " LEFT OUTER JOIN post_tag pt ON pt.post_id = p.id" + " LEFT OUTER JOIN tag t ON pt.tag_id = t.id" + " LEFT OUTER JOIN comment c ON c.post_id = p.id" + " WHERE p.id = ?");
    final ParameterMap parameterMap = new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(int.class)).build());
        }
    }).build();
    final ResultMap tagResultMap = new ResultMap.Builder(config, "tagResultMap", Tag.class, new ArrayList<ResultMapping>() {

        {
            add(new ResultMapping.Builder(config, "id", "tag_id", registry.getTypeHandler(int.class)).flags(new ArrayList<ResultFlag>() {

                {
                    add(ResultFlag.ID);
                }
            }).build());
            add(new ResultMapping.Builder(config, "name", "tag_name", registry.getTypeHandler(String.class)).build());
        }
    }).build();
    final ResultMap commentResultMap = new ResultMap.Builder(config, "commentResultMap", Comment.class, new ArrayList<ResultMapping>() {

        {
            add(new ResultMapping.Builder(config, "id", "comment_id", registry.getTypeHandler(int.class)).flags(new ArrayList<ResultFlag>() {

                {
                    add(ResultFlag.ID);
                }
            }).build());
            add(new ResultMapping.Builder(config, "name", "comment_name", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "comment", "comment", registry.getTypeHandler(String.class)).build());
        }
    }).build();
    config.addResultMap(tagResultMap);
    config.addResultMap(commentResultMap);
    final ResultMap postResultMap = new ResultMap.Builder(config, "postResultMap", Post.class, new ArrayList<ResultMapping>() {

        {
            add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)).flags(new ArrayList<ResultFlag>() {

                {
                    add(ResultFlag.ID);
                }
            }).build());
            add(new ResultMapping.Builder(config, "blog").nestedQueryId("selectBlogByIdAndAuthor").composites(new ArrayList<ResultMapping>() {

                {
                    add(new ResultMapping.Builder(config, "authorId", "author_id", registry.getTypeHandler(int.class)).build());
                    add(new ResultMapping.Builder(config, "blogId", "blog_id", registry.getTypeHandler(int.class)).build());
                }
            }).build());
            add(new ResultMapping.Builder(config, "createdOn", "created_on", registry.getTypeHandler(Date.class)).build());
            add(new ResultMapping.Builder(config, "section", "section", registry.getTypeHandler(Section.class)).build());
            add(new ResultMapping.Builder(config, "subject", "subject", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "body", "body", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "tags").nestedResultMapId(tagResultMap.getId()).build());
            add(new ResultMapping.Builder(config, "comments").nestedResultMapId(commentResultMap.getId()).build());
        }
    }).build();
    return new MappedStatement.Builder(config, "selectPostsForBlog", sqlSource, SqlCommandType.SELECT).parameterMap(parameterMap).resultMaps(new ArrayList<ResultMap>() {

        {
            add(postResultMap);
        }
    }).build();
}
Also used : Comment(org.apache.ibatis.domain.blog.Comment) TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) SqlSource(org.apache.ibatis.mapping.SqlSource) ResultMap(org.apache.ibatis.mapping.ResultMap) Post(org.apache.ibatis.domain.blog.Post) ArrayList(java.util.ArrayList) ParameterMap(org.apache.ibatis.mapping.ParameterMap) Section(org.apache.ibatis.domain.blog.Section) Date(java.util.Date) ResultMapping(org.apache.ibatis.mapping.ResultMapping) Author(org.apache.ibatis.domain.blog.Author) Tag(org.apache.ibatis.domain.blog.Tag) MappedStatement(org.apache.ibatis.mapping.MappedStatement) ResultFlag(org.apache.ibatis.mapping.ResultFlag) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource)

Aggregations

ArrayList (java.util.ArrayList)11 ResultFlag (org.apache.ibatis.mapping.ResultFlag)11 ResultMapping (org.apache.ibatis.mapping.ResultMapping)9 ResultMap (org.apache.ibatis.mapping.ResultMap)7 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)6 ParameterMap (org.apache.ibatis.mapping.ParameterMap)6 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)6 MappedStatement (org.apache.ibatis.mapping.MappedStatement)5 SqlSource (org.apache.ibatis.mapping.SqlSource)5 DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)5 Author (org.apache.ibatis.domain.blog.Author)4 Blog (org.apache.ibatis.domain.blog.Blog)4 Section (org.apache.ibatis.domain.blog.Section)4 Date (java.util.Date)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 XNode (org.apache.ibatis.parsing.XNode)2 TypeHandler (org.apache.ibatis.type.TypeHandler)2