Example usage for org.springframework.jdbc.object BatchSqlUpdate setSql

List of usage examples for org.springframework.jdbc.object BatchSqlUpdate setSql

Introduction

In this page you can find the example usage for org.springframework.jdbc.object BatchSqlUpdate setSql.

Prototype

public void setSql(@Nullable String sql) 

Source Link

Document

Set the SQL executed by this operation.

Usage

From source file:anyframe.core.query.impl.QueryServiceImpl.java

/**
 * BatchSqlUpdate ? ? ? SQL?  Batch  ./*from www  .j  av a2  s.  c  o m*/
 * 
 * @param sql
 *            query statement.
 * @param types
 *            is matched with input parameters. A type must belong to fields
 *            defined java.sql.Types package.
 * @param targets
 *            object of class which is matched with specified table in XML
 *            files. is the List type of Object Array.
 * @return an array of the number of rows affected by each statement.
 */
protected int[] batchExecutor(final String sql, int[] types, final List targets) throws QueryServiceException {
    if (targets.size() <= 0)
        throw new QueryServiceException(getMessageSource(), "error.query.runtime.batch");

    BatchSqlUpdate sqlBatch = new BatchSqlUpdate();
    sqlBatch.setJdbcTemplate(jdbcTemplate);
    sqlBatch.setSql(sql);

    for (int i = 0; types != null && i < types.length; i++) {
        SqlParameter sp = new SqlParameter(types[i]);
        sqlBatch.declareParameter(sp);
    }
    sqlBatch.compile();

    for (int i = 0; i < targets.size(); i++) {
        Object obj = targets.get(i);
        if (obj instanceof Object[])
            sqlBatch.update((Object[]) obj);
    }
    return sqlBatch.flush();
}