Search in sources :

Example 26 with TypeHandlerRegistry

use of org.apache.ibatis.type.TypeHandlerRegistry in project mybatis-3 by mybatis.

the class ExecutorTestHelper method prepareSelectOneAuthorMappedStatement.

static MappedStatement prepareSelectOneAuthorMappedStatement(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    final ResultMap rm = new ResultMap.Builder(config, "defaultResultMap", Author.class, new ArrayList<ResultMapping>() {

        {
            add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)).build());
            add(new ResultMapping.Builder(config, "username", "username", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "password", "password", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "email", "email", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "bio", "bio", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "favouriteSection", "favourite_section", registry.getTypeHandler(Section.class)).build());
        }
    }).build();
    return new MappedStatement.Builder(config, "selectAuthor", new StaticSqlSource(config, "SELECT * FROM author WHERE id = ?"), SqlCommandType.SELECT).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(int.class)).build());
        }
    }).build()).resultMaps(new ArrayList<ResultMap>() {

        {
            add(rm);
        }
    }).cache(authorCache).build();
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ResultMap(org.apache.ibatis.mapping.ResultMap) ArrayList(java.util.ArrayList) Author(org.apache.ibatis.domain.blog.Author) ParameterMap(org.apache.ibatis.mapping.ParameterMap) Section(org.apache.ibatis.domain.blog.Section) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource)

Example 27 with TypeHandlerRegistry

use of org.apache.ibatis.type.TypeHandlerRegistry in project mybatis-3 by mybatis.

the class ExecutorTestHelper method prepareSelectDiscriminatedPost.

static MappedStatement prepareSelectDiscriminatedPost(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    final ResultMap discriminatorResultMap = new ResultMap.Builder(config, "postResultMap", HashMap.class, new ArrayList<ResultMapping>() {

        {
            add(new ResultMapping.Builder(config, "subject", "subject", registry.getTypeHandler(String.class)).build());
            add(new ResultMapping.Builder(config, "body", "body", registry.getTypeHandler(String.class)).build());
        }
    }).build();
    config.addResultMap(discriminatorResultMap);
    return new MappedStatement.Builder(config, "selectPosts", new StaticSqlSource(config, "SELECT * FROM post"), SqlCommandType.SELECT).resultMaps(new ArrayList<ResultMap>() {

        {
            add(new ResultMap.Builder(config, "defaultResultMap", HashMap.class, new ArrayList<ResultMapping>() {

                {
                    add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(int.class)).build());
                    add(new ResultMapping.Builder(config, "blog_id", "blog_id", registry.getTypeHandler(int.class)).build());
                }
            }).discriminator(new Discriminator.Builder(config, new ResultMapping.Builder(config, "section", "section", registry.getTypeHandler(String.class)).build(), new HashMap<String, String>() {

                {
                    put("NEWS", discriminatorResultMap.getId());
                    put("VIDEOS", discriminatorResultMap.getId());
                    put("PODCASTS", discriminatorResultMap.getId());
                // NEWS left out on purpose.
                }
            }).build()).build());
        }
    }).build();
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ResultMap(org.apache.ibatis.mapping.ResultMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource)

Example 28 with TypeHandlerRegistry

use of org.apache.ibatis.type.TypeHandlerRegistry in project mybatis-3 by mybatis.

the class ExecutorTestHelper method prepareSelectOneAuthorMappedStatementWithConstructorResults.

static MappedStatement prepareSelectOneAuthorMappedStatementWithConstructorResults(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    return new MappedStatement.Builder(config, "selectAuthor", new StaticSqlSource(config, "SELECT * FROM author WHERE id = ?"), SqlCommandType.SELECT).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(int.class)).build());
        }
    }).build()).resultMaps(new ArrayList<ResultMap>() {

        {
            add(new ResultMap.Builder(config, "defaultResultMap", Author.class, new ArrayList<ResultMapping>() {

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

                        {
                            add(ResultFlag.CONSTRUCTOR);
                        }
                    }).build());
                    add(new ResultMapping.Builder(config, "username", "username", registry.getTypeHandler(String.class)).build());
                    add(new ResultMapping.Builder(config, "password", "password", registry.getTypeHandler(String.class)).build());
                    add(new ResultMapping.Builder(config, "email", "email", registry.getTypeHandler(String.class)).build());
                    add(new ResultMapping.Builder(config, "bio", "bio", registry.getTypeHandler(String.class)).build());
                    add(new ResultMapping.Builder(config, "favouriteSection", "favourite_section", registry.getTypeHandler(Section.class)).build());
                }
            }).build());
        }
    }).cache(authorCache).build();
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ResultMap(org.apache.ibatis.mapping.ResultMap) ArrayList(java.util.ArrayList) ParameterMap(org.apache.ibatis.mapping.ParameterMap) Section(org.apache.ibatis.domain.blog.Section) Author(org.apache.ibatis.domain.blog.Author) ResultFlag(org.apache.ibatis.mapping.ResultFlag) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource)

Example 29 with TypeHandlerRegistry

use of org.apache.ibatis.type.TypeHandlerRegistry in project mybatis-3 by mybatis.

the class DefaultResultSetHandlerTest2 method shouldNotCallNextOnClosedResultSet_NestedResult.

@SuppressWarnings("serial")
@Test
void shouldNotCallNextOnClosedResultSet_NestedResult() throws Exception {
    final Configuration config = new Configuration();
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    final ResultMap nestedResultMap = new ResultMap.Builder(config, "roleMap", HashMap.class, new ArrayList<ResultMapping>() {

        {
            add(new ResultMapping.Builder(config, "role", "role", registry.getTypeHandler(String.class)).build());
        }
    }).build();
    config.addResultMap(nestedResultMap);
    final MappedStatement ms = new MappedStatement.Builder(config, "selectPerson", new StaticSqlSource(config, "select person..."), SqlCommandType.SELECT).resultMaps(new ArrayList<ResultMap>() {

        {
            add(new ResultMap.Builder(config, "personMap", HashMap.class, new ArrayList<ResultMapping>() {

                {
                    add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(Integer.class)).build());
                    add(new ResultMapping.Builder(config, "roles").nestedResultMapId("roleMap").build());
                }
            }).build());
        }
    }).resultOrdered(true).build();
    final Executor executor = null;
    final ParameterHandler parameterHandler = null;
    final ResultHandler<?> resultHandler = null;
    final BoundSql boundSql = null;
    final RowBounds rowBounds = new RowBounds(5, 1);
    final DefaultResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, ms, parameterHandler, resultHandler, boundSql, rowBounds);
    when(stmt.getResultSet()).thenReturn(rs);
    when(rsmd.getColumnCount()).thenReturn(2);
    when(rsmd.getColumnLabel(1)).thenReturn("id");
    when(rsmd.getColumnType(1)).thenReturn(Types.INTEGER);
    when(rsmd.getColumnClassName(1)).thenReturn(Integer.class.getCanonicalName());
    final List<Object> results = resultSetHandler.handleResultSets(stmt);
    assertEquals(0, results.size());
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ResultMap(org.apache.ibatis.mapping.ResultMap) Configuration(org.apache.ibatis.session.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RowBounds(org.apache.ibatis.session.RowBounds) ParameterHandler(org.apache.ibatis.executor.parameter.ParameterHandler) Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) ResultMapping(org.apache.ibatis.mapping.ResultMapping) MappedStatement(org.apache.ibatis.mapping.MappedStatement) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) Test(org.junit.jupiter.api.Test)

Example 30 with TypeHandlerRegistry

use of org.apache.ibatis.type.TypeHandlerRegistry in project mybatis-3 by mybatis.

the class DefaultResultSetHandlerTest2 method shouldNotCallNextOnClosedResultSet_SimpleResult.

@SuppressWarnings("serial")
@Test
void shouldNotCallNextOnClosedResultSet_SimpleResult() throws Exception {
    final Configuration config = new Configuration();
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    final MappedStatement ms = new MappedStatement.Builder(config, "testSelect", new StaticSqlSource(config, "some select statement"), SqlCommandType.SELECT).resultMaps(new ArrayList<ResultMap>() {

        {
            add(new ResultMap.Builder(config, "testMap", HashMap.class, new ArrayList<ResultMapping>() {

                {
                    add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(Integer.class)).build());
                }
            }).build());
        }
    }).build();
    final Executor executor = null;
    final ParameterHandler parameterHandler = null;
    final ResultHandler<?> resultHandler = null;
    final BoundSql boundSql = null;
    final RowBounds rowBounds = new RowBounds(5, 1);
    final DefaultResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, ms, parameterHandler, resultHandler, boundSql, rowBounds);
    when(stmt.getResultSet()).thenReturn(rs);
    when(rsmd.getColumnCount()).thenReturn(1);
    when(rsmd.getColumnLabel(1)).thenReturn("id");
    when(rsmd.getColumnType(1)).thenReturn(Types.INTEGER);
    when(rsmd.getColumnClassName(1)).thenReturn(Integer.class.getCanonicalName());
    when(stmt.getConnection()).thenReturn(conn);
    when(conn.getMetaData()).thenReturn(dbmd);
    // for simplicity.
    when(dbmd.supportsMultipleResultSets()).thenReturn(false);
    final List<Object> results = resultSetHandler.handleResultSets(stmt);
    assertEquals(0, results.size());
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) Configuration(org.apache.ibatis.session.Configuration) ArrayList(java.util.ArrayList) RowBounds(org.apache.ibatis.session.RowBounds) ParameterHandler(org.apache.ibatis.executor.parameter.ParameterHandler) Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) ResultMapping(org.apache.ibatis.mapping.ResultMapping) MappedStatement(org.apache.ibatis.mapping.MappedStatement) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource) Test(org.junit.jupiter.api.Test)

Aggregations

TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)33 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)22 ArrayList (java.util.ArrayList)18 MappedStatement (org.apache.ibatis.mapping.MappedStatement)16 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)15 ResultMap (org.apache.ibatis.mapping.ResultMap)12 ResultMapping (org.apache.ibatis.mapping.ResultMapping)10 Configuration (org.apache.ibatis.session.Configuration)9 Section (org.apache.ibatis.domain.blog.Section)8 ParameterMap (org.apache.ibatis.mapping.ParameterMap)8 Author (org.apache.ibatis.domain.blog.Author)7 MetaObject (org.apache.ibatis.reflection.MetaObject)7 ResultFlag (org.apache.ibatis.mapping.ResultFlag)6 DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)6 SqlSource (org.apache.ibatis.mapping.SqlSource)5 HashMap (java.util.HashMap)4 Blog (org.apache.ibatis.domain.blog.Blog)4 Test (org.junit.jupiter.api.Test)4 Date (java.util.Date)3 Comment (org.apache.ibatis.domain.blog.Comment)3