Search in sources :

Example 16 with MappedStatement

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

the class ExecutorTestHelper method prepareInsertAuthorMappedStatementWithAutoKey.

public static MappedStatement prepareInsertAuthorMappedStatementWithAutoKey(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    MappedStatement ms = new MappedStatement.Builder(config, "insertAuthor", new StaticSqlSource(config, "INSERT INTO author (username,password,email,bio,favourite_section) values(?,?,?,?,?)"), SqlCommandType.INSERT).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "username", registry.getTypeHandler(String.class)).build());
            add(new ParameterMapping.Builder(config, "password", registry.getTypeHandler(String.class)).build());
            add(new ParameterMapping.Builder(config, "email", registry.getTypeHandler(String.class)).build());
            add(new ParameterMapping.Builder(config, "bio", registry.getTypeHandler(String.class)).jdbcType(JdbcType.VARCHAR).build());
            add(new ParameterMapping.Builder(config, "favouriteSection", registry.getTypeHandler(Section.class)).jdbcType(JdbcType.VARCHAR).build());
        }
    }).build()).cache(authorCache).keyGenerator(Jdbc3KeyGenerator.INSTANCE).keyProperty("id").build();
    return ms;
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MappedStatement(org.apache.ibatis.mapping.MappedStatement) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) Section(org.apache.ibatis.domain.blog.Section)

Example 17 with MappedStatement

use of org.apache.ibatis.mapping.MappedStatement 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)

Example 18 with MappedStatement

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

the class MapperMethod method executeWithResultHandler.

private void executeWithResultHandler(SqlSession sqlSession, Object[] args) {
    MappedStatement ms = sqlSession.getConfiguration().getMappedStatement(command.getName());
    if (void.class.equals(ms.getResultMaps().get(0).getType())) {
        throw new BindingException("method " + command.getName() + " needs either a @ResultMap annotation, a @ResultType annotation," + " or a resultType attribute in XML so a ResultHandler can be used as a parameter.");
    }
    Object param = method.convertArgsToSqlCommandParam(args);
    if (method.hasRowBounds()) {
        RowBounds rowBounds = method.extractRowBounds(args);
        sqlSession.select(command.getName(), param, rowBounds, method.extractResultHandler(args));
    } else {
        sqlSession.select(command.getName(), param, method.extractResultHandler(args));
    }
}
Also used : RowBounds(org.apache.ibatis.session.RowBounds) MetaObject(org.apache.ibatis.reflection.MetaObject) MappedStatement(org.apache.ibatis.mapping.MappedStatement)

Example 19 with MappedStatement

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

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

the class DefaultSqlSession method select.

@Override
public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
    try {
        MappedStatement ms = configuration.getMappedStatement(statement);
        executor.query(ms, wrapCollection(parameter), rowBounds, handler);
    } catch (Exception e) {
        throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);
    } finally {
        ErrorContext.instance().reset();
    }
}
Also used : MappedStatement(org.apache.ibatis.mapping.MappedStatement) BindingException(org.apache.ibatis.binding.BindingException) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) IOException(java.io.IOException) SQLException(java.sql.SQLException)

Aggregations

MappedStatement (org.apache.ibatis.mapping.MappedStatement)69 Test (org.junit.Test)27 Author (org.apache.ibatis.domain.blog.Author)19 BaseDataTest (org.apache.ibatis.BaseDataTest)18 JdbcTransaction (org.apache.ibatis.transaction.jdbc.JdbcTransaction)18 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)17 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)17 ArrayList (java.util.ArrayList)15 Configuration (org.apache.ibatis.session.Configuration)15 ResultMap (org.apache.ibatis.mapping.ResultMap)12 Statement (java.sql.Statement)10 StatementHandler (org.apache.ibatis.executor.statement.StatementHandler)9 ParameterMap (org.apache.ibatis.mapping.ParameterMap)9 Section (org.apache.ibatis.domain.blog.Section)8 BoundSql (org.apache.ibatis.mapping.BoundSql)8 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)8 ResultMapping (org.apache.ibatis.mapping.ResultMapping)7 SqlSource (org.apache.ibatis.mapping.SqlSource)7 RowBounds (org.apache.ibatis.session.RowBounds)7 Post (org.apache.ibatis.domain.blog.Post)6