Search in sources :

Example 6 with SqlSource

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

the class MapperAnnotationBuilder method handleSelectKeyAnnotation.

private KeyGenerator handleSelectKeyAnnotation(SelectKey selectKeyAnnotation, String baseStatementId, Class<?> parameterTypeClass, LanguageDriver languageDriver) {
    String id = baseStatementId + SelectKeyGenerator.SELECT_KEY_SUFFIX;
    Class<?> resultTypeClass = selectKeyAnnotation.resultType();
    StatementType statementType = selectKeyAnnotation.statementType();
    String keyProperty = selectKeyAnnotation.keyProperty();
    String keyColumn = selectKeyAnnotation.keyColumn();
    boolean executeBefore = selectKeyAnnotation.before();
    // defaults
    boolean useCache = false;
    KeyGenerator keyGenerator = NoKeyGenerator.INSTANCE;
    Integer fetchSize = null;
    Integer timeout = null;
    boolean flushCache = false;
    String parameterMap = null;
    String resultMap = null;
    ResultSetType resultSetTypeEnum = null;
    SqlSource sqlSource = buildSqlSourceFromStrings(selectKeyAnnotation.statement(), parameterTypeClass, languageDriver);
    SqlCommandType sqlCommandType = SqlCommandType.SELECT;
    assistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass, resultSetTypeEnum, flushCache, useCache, false, keyGenerator, keyProperty, keyColumn, null, languageDriver, null);
    id = assistant.applyCurrentNamespace(id, false);
    MappedStatement keyStatement = configuration.getMappedStatement(id, false);
    SelectKeyGenerator answer = new SelectKeyGenerator(keyStatement, executeBefore);
    configuration.addKeyGenerator(id, answer);
    return answer;
}
Also used : SqlSource(org.apache.ibatis.mapping.SqlSource) ResultSetType(org.apache.ibatis.mapping.ResultSetType) StatementType(org.apache.ibatis.mapping.StatementType) SqlCommandType(org.apache.ibatis.mapping.SqlCommandType) SelectKeyGenerator(org.apache.ibatis.executor.keygen.SelectKeyGenerator) MappedStatement(org.apache.ibatis.mapping.MappedStatement) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) SelectKeyGenerator(org.apache.ibatis.executor.keygen.SelectKeyGenerator) NoKeyGenerator(org.apache.ibatis.executor.keygen.NoKeyGenerator) KeyGenerator(org.apache.ibatis.executor.keygen.KeyGenerator)

Example 7 with SqlSource

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

the class MapperAnnotationBuilder method parseStatement.

void parseStatement(Method method) {
    Class<?> parameterTypeClass = getParameterType(method);
    LanguageDriver languageDriver = getLanguageDriver(method);
    SqlSource sqlSource = getSqlSourceFromAnnotations(method, parameterTypeClass, languageDriver);
    if (sqlSource != null) {
        Options options = method.getAnnotation(Options.class);
        final String mappedStatementId = type.getName() + "." + method.getName();
        Integer fetchSize = null;
        Integer timeout = null;
        StatementType statementType = StatementType.PREPARED;
        ResultSetType resultSetType = ResultSetType.FORWARD_ONLY;
        SqlCommandType sqlCommandType = getSqlCommandType(method);
        boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
        boolean flushCache = !isSelect;
        boolean useCache = isSelect;
        KeyGenerator keyGenerator;
        String keyProperty = "id";
        String keyColumn = null;
        if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
            // first check for SelectKey annotation - that overrides everything else
            SelectKey selectKey = method.getAnnotation(SelectKey.class);
            if (selectKey != null) {
                keyGenerator = handleSelectKeyAnnotation(selectKey, mappedStatementId, getParameterType(method), languageDriver);
                keyProperty = selectKey.keyProperty();
            } else if (options == null) {
                keyGenerator = configuration.isUseGeneratedKeys() ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
            } else {
                keyGenerator = options.useGeneratedKeys() ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
                keyProperty = options.keyProperty();
                keyColumn = options.keyColumn();
            }
        } else {
            keyGenerator = NoKeyGenerator.INSTANCE;
        }
        if (options != null) {
            if (FlushCachePolicy.TRUE.equals(options.flushCache())) {
                flushCache = true;
            } else if (FlushCachePolicy.FALSE.equals(options.flushCache())) {
                flushCache = false;
            }
            useCache = options.useCache();
            //issue #348
            fetchSize = options.fetchSize() > -1 || options.fetchSize() == Integer.MIN_VALUE ? options.fetchSize() : null;
            timeout = options.timeout() > -1 ? options.timeout() : null;
            statementType = options.statementType();
            resultSetType = options.resultSetType();
        }
        String resultMapId = null;
        ResultMap resultMapAnnotation = method.getAnnotation(ResultMap.class);
        if (resultMapAnnotation != null) {
            String[] resultMaps = resultMapAnnotation.value();
            StringBuilder sb = new StringBuilder();
            for (String resultMap : resultMaps) {
                if (sb.length() > 0) {
                    sb.append(",");
                }
                sb.append(resultMap);
            }
            resultMapId = sb.toString();
        } else if (isSelect) {
            resultMapId = parseResultMap(method);
        }
        assistant.addMappedStatement(mappedStatementId, sqlSource, statementType, sqlCommandType, fetchSize, timeout, // ParameterMapID
        null, parameterTypeClass, resultMapId, getReturnType(method), resultSetType, flushCache, useCache, // TODO gcode issue #577
        false, keyGenerator, keyProperty, keyColumn, // DatabaseID
        null, languageDriver, // ResultSets
        options != null ? nullOrEmpty(options.resultSets()) : null);
    }
}
Also used : Options(org.apache.ibatis.annotations.Options) SqlSource(org.apache.ibatis.mapping.SqlSource) ResultMap(org.apache.ibatis.annotations.ResultMap) LanguageDriver(org.apache.ibatis.scripting.LanguageDriver) SelectKey(org.apache.ibatis.annotations.SelectKey) ResultSetType(org.apache.ibatis.mapping.ResultSetType) StatementType(org.apache.ibatis.mapping.StatementType) SqlCommandType(org.apache.ibatis.mapping.SqlCommandType) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) SelectKeyGenerator(org.apache.ibatis.executor.keygen.SelectKeyGenerator) NoKeyGenerator(org.apache.ibatis.executor.keygen.NoKeyGenerator) KeyGenerator(org.apache.ibatis.executor.keygen.KeyGenerator)

Example 8 with SqlSource

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

the class ExecutorTestHelper method prepareSelectPostWithBlogByAuthorMappedStatement.

public 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)

Example 9 with SqlSource

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

the class ExecutorTestHelper method prepareSelectBlogByIdAndAuthor.

public static MappedStatement prepareSelectBlogByIdAndAuthor(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 = ? and a.id = ?");
    final ParameterMap parameterMap = new ParameterMap.Builder(config, "defaultParameterMap", Map.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "blogId", registry.getTypeHandler(int.class)).build());
            add(new ParameterMapping.Builder(config, "authorId", 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, "selectBlogByIdAndAuthor", 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) HashMap(java.util.HashMap) Map(java.util.Map) ParameterMap(org.apache.ibatis.mapping.ParameterMap) ResultMap(org.apache.ibatis.mapping.ResultMap) Blog(org.apache.ibatis.domain.blog.Blog)

Example 10 with SqlSource

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

the class ExecutorTestHelper method prepareComplexSelectBlogMappedStatement.

public 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)

Aggregations

SqlSource (org.apache.ibatis.mapping.SqlSource)15 MappedStatement (org.apache.ibatis.mapping.MappedStatement)8 ArrayList (java.util.ArrayList)5 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)5 ParameterMap (org.apache.ibatis.mapping.ParameterMap)5 ResultFlag (org.apache.ibatis.mapping.ResultFlag)5 ResultMap (org.apache.ibatis.mapping.ResultMap)5 ResultMapping (org.apache.ibatis.mapping.ResultMapping)5 DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)5 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)5 Blog (org.apache.ibatis.domain.blog.Blog)4 Jdbc3KeyGenerator (org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator)4 KeyGenerator (org.apache.ibatis.executor.keygen.KeyGenerator)4 NoKeyGenerator (org.apache.ibatis.executor.keygen.NoKeyGenerator)4 SelectKeyGenerator (org.apache.ibatis.executor.keygen.SelectKeyGenerator)4 ResultSetType (org.apache.ibatis.mapping.ResultSetType)4 SqlCommandType (org.apache.ibatis.mapping.SqlCommandType)4 StatementType (org.apache.ibatis.mapping.StatementType)4 Date (java.util.Date)3 Map (java.util.Map)3