@ -16,6 +16,8 @@
@@ -16,6 +16,8 @@
package org.springframework.jdbc.core.namedparam ;
import java.sql.PreparedStatement ;
import java.sql.SQLException ;
import java.util.LinkedHashMap ;
import java.util.List ;
import java.util.Map ;
@ -24,6 +26,7 @@ import javax.sql.DataSource;
@@ -24,6 +26,7 @@ import javax.sql.DataSource;
import org.springframework.dao.DataAccessException ;
import org.springframework.dao.support.DataAccessUtils ;
import org.springframework.jdbc.core.BatchPreparedStatementSetter ;
import org.springframework.jdbc.core.ColumnMapRowMapper ;
import org.springframework.jdbc.core.JdbcOperations ;
import org.springframework.jdbc.core.JdbcTemplate ;
@ -352,8 +355,26 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations
@@ -352,8 +355,26 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations
@Override
public int [ ] batchUpdate ( String sql , SqlParameterSource [ ] batchArgs ) {
return NamedParameterBatchUpdateUtils . executeBatchUpdateWithNamedParameters (
getParsedSql ( sql ) , batchArgs , getJdbcOperations ( ) ) ;
if ( batchArgs . length = = 0 ) {
return new int [ 0 ] ;
}
ParsedSql parsedSql = getParsedSql ( sql ) ;
PreparedStatementCreatorFactory pscf = getPreparedStatementCreatorFactory ( parsedSql , batchArgs [ 0 ] ) ;
return getJdbcOperations ( ) . batchUpdate (
pscf . getSql ( ) ,
new BatchPreparedStatementSetter ( ) {
@Override
public void setValues ( PreparedStatement ps , int i ) throws SQLException {
Object [ ] values = NamedParameterUtils . buildValueArray ( parsedSql , batchArgs [ i ] , null ) ;
pscf . newPreparedStatementSetter ( values ) . setValues ( ps ) ;
}
@Override
public int getBatchSize ( ) {
return batchArgs . length ;
}
} ) ;
}
@ -389,9 +410,7 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations
@@ -389,9 +410,7 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations
@Nullable Consumer < PreparedStatementCreatorFactory > customizer ) {
ParsedSql parsedSql = getParsedSql ( sql ) ;
String sqlToUse = NamedParameterUtils . substituteNamedParameters ( parsedSql , paramSource ) ;
List < SqlParameter > declaredParameters = NamedParameterUtils . buildSqlParameterList ( parsedSql , paramSource ) ;
PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory ( sqlToUse , declaredParameters ) ;
PreparedStatementCreatorFactory pscf = getPreparedStatementCreatorFactory ( parsedSql , paramSource ) ;
if ( customizer ! = null ) {
customizer . accept ( pscf ) ;
}
@ -419,4 +438,21 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations
@@ -419,4 +438,21 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations
}
}
/ * *
* Build a { @link PreparedStatementCreatorFactory } based on the given SQL and named parameters .
* @param parsedSql parsed representation of the given SQL statement
* @param paramSource container of arguments to bind
* @return the corresponding { @link PreparedStatementCreatorFactory }
* @since 5 . 1 . 3
* @see # getPreparedStatementCreator ( String , SqlParameterSource , Consumer )
* @see # getParsedSql ( String )
* /
protected PreparedStatementCreatorFactory getPreparedStatementCreatorFactory (
ParsedSql parsedSql , SqlParameterSource paramSource ) {
String sqlToUse = NamedParameterUtils . substituteNamedParameters ( parsedSql , paramSource ) ;
List < SqlParameter > declaredParameters = NamedParameterUtils . buildSqlParameterList ( parsedSql , paramSource ) ;
return new PreparedStatementCreatorFactory ( sqlToUse , declaredParameters ) ;
}
}