From a58ef88f1df3e391ee78f89d67af6a168687bb1d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 20 Nov 2017 13:33:50 +0100 Subject: [PATCH] SqlParameterSourceUtils.createBatch polishing (partial backport) Issue: SPR-16215 --- .../jdbc/core/BatchUpdateUtils.java | 4 +-- .../NamedParameterJdbcTemplate.java | 8 +---- .../namedparam/SqlParameterSourceUtils.java | 32 ++++++++++--------- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/BatchUpdateUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/BatchUpdateUtils.java index c0dfbc1ba4b..d18099a6490 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/BatchUpdateUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/BatchUpdateUtils.java @@ -21,8 +21,8 @@ import java.sql.SQLException; import java.util.List; /** - * Generic utility methods for working with JDBC batch statements. Mainly for internal use - * within the framework. + * Generic utility methods for working with JDBC batch statements. + * Mainly for internal use within the framework. * * @author Thomas Risberg * @since 3.0 diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java index 68e3491a784..9159d3c61dd 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java @@ -320,13 +320,7 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations @Override public int[] batchUpdate(String sql, Map[] batchValues) { - SqlParameterSource[] batchArgs = new SqlParameterSource[batchValues.length]; - int i = 0; - for (Map values : batchValues) { - batchArgs[i] = new MapSqlParameterSource(values); - i++; - } - return batchUpdate(sql, batchArgs); + return batchUpdate(sql, SqlParameterSourceUtils.createBatch(batchValues)); } @Override diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java index 7aeb335e835..e9c614c999f 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java @@ -22,8 +22,8 @@ import java.util.Map; import org.springframework.jdbc.core.SqlParameterValue; /** - * Class that provides helper methods for the use of {@link SqlParameterSource} - * with {@code SimpleJdbc} classes. + * Class that provides helper methods for the use of {@link SqlParameterSource}, + * in particular with {@link NamedParameterJdbcTemplate}. * * @author Thomas Risberg * @since 2.5 @@ -31,38 +31,40 @@ import org.springframework.jdbc.core.SqlParameterValue; public class SqlParameterSourceUtils { /** - * Create an array of MapSqlParameterSource objects populated with data from the - * values passed in. This will define what is included in a batch operation. - * @param valueMaps array of Maps containing the values to be used - * @return an array of SqlParameterSource + * Create an array of {@link MapSqlParameterSource} objects populated with data from + * the values passed in. This will define what is included in a batch operation. + * @param valueMaps array of {@link Map} instances containing the values to be used + * @return an array of {@link SqlParameterSource} + * @see MapSqlParameterSource + * @see NamedParameterJdbcTemplate#batchUpdate(String, Map[]) */ public static SqlParameterSource[] createBatch(Map[] valueMaps) { MapSqlParameterSource[] batch = new MapSqlParameterSource[valueMaps.length]; for (int i = 0; i < valueMaps.length; i++) { - Map valueMap = valueMaps[i]; - batch[i] = new MapSqlParameterSource(valueMap); + batch[i] = new MapSqlParameterSource(valueMaps[i]); } return batch; } /** - * Create an array of BeanPropertySqlParameterSource objects populated with data + * Create an array of {@link BeanPropertySqlParameterSource} objects populated with data * from the values passed in. This will define what is included in a batch operation. * @param beans object array of beans containing the values to be used - * @return an array of SqlParameterSource + * @return an array of {@link SqlParameterSource} + * @see BeanPropertySqlParameterSource + * @see NamedParameterJdbcTemplate#batchUpdate(String, SqlParameterSource[]) */ public static SqlParameterSource[] createBatch(Object[] beans) { BeanPropertySqlParameterSource[] batch = new BeanPropertySqlParameterSource[beans.length]; for (int i = 0; i < beans.length; i++) { - Object bean = beans[i]; - batch[i] = new BeanPropertySqlParameterSource(bean); + batch[i] = new BeanPropertySqlParameterSource(beans[i]); } return batch; } /** * Create a wrapped value if parameter has type information, plain object if not. - * @param source the source of paramer values and type information + * @param source the source of parameter values and type information * @param parameterName the name of the parameter * @return the value object */ @@ -83,13 +85,13 @@ public class SqlParameterSourceUtils { /** * Create a Map of case insensitive parameter names together with the original name. - * @param parameterSource the source of paramer names + * @param parameterSource the source of parameter names * @return the Map that can be used for case insensitive matching of parameter names */ public static Map extractCaseInsensitiveParameterNames(SqlParameterSource parameterSource) { Map caseInsensitiveParameterNames = new HashMap(); if (parameterSource instanceof BeanPropertySqlParameterSource) { - String[] propertyNames = ((BeanPropertySqlParameterSource)parameterSource).getReadablePropertyNames(); + String[] propertyNames = ((BeanPropertySqlParameterSource) parameterSource).getReadablePropertyNames(); for (String name : propertyNames) { caseInsensitiveParameterNames.put(name.toLowerCase(), name); }