Example usage for org.apache.ibatis.session Configuration getKeyGenerator

List of usage examples for org.apache.ibatis.session Configuration getKeyGenerator

Introduction

In this page you can find the example usage for org.apache.ibatis.session Configuration getKeyGenerator.

Prototype

public KeyGenerator getKeyGenerator(String id) 

Source Link

Usage

From source file:com.github.abel533.mapper.MapperTemplate.java

License:Open Source License

/**
 * SelectKey - ?mysqlOracle?// ww  w .  jav  a 2  s  .  com
 *
 * @param ms
 * @param column
 */
protected void newSelectKeyMappedStatement(MappedStatement ms, EntityHelper.EntityColumn column) {
    String keyId = ms.getId() + SelectKeyGenerator.SELECT_KEY_SUFFIX;
    if (ms.getConfiguration().hasKeyGenerator(keyId)) {
        return;
    }
    Class<?> entityClass = getSelectReturnType(ms);
    //defaults
    Configuration configuration = ms.getConfiguration();
    KeyGenerator keyGenerator = new NoKeyGenerator();
    Boolean executeBefore = getBEFORE();
    String IDENTITY = (column.getGenerator() == null || column.getGenerator().equals("")) ? getIDENTITY()
            : column.getGenerator();
    SqlSource sqlSource = new RawSqlSource(configuration, IDENTITY, entityClass);

    MappedStatement.Builder statementBuilder = new MappedStatement.Builder(configuration, keyId, sqlSource,
            SqlCommandType.SELECT);
    statementBuilder.resource(ms.getResource());
    statementBuilder.fetchSize(null);
    statementBuilder.statementType(StatementType.STATEMENT);
    statementBuilder.keyGenerator(keyGenerator);
    statementBuilder.keyProperty(column.getProperty());
    statementBuilder.keyColumn(null);
    statementBuilder.databaseId(null);
    statementBuilder.lang(configuration.getDefaultScriptingLanuageInstance());
    statementBuilder.resultOrdered(false);
    statementBuilder.resulSets(null);
    statementBuilder.timeout(configuration.getDefaultStatementTimeout());

    List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>();
    ParameterMap.Builder inlineParameterMapBuilder = new ParameterMap.Builder(configuration,
            statementBuilder.id() + "-Inline", entityClass, parameterMappings);
    statementBuilder.parameterMap(inlineParameterMapBuilder.build());

    List<ResultMap> resultMaps = new ArrayList<ResultMap>();
    ResultMap.Builder inlineResultMapBuilder = new ResultMap.Builder(configuration,
            statementBuilder.id() + "-Inline", int.class, new ArrayList<ResultMapping>(), null);
    resultMaps.add(inlineResultMapBuilder.build());
    statementBuilder.resultMaps(resultMaps);
    statementBuilder.resultSetType(null);

    statementBuilder.flushCacheRequired(false);
    statementBuilder.useCache(false);
    statementBuilder.cache(null);

    MappedStatement statement = statementBuilder.build();
    configuration.addMappedStatement(statement);

    MappedStatement keyStatement = configuration.getMappedStatement(keyId, false);
    configuration.addKeyGenerator(keyId, new SelectKeyGenerator(keyStatement, executeBefore));
    //keyGenerator
    try {
        MetaObject msObject = forObject(ms);
        msObject.setValue("keyGenerator", configuration.getKeyGenerator(keyId));
    } catch (Exception e) {
        //ignore
    }
}