Search in sources :

Example 1 with TypeHandlerRegistry

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

the class Jdbc3KeyGenerator method processBatch.

public void processBatch(MappedStatement ms, Statement stmt, Collection<Object> parameters) {
    ResultSet rs = null;
    try {
        rs = stmt.getGeneratedKeys();
        final Configuration configuration = ms.getConfiguration();
        final TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
        final String[] keyProperties = ms.getKeyProperties();
        final ResultSetMetaData rsmd = rs.getMetaData();
        TypeHandler<?>[] typeHandlers = null;
        if (keyProperties != null && rsmd.getColumnCount() >= keyProperties.length) {
            for (Object parameter : parameters) {
                // there should be one row for each statement (also one for each parameter)
                if (!rs.next()) {
                    break;
                }
                final MetaObject metaParam = configuration.newMetaObject(parameter);
                if (typeHandlers == null) {
                    typeHandlers = getTypeHandlers(typeHandlerRegistry, metaParam, keyProperties, rsmd);
                }
                populateKeys(rs, metaParam, keyProperties, typeHandlers);
            }
        }
    } catch (Exception e) {
        throw new ExecutorException("Error getting generated key or setting result to parameter object. Cause: " + e, e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
            // ignore
            }
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ExecutorException(org.apache.ibatis.executor.ExecutorException) Configuration(org.apache.ibatis.session.Configuration) MetaObject(org.apache.ibatis.reflection.MetaObject) ResultSet(java.sql.ResultSet) MetaObject(org.apache.ibatis.reflection.MetaObject) TypeHandler(org.apache.ibatis.type.TypeHandler) ExecutorException(org.apache.ibatis.executor.ExecutorException) SQLException(java.sql.SQLException)

Example 2 with TypeHandlerRegistry

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

the class ExecutorTestHelper method prepareDeleteAuthorMappedStatement.

public static MappedStatement prepareDeleteAuthorMappedStatement(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    MappedStatement ms = new MappedStatement.Builder(config, "deleteAuthor", new StaticSqlSource(config, "DELETE FROM author WHERE id = ?"), SqlCommandType.DELETE).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(int.class)).build());
        }
    }).build()).cache(authorCache).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)

Example 3 with TypeHandlerRegistry

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

the class ExecutorTestHelper method prepareInsertAuthorProc.

public static MappedStatement prepareInsertAuthorProc(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    MappedStatement ms = new MappedStatement.Builder(config, "insertAuthorProc", new StaticSqlSource(config, "{call insertAuthor(?,?,?,?)}"), SqlCommandType.INSERT).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(int.class)).build());
            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());
        }
    }).build()).cache(authorCache).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)

Example 4 with TypeHandlerRegistry

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

the class ExecutorTestHelper method createSelectAuthorWithIDof99MappedStatement.

public static MappedStatement createSelectAuthorWithIDof99MappedStatement(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    MappedStatement ms = new MappedStatement.Builder(config, "selectAuthor", new StaticSqlSource(config, "SELECT * FROM author WHERE id = 99"), SqlCommandType.SELECT).statementType(StatementType.STATEMENT).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>()).build()).resultMaps(new ArrayList<ResultMap>() {

        {
            add(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());
                }
            }).build());
        }
    }).build();
    return ms;
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ArrayList(java.util.ArrayList) MappedStatement(org.apache.ibatis.mapping.MappedStatement) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource)

Example 5 with TypeHandlerRegistry

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

the class ExecutorTestHelper method prepareUpdateAuthorMappedStatement.

public static MappedStatement prepareUpdateAuthorMappedStatement(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    MappedStatement ms = new MappedStatement.Builder(config, "updateAuthor", new StaticSqlSource(config, "UPDATE author SET username = ?, password = ?, email = ?, bio = ? WHERE id = ?"), SqlCommandType.UPDATE).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, "id", registry.getTypeHandler(int.class)).build());
        }
    }).build()).cache(authorCache).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)

Aggregations

TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)32 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)14 ResultMap (org.apache.ibatis.mapping.ResultMap)12 ResultMapping (org.apache.ibatis.mapping.ResultMapping)10 Section (org.apache.ibatis.domain.blog.Section)8 ParameterMap (org.apache.ibatis.mapping.ParameterMap)8 Configuration (org.apache.ibatis.session.Configuration)8 Author (org.apache.ibatis.domain.blog.Author)7 ResultFlag (org.apache.ibatis.mapping.ResultFlag)6 MetaObject (org.apache.ibatis.reflection.MetaObject)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