1616
1717package org .springframework .jdbc .core .namedparam ;
1818
19+ import java .util .Arrays ;
20+ import java .util .Collection ;
1921import java .util .HashMap ;
2022import java .util .Map ;
2123
2224import org .springframework .jdbc .core .SqlParameterValue ;
2325import 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