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

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

Introduction

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

Prototype

public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement,
            Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) 

Source Link

Usage

From source file:com.luxoft.mybatis.splitter.ReusingBatchExecutor.java

License:Apache License

public int doUpdate(MappedStatement ms, Object parameterObject) throws SQLException {
    final Configuration configuration = ms.getConfiguration();
    final StatementHandler handler = configuration.newStatementHandler(this, ms, parameterObject,
            RowBounds.DEFAULT, null, null);
    final BoundSql boundSql = handler.getBoundSql();
    PreparedStatementKey key = new PreparedStatementKey(boundSql.getSql(), ms);
    StatementData statementData = statementsData.get(key);
    if (retainExecuteOrder && statementData != null && !key.equals(lastKey)) {
        statementData = null;/*  w  w  w . j a v a  2s  . com*/
        executeUpTo(key, true);
    }
    if (statementData == null) {
        statementData = unusedStatementData.remove(key);
        if (statementData == null) {
            Connection connection = getConnection(ms.getStatementLog());
            Statement stmt = handler.prepare(connection);
            statementData = new StatementData(stmt);
        }
        statementsData.put(key, statementData);
    }
    lastKey = key;
    statementData.addParameterObject(parameterObject);
    handler.parameterize(statementData.getStatement());
    handler.batch(statementData.getStatement());
    return BATCH_UPDATE_RETURN_VALUE;
}

From source file:com.luxoft.mybatis.splitter.ReusingBatchExecutor.java

License:Apache License

public <E> List<E> doQuery(MappedStatement ms, Object parameterObject, RowBounds rowBounds,
        ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
    Statement stmt = null;/*from   ww w. j a  v  a 2 s .c  o  m*/
    try {
        flushStatements();
        Configuration configuration = ms.getConfiguration();
        StatementHandler handler = configuration.newStatementHandler(this, ms, parameterObject, rowBounds,
                resultHandler, boundSql);
        Connection connection = getConnection(ms.getStatementLog());
        stmt = handler.prepare(connection);
        handler.parameterize(stmt);
        return handler.<E>query(stmt, resultHandler);
    } finally {
        closeStatement(stmt);
    }
}

From source file:com.luxoft.mybatis.splitter.UpdateSplitterPlugin.java

License:Apache License

@Override
public Object intercept(Invocation invocation) throws Throwable {
    MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
    Object parameterObject = invocation.getArgs()[1];
    final Configuration configuration = ms.getConfiguration();
    final StatementHandler handler = configuration.newStatementHandler((Executor) invocation.getTarget(), ms,
            parameterObject, RowBounds.DEFAULT, null, null);
    final BoundSql boundSql = handler.getBoundSql();
    final String sql = boundSql.getSql();
    List<String> splitted = splitter.split(sql);
    int rc = 0;//from ww w.j a va  2 s. c o m
    List<ParameterMapping> fullParameterMappings = new ArrayList<ParameterMapping>(
            boundSql.getParameterMappings());
    for (String sqlPart : splitted) {
        if (skipEmptyStatements && sqlPart.length() == 0) {
            continue;
        }
        int numParams = 0;
        for (int index = sqlPart.indexOf('?'); index >= 0; index = sqlPart.indexOf('?', index + 1)) {
            numParams++;
        }
        MappedStatement subStatement = subStatements.get(ms);
        if (subStatement == null) {
            subStatement = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(),
                    new SwitchingSqlSource(configuration), ms.getSqlCommandType()).cache(ms.getCache())
                            .databaseId(ms.getDatabaseId()).fetchSize(ms.getFetchSize())
                            .timeout(ms.getTimeout()).flushCacheRequired(ms.isFlushCacheRequired())
                            .useCache(ms.isUseCache()).build();
            subStatements.put(ms, subStatement);
        }
        List<ParameterMapping> subParameterMappings = fullParameterMappings.subList(0, numParams);
        ((SwitchingSqlSource) subStatement.getSqlSource()).switchParams(sqlPart, boundSql,
                new ArrayList<ParameterMapping>(subParameterMappings));
        subParameterMappings.clear();
        int subRc = (Integer) invocation.getMethod().invoke(invocation.getTarget(), subStatement,
                parameterObject);
        if (rc >= 0) {
            rc = subRc < 0 ? subRc : rc + subRc;
        }
    }
    return rc;
}