Skip to content

Commit a58ef88

Browse files
committed
SqlParameterSourceUtils.createBatch polishing (partial backport)
Issue: SPR-16215
1 parent 1d060ec commit a58ef88

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/BatchUpdateUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import java.util.List;
2222

2323
/**
24-
* Generic utility methods for working with JDBC batch statements. Mainly for internal use
25-
* within the framework.
24+
* Generic utility methods for working with JDBC batch statements.
25+
* Mainly for internal use within the framework.
2626
*
2727
* @author Thomas Risberg
2828
* @since 3.0

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,7 @@ public int update(
320320

321321
@Override
322322
public int[] batchUpdate(String sql, Map<String, ?>[] batchValues) {
323-
SqlParameterSource[] batchArgs = new SqlParameterSource[batchValues.length];
324-
int i = 0;
325-
for (Map<String, ?> values : batchValues) {
326-
batchArgs[i] = new MapSqlParameterSource(values);
327-
i++;
328-
}
329-
return batchUpdate(sql, batchArgs);
323+
return batchUpdate(sql, SqlParameterSourceUtils.createBatch(batchValues));
330324
}
331325

332326
@Override

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,49 @@
2222
import org.springframework.jdbc.core.SqlParameterValue;
2323

2424
/**
25-
* Class that provides helper methods for the use of {@link SqlParameterSource}
26-
* with {@code SimpleJdbc} classes.
25+
* Class that provides helper methods for the use of {@link SqlParameterSource},
26+
* in particular with {@link NamedParameterJdbcTemplate}.
2727
*
2828
* @author Thomas Risberg
2929
* @since 2.5
3030
*/
3131
public class SqlParameterSourceUtils {
3232

3333
/**
34-
* Create an array of MapSqlParameterSource objects populated with data from the
35-
* values passed in. This will define what is included in a batch operation.
36-
* @param valueMaps array of Maps containing the values to be used
37-
* @return an array of SqlParameterSource
34+
* Create an array of {@link MapSqlParameterSource} objects populated with data from
35+
* the values passed in. This will define what is included in a batch operation.
36+
* @param valueMaps array of {@link Map} instances containing the values to be used
37+
* @return an array of {@link SqlParameterSource}
38+
* @see MapSqlParameterSource
39+
* @see NamedParameterJdbcTemplate#batchUpdate(String, Map[])
3840
*/
3941
public static SqlParameterSource[] createBatch(Map<String, ?>[] valueMaps) {
4042
MapSqlParameterSource[] batch = new MapSqlParameterSource[valueMaps.length];
4143
for (int i = 0; i < valueMaps.length; i++) {
42-
Map<String, ?> valueMap = valueMaps[i];
43-
batch[i] = new MapSqlParameterSource(valueMap);
44+
batch[i] = new MapSqlParameterSource(valueMaps[i]);
4445
}
4546
return batch;
4647
}
4748

4849
/**
49-
* Create an array of BeanPropertySqlParameterSource objects populated with data
50+
* Create an array of {@link BeanPropertySqlParameterSource} objects populated with data
5051
* from the values passed in. This will define what is included in a batch operation.
5152
* @param beans object array of beans containing the values to be used
52-
* @return an array of SqlParameterSource
53+
* @return an array of {@link SqlParameterSource}
54+
* @see BeanPropertySqlParameterSource
55+
* @see NamedParameterJdbcTemplate#batchUpdate(String, SqlParameterSource[])
5356
*/
5457
public static SqlParameterSource[] createBatch(Object[] beans) {
5558
BeanPropertySqlParameterSource[] batch = new BeanPropertySqlParameterSource[beans.length];
5659
for (int i = 0; i < beans.length; i++) {
57-
Object bean = beans[i];
58-
batch[i] = new BeanPropertySqlParameterSource(bean);
60+
batch[i] = new BeanPropertySqlParameterSource(beans[i]);
5961
}
6062
return batch;
6163
}
6264

6365
/**
6466
* Create a wrapped value if parameter has type information, plain object if not.
65-
* @param source the source of paramer values and type information
67+
* @param source the source of parameter values and type information
6668
* @param parameterName the name of the parameter
6769
* @return the value object
6870
*/
@@ -83,13 +85,13 @@ public static Object getTypedValue(SqlParameterSource source, String parameterNa
8385

8486
/**
8587
* Create a Map of case insensitive parameter names together with the original name.
86-
* @param parameterSource the source of paramer names
88+
* @param parameterSource the source of parameter names
8789
* @return the Map that can be used for case insensitive matching of parameter names
8890
*/
8991
public static Map<String, String> extractCaseInsensitiveParameterNames(SqlParameterSource parameterSource) {
9092
Map<String, String> caseInsensitiveParameterNames = new HashMap<String, String>();
9193
if (parameterSource instanceof BeanPropertySqlParameterSource) {
92-
String[] propertyNames = ((BeanPropertySqlParameterSource)parameterSource).getReadablePropertyNames();
94+
String[] propertyNames = ((BeanPropertySqlParameterSource) parameterSource).getReadablePropertyNames();
9395
for (String name : propertyNames) {
9496
caseInsensitiveParameterNames.put(name.toLowerCase(), name);
9597
}

0 commit comments

Comments
 (0)