Search in sources :

Example 1 with SelectKeyGenerator

use of org.apache.ibatis.executor.keygen.SelectKeyGenerator 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 2 with SelectKeyGenerator

use of org.apache.ibatis.executor.keygen.SelectKeyGenerator in project mybatis-3 by mybatis.

the class SimpleStatementHandler method update.

@Override
public int update(Statement statement) throws SQLException {
    String sql = boundSql.getSql();
    Object parameterObject = boundSql.getParameterObject();
    KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
    int rows;
    if (keyGenerator instanceof Jdbc3KeyGenerator) {
        statement.execute(sql, Statement.RETURN_GENERATED_KEYS);
        rows = statement.getUpdateCount();
        keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject);
    } else if (keyGenerator instanceof SelectKeyGenerator) {
        statement.execute(sql);
        rows = statement.getUpdateCount();
        keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject);
    } else {
        statement.execute(sql);
        rows = statement.getUpdateCount();
    }
    return rows;
}
Also used : Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) SelectKeyGenerator(org.apache.ibatis.executor.keygen.SelectKeyGenerator) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) KeyGenerator(org.apache.ibatis.executor.keygen.KeyGenerator) SelectKeyGenerator(org.apache.ibatis.executor.keygen.SelectKeyGenerator)

Example 3 with SelectKeyGenerator

use of org.apache.ibatis.executor.keygen.SelectKeyGenerator in project mybatis-3 by mybatis.

the class XMLStatementBuilder method parseSelectKeyNode.

private void parseSelectKeyNode(String id, XNode nodeToHandle, Class<?> parameterTypeClass, LanguageDriver langDriver, String databaseId) {
    String resultType = nodeToHandle.getStringAttribute("resultType");
    Class<?> resultTypeClass = resolveClass(resultType);
    StatementType statementType = StatementType.valueOf(nodeToHandle.getStringAttribute("statementType", StatementType.PREPARED.toString()));
    String keyProperty = nodeToHandle.getStringAttribute("keyProperty");
    String keyColumn = nodeToHandle.getStringAttribute("keyColumn");
    boolean executeBefore = "BEFORE".equals(nodeToHandle.getStringAttribute("order", "AFTER"));
    //defaults
    boolean useCache = false;
    boolean resultOrdered = 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 = langDriver.createSqlSource(configuration, nodeToHandle, parameterTypeClass);
    SqlCommandType sqlCommandType = SqlCommandType.SELECT;
    builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass, resultSetTypeEnum, flushCache, useCache, resultOrdered, keyGenerator, keyProperty, keyColumn, databaseId, langDriver, null);
    id = builderAssistant.applyCurrentNamespace(id, false);
    MappedStatement keyStatement = configuration.getMappedStatement(id, false);
    configuration.addKeyGenerator(id, new SelectKeyGenerator(keyStatement, executeBefore));
}
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) NoKeyGenerator(org.apache.ibatis.executor.keygen.NoKeyGenerator) Jdbc3KeyGenerator(org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator) KeyGenerator(org.apache.ibatis.executor.keygen.KeyGenerator) SelectKeyGenerator(org.apache.ibatis.executor.keygen.SelectKeyGenerator)

Example 4 with SelectKeyGenerator

use of org.apache.ibatis.executor.keygen.SelectKeyGenerator in project mybatis-3 by mybatis.

the class ExecutorTestHelper method prepareInsertAuthorMappedStatementWithBeforeAutoKey.

public static MappedStatement prepareInsertAuthorMappedStatementWithBeforeAutoKey(final Configuration config) {
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
    final ResultMap rm = new ResultMap.Builder(config, "keyResultMap", Integer.class, new ArrayList<ResultMapping>()).build();
    MappedStatement kms = new MappedStatement.Builder(config, "insertAuthor!selectKey", new StaticSqlSource(config, "SELECT 123456 as id FROM SYSIBM.SYSDUMMY1"), SqlCommandType.SELECT).keyProperty("id").resultMaps(new ArrayList<ResultMap>() {

        {
            add(rm);
        }
    }).build();
    config.addMappedStatement(kms);
    MappedStatement ms = new MappedStatement.Builder(config, "insertAuthor", new DynamicSqlSource(config, new TextSqlNode("INSERT INTO author (id,username,password,email,bio,favourite_section) values(#{id},#{username},#{password},#{email},#{bio:VARCHAR},#{favouriteSection})")), SqlCommandType.INSERT).parameterMap(new ParameterMap.Builder(config, "defaultParameterMap", Author.class, new ArrayList<ParameterMapping>() {

        {
            add(new ParameterMapping.Builder(config, "id", registry.getTypeHandler(Integer.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());
            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(new SelectKeyGenerator(kms, true)).keyProperty("id").build();
    return ms;
}
Also used : TypeHandlerRegistry(org.apache.ibatis.type.TypeHandlerRegistry) ResultMap(org.apache.ibatis.mapping.ResultMap) DynamicSqlSource(org.apache.ibatis.scripting.xmltags.DynamicSqlSource) ArrayList(java.util.ArrayList) SelectKeyGenerator(org.apache.ibatis.executor.keygen.SelectKeyGenerator) Section(org.apache.ibatis.domain.blog.Section) TextSqlNode(org.apache.ibatis.scripting.xmltags.TextSqlNode) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) MappedStatement(org.apache.ibatis.mapping.MappedStatement) StaticSqlSource(org.apache.ibatis.builder.StaticSqlSource)

Aggregations

SelectKeyGenerator (org.apache.ibatis.executor.keygen.SelectKeyGenerator)4 Jdbc3KeyGenerator (org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator)3 KeyGenerator (org.apache.ibatis.executor.keygen.KeyGenerator)3 MappedStatement (org.apache.ibatis.mapping.MappedStatement)3 NoKeyGenerator (org.apache.ibatis.executor.keygen.NoKeyGenerator)2 ResultSetType (org.apache.ibatis.mapping.ResultSetType)2 SqlCommandType (org.apache.ibatis.mapping.SqlCommandType)2 SqlSource (org.apache.ibatis.mapping.SqlSource)2 StatementType (org.apache.ibatis.mapping.StatementType)2 ArrayList (java.util.ArrayList)1 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)1 Section (org.apache.ibatis.domain.blog.Section)1 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)1 ResultMap (org.apache.ibatis.mapping.ResultMap)1 DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)1 TextSqlNode (org.apache.ibatis.scripting.xmltags.TextSqlNode)1 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)1