Skip to content

Commit a8b4884

Browse files
committed
SqlParameterSourceUtils.createBatch with Collection support
Issue: SPR-16215
1 parent 20fcefc commit a8b4884

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import org.springframework.lang.Nullable;
2424

2525
/**
26-
* Generic utility methods for working with JDBC batch statements. Mainly for internal use
27-
* within the framework.
26+
* Generic utility methods for working with JDBC batch statements.
27+
* Mainly for internal use within the framework.
2828
*
2929
* @author Thomas Risberg
3030
* @since 3.0

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,7 @@ public int update(
337337

338338
@Override
339339
public int[] batchUpdate(String sql, Map<String, ?>[] batchValues) {
340-
SqlParameterSource[] batchArgs = new SqlParameterSource[batchValues.length];
341-
int i = 0;
342-
for (Map<String, ?> values : batchValues) {
343-
batchArgs[i] = new MapSqlParameterSource(values);
344-
i++;
345-
}
346-
return batchUpdate(sql, batchArgs);
340+
return batchUpdate(sql, SqlParameterSourceUtils.createBatch(batchValues));
347341
}
348342

349343
@Override

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

+51-24
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,81 @@
1616

1717
package org.springframework.jdbc.core.namedparam;
1818

19+
import java.util.Arrays;
20+
import java.util.Collection;
1921
import java.util.HashMap;
2022
import java.util.Map;
2123

2224
import org.springframework.jdbc.core.SqlParameterValue;
2325
import org.springframework.lang.Nullable;
2426

2527
/**
26-
* Class that provides helper methods for the use of {@link SqlParameterSource}
27-
* with {@code SimpleJdbc} classes.
28+
* Class that provides helper methods for the use of {@link SqlParameterSource},
29+
* in particular with {@link NamedParameterJdbcTemplate}.
2830
*
2931
* @author Thomas Risberg
32+
* @author Juergen Hoeller
3033
* @since 2.5
3134
*/
32-
public class SqlParameterSourceUtils {
35+
public abstract class SqlParameterSourceUtils {
3336

3437
/**
35-
* Create an array of MapSqlParameterSource objects populated with data from the
36-
* values passed in. This will define what is included in a batch operation.
37-
* @param valueMaps array of Maps containing the values to be used
38-
* @return an array of SqlParameterSource
38+
* Create an array of {@link SqlParameterSource} objects populated with data
39+
* from the values passed in (either a {@link Map} or a bean object).
40+
* This will define what is included in a batch operation.
41+
* @param candidates object array of objects containing the values to be used
42+
* @return an array of {@link SqlParameterSource}
43+
* @see MapSqlParameterSource
44+
* @see BeanPropertySqlParameterSource
45+
* @see NamedParameterJdbcTemplate#batchUpdate(String, SqlParameterSource[]))
3946
*/
40-
public static SqlParameterSource[] createBatch(Map<String, ?>[] valueMaps) {
41-
MapSqlParameterSource[] batch = new MapSqlParameterSource[valueMaps.length];
42-
for (int i = 0; i < valueMaps.length; i++) {
43-
Map<String, ?> valueMap = valueMaps[i];
44-
batch[i] = new MapSqlParameterSource(valueMap);
47+
@SuppressWarnings("unchecked")
48+
public static SqlParameterSource[] createBatch(Object... candidates) {
49+
return createBatch(Arrays.asList(candidates));
50+
}
51+
52+
/**
53+
* Create an array of {@link SqlParameterSource} objects populated with data
54+
* from the values passed in (either a {@link Map} or a bean object).
55+
* This will define what is included in a batch operation.
56+
* @param candidates collection of objects containing the values to be used
57+
* @return an array of {@link SqlParameterSource}
58+
* @since 5.0.2
59+
* @see MapSqlParameterSource
60+
* @see BeanPropertySqlParameterSource
61+
* @see NamedParameterJdbcTemplate#batchUpdate(String, SqlParameterSource[]))
62+
*/
63+
@SuppressWarnings("unchecked")
64+
public static SqlParameterSource[] createBatch(Collection<?> candidates) {
65+
SqlParameterSource[] batch = new SqlParameterSource[candidates.size()];
66+
int i = 0;
67+
for (Object candidate : candidates) {
68+
batch[i] = (candidate instanceof Map ? new MapSqlParameterSource((Map<String, ?>) candidate) :
69+
new BeanPropertySqlParameterSource(candidate));
70+
i++;
4571
}
4672
return batch;
4773
}
4874

4975
/**
50-
* Create an array of BeanPropertySqlParameterSource objects populated with data
51-
* from the values passed in. This will define what is included in a batch operation.
52-
* @param beans object array of beans containing the values to be used
53-
* @return an array of SqlParameterSource
76+
* Create an array of {@link MapSqlParameterSource} objects populated with data from
77+
* the values passed in. This will define what is included in a batch operation.
78+
* @param valueMaps array of {@link Map} instances containing the values to be used
79+
* @return an array of {@link SqlParameterSource}
80+
* @see MapSqlParameterSource
81+
* @see NamedParameterJdbcTemplate#batchUpdate(String, Map[])
5482
*/
55-
public static SqlParameterSource[] createBatch(Object[] beans) {
56-
BeanPropertySqlParameterSource[] batch = new BeanPropertySqlParameterSource[beans.length];
57-
for (int i = 0; i < beans.length; i++) {
58-
Object bean = beans[i];
59-
batch[i] = new BeanPropertySqlParameterSource(bean);
83+
public static SqlParameterSource[] createBatch(Map<String, ?>[] valueMaps) {
84+
SqlParameterSource[] batch = new SqlParameterSource[valueMaps.length];
85+
for (int i = 0; i < valueMaps.length; i++) {
86+
batch[i] = new MapSqlParameterSource(valueMaps[i]);
6087
}
6188
return batch;
6289
}
6390

6491
/**
6592
* Create a wrapped value if parameter has type information, plain object if not.
66-
* @param source the source of paramer values and type information
93+
* @param source the source of parameter values and type information
6794
* @param parameterName the name of the parameter
6895
* @return the value object
6996
*/
@@ -85,13 +112,13 @@ public static Object getTypedValue(SqlParameterSource source, String parameterNa
85112

86113
/**
87114
* Create a Map of case insensitive parameter names together with the original name.
88-
* @param parameterSource the source of paramer names
115+
* @param parameterSource the source of parameter names
89116
* @return the Map that can be used for case insensitive matching of parameter names
90117
*/
91118
public static Map<String, String> extractCaseInsensitiveParameterNames(SqlParameterSource parameterSource) {
92119
Map<String, String> caseInsensitiveParameterNames = new HashMap<>();
93120
if (parameterSource instanceof BeanPropertySqlParameterSource) {
94-
String[] propertyNames = ((BeanPropertySqlParameterSource)parameterSource).getReadablePropertyNames();
121+
String[] propertyNames = ((BeanPropertySqlParameterSource) parameterSource).getReadablePropertyNames();
95122
for (String name : propertyNames) {
96123
caseInsensitiveParameterNames.put(name.toLowerCase(), name);
97124
}

0 commit comments

Comments
 (0)