Skip to content

Commit 3fa6723

Browse files
committed
Added useful query variants without parameters to NamedParameterJdbcTemplate, for convenience in DAOs
Also deprecated NamedParameterJdbcTemplate's queryForInt/Long operations in favor of queryForObject. Issue: SPR-10256 Issue: SPR-10257
1 parent 9881517 commit 3fa6723

File tree

4 files changed

+273
-49
lines changed

4 files changed

+273
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.jdbc.core.namedparam;
18+
19+
/**
20+
* A simple empty implementation of the {@link SqlParameterSource} interface.
21+
*
22+
* @author Juergen Hoeller
23+
* @since 3.2.2
24+
*/
25+
public class EmptySqlParameterSource implements SqlParameterSource {
26+
27+
/**
28+
* A shared instance of {@link EmptySqlParameterSource}.
29+
*/
30+
public static final EmptySqlParameterSource INSTANCE = new EmptySqlParameterSource();
31+
32+
33+
public boolean hasValue(String paramName) {
34+
return false;
35+
}
36+
37+
public Object getValue(String paramName) throws IllegalArgumentException {
38+
throw new IllegalArgumentException("This SqlParameterSource is empty");
39+
}
40+
41+
public int getSqlType(String paramName) {
42+
return TYPE_UNKNOWN;
43+
}
44+
45+
public String getTypeName(String paramName) {
46+
return null;
47+
}
48+
49+
}

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

+69-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -88,6 +88,21 @@ <T> T execute(String sql, SqlParameterSource paramSource, PreparedStatementCallb
8888
<T> T execute(String sql, Map<String, ?> paramMap, PreparedStatementCallback<T> action)
8989
throws DataAccessException;
9090

91+
/**
92+
* Execute a JDBC data access operation, implemented as callback action
93+
* working on a JDBC PreparedStatement. This allows for implementing arbitrary
94+
* data access operations on a single Statement, within Spring's managed
95+
* JDBC environment: that is, participating in Spring-managed transactions
96+
* and converting JDBC SQLExceptions into Spring's DataAccessException hierarchy.
97+
* <p>The callback action can return a result object, for example a
98+
* domain object or a collection of domain objects.
99+
* @param sql SQL to execute
100+
* @param action callback object that specifies the action
101+
* @return a result object returned by the action, or {@code null}
102+
* @throws DataAccessException if there is any problem
103+
*/
104+
<T> T execute(String sql, PreparedStatementCallback<T> action) throws DataAccessException;
105+
91106
/**
92107
* Query given SQL to create a prepared statement from SQL and a list
93108
* of arguments to bind to the query, reading the ResultSet with a
@@ -115,6 +130,19 @@ <T> T query(String sql, SqlParameterSource paramSource, ResultSetExtractor<T> rs
115130
<T> T query(String sql, Map<String, ?> paramMap, ResultSetExtractor<T> rse)
116131
throws DataAccessException;
117132

133+
/**
134+
* Query given SQL to create a prepared statement from SQL,
135+
* reading the ResultSet with a ResultSetExtractor.
136+
* <p>Note: In contrast to the JdbcOperations method with the same signature,
137+
* this query variant always uses a PreparedStatement. It is effectively
138+
* equivalent to a query call with an empty parameter Map.
139+
* @param sql SQL query to execute
140+
* @param rse object that will extract results
141+
* @return an arbitrary result object, as returned by the ResultSetExtractor
142+
* @throws org.springframework.dao.DataAccessException if the query fails
143+
*/
144+
<T> T query(String sql, ResultSetExtractor<T> rse) throws DataAccessException;
145+
118146
/**
119147
* Query given SQL to create a prepared statement from SQL and a list of
120148
* arguments to bind to the query, reading the ResultSet on a per-row basis
@@ -139,6 +167,18 @@ void query(String sql, SqlParameterSource paramSource, RowCallbackHandler rch)
139167
*/
140168
void query(String sql, Map<String, ?> paramMap, RowCallbackHandler rch) throws DataAccessException;
141169

170+
/**
171+
* Query given SQL to create a prepared statement from SQL,
172+
* reading the ResultSet on a per-row basis with a RowCallbackHandler.
173+
* <p>Note: In contrast to the JdbcOperations method with the same signature,
174+
* this query variant always uses a PreparedStatement. It is effectively
175+
* equivalent to a query call with an empty parameter Map.
176+
* @param sql SQL query to execute
177+
* @param rch object that will extract results, one row at a time
178+
* @throws org.springframework.dao.DataAccessException if the query fails
179+
*/
180+
void query(String sql, RowCallbackHandler rch) throws DataAccessException;
181+
142182
/**
143183
* Query given SQL to create a prepared statement from SQL and a list
144184
* of arguments to bind to the query, mapping each row to a Java object
@@ -166,6 +206,19 @@ <T> List<T> query(String sql, SqlParameterSource paramSource, RowMapper<T> rowMa
166206
<T> List<T> query(String sql, Map<String, ?> paramMap, RowMapper<T> rowMapper)
167207
throws DataAccessException;
168208

209+
/**
210+
* Query given SQL to create a prepared statement from SQL,
211+
* mapping each row to a Java object via a RowMapper.
212+
* <p>Note: In contrast to the JdbcOperations method with the same signature,
213+
* this query variant always uses a PreparedStatement. It is effectively
214+
* equivalent to a query call with an empty parameter Map.
215+
* @param sql SQL query to execute
216+
* @param rowMapper object that will map one object per row
217+
* @return the result List, containing mapped objects
218+
* @throws org.springframework.dao.DataAccessException if the query fails
219+
*/
220+
<T> List<T> query(String sql, RowMapper<T> rowMapper) throws DataAccessException;
221+
169222
/**
170223
* Query given SQL to create a prepared statement from SQL and a list
171224
* of arguments to bind to the query, mapping a single result row to a
@@ -245,8 +298,7 @@ <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType)
245298
* @param paramSource container of arguments to bind to the query
246299
* @return the result Map (one entry for each column, using the column name as the key)
247300
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException
248-
* if the query does not return exactly one row, or does not return exactly
249-
* one column in that row
301+
* if the query does not return exactly one row
250302
* @throws org.springframework.dao.DataAccessException if the query fails
251303
* @see org.springframework.jdbc.core.JdbcTemplate#queryForMap(String)
252304
* @see org.springframework.jdbc.core.ColumnMapRowMapper
@@ -266,8 +318,7 @@ <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType)
266318
* (leaving it to the PreparedStatement to guess the corresponding SQL type)
267319
* @return the result Map (one entry for each column, using the column name as the key)
268320
* @throws org.springframework.dao.IncorrectResultSizeDataAccessException
269-
* if the query does not return exactly one row, or does not return exactly
270-
* one column in that row
321+
* if the query does not return exactly one row
271322
* @throws org.springframework.dao.DataAccessException if the query fails
272323
* @see org.springframework.jdbc.core.JdbcTemplate#queryForMap(String)
273324
* @see org.springframework.jdbc.core.ColumnMapRowMapper
@@ -287,7 +338,9 @@ <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType)
287338
* one column in that row
288339
* @throws org.springframework.dao.DataAccessException if the query fails
289340
* @see org.springframework.jdbc.core.JdbcTemplate#queryForLong(String)
341+
* @deprecated in favor of {@link #queryForObject(String, SqlParameterSource, Class)}
290342
*/
343+
@Deprecated
291344
long queryForLong(String sql, SqlParameterSource paramSource) throws DataAccessException;
292345

293346
/**
@@ -304,7 +357,9 @@ <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType)
304357
* one column in that row
305358
* @throws org.springframework.dao.DataAccessException if the query fails
306359
* @see org.springframework.jdbc.core.JdbcTemplate#queryForLong(String)
360+
* @deprecated in favor of {@link #queryForObject(String, Map, Class)}
307361
*/
362+
@Deprecated
308363
long queryForLong(String sql, Map<String, ?> paramMap) throws DataAccessException;
309364

310365
/**
@@ -319,7 +374,9 @@ <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType)
319374
* exactly one row, or does not return exactly one column in that row
320375
* @throws org.springframework.dao.DataAccessException if the query fails
321376
* @see org.springframework.jdbc.core.JdbcTemplate#queryForInt(String)
377+
* @deprecated in favor of {@link #queryForObject(String, SqlParameterSource, Class)}
322378
*/
379+
@Deprecated
323380
int queryForInt(String sql, SqlParameterSource paramSource) throws DataAccessException;
324381

325382
/**
@@ -335,7 +392,9 @@ <T> T queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType)
335392
* exactly one row, or does not return exactly one column in that row
336393
* @throws org.springframework.dao.DataAccessException if the query fails
337394
* @see org.springframework.jdbc.core.JdbcTemplate#queryForInt(String)
395+
* @deprecated in favor of {@link #queryForObject(String, Map, Class)}
338396
*/
397+
@Deprecated
339398
int queryForInt(String sql, Map<String, ?> paramMap) throws DataAccessException;
340399

341400
/**
@@ -378,8 +437,8 @@ <T> List<T> queryForList(String sql, Map<String, ?> paramMap, Class<T> elementTy
378437
* list of arguments to bind to the query, expecting a result list.
379438
* <p>The results will be mapped to a List (one entry for each row) of
380439
* Maps (one entry for each column, using the column name as the key).
381-
* Thus Each element in the list will be of the form returned by this interface's
382-
* queryForMap() methods.
440+
* Each element in the list will be of the form returned by this interface's
441+
* {@code queryForMap} methods.
383442
* @param sql SQL query to execute
384443
* @param paramSource container of arguments to bind to the query
385444
* @return a List that contains a Map per row
@@ -394,7 +453,7 @@ <T> List<T> queryForList(String sql, Map<String, ?> paramMap, Class<T> elementTy
394453
* <p>The results will be mapped to a List (one entry for each row) of
395454
* Maps (one entry for each column, using the column name as the key).
396455
* Each element in the list will be of the form returned by this interface's
397-
* queryForMap() methods.
456+
* {@code queryForMap} methods.
398457
* @param sql SQL query to execute
399458
* @param paramMap map of parameters to bind to the query
400459
* (leaving it to the PreparedStatement to guess the corresponding SQL type)
@@ -499,14 +558,14 @@ int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHol
499558
* @param batchValues the array of Maps containing the batch of arguments for the query
500559
* @return an array containing the numbers of rows affected by each update in the batch
501560
*/
502-
public int[] batchUpdate(String sql, Map<String, ?>[] batchValues);
561+
int[] batchUpdate(String sql, Map<String, ?>[] batchValues);
503562

504563
/**
505564
* Execute a batch using the supplied SQL statement with the batch of supplied arguments.
506565
* @param sql the SQL statement to execute
507566
* @param batchArgs the array of {@link SqlParameterSource} containing the batch of arguments for the query
508567
* @return an array containing the numbers of rows affected by each update in the batch
509568
*/
510-
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs);
569+
int[] batchUpdate(String sql, SqlParameterSource[] batchArgs);
511570

512571
}

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -137,6 +137,10 @@ public <T> T execute(String sql, Map<String, ?> paramMap, PreparedStatementCallb
137137
return execute(sql, new MapSqlParameterSource(paramMap), action);
138138
}
139139

140+
public <T> T execute(String sql, PreparedStatementCallback<T> action) throws DataAccessException {
141+
return execute(sql, EmptySqlParameterSource.INSTANCE, action);
142+
}
143+
140144
public <T> T query(String sql, SqlParameterSource paramSource, ResultSetExtractor<T> rse)
141145
throws DataAccessException {
142146

@@ -149,6 +153,10 @@ public <T> T query(String sql, Map<String, ?> paramMap, ResultSetExtractor<T> rs
149153
return query(sql, new MapSqlParameterSource(paramMap), rse);
150154
}
151155

156+
public <T> T query(String sql, ResultSetExtractor<T> rse) throws DataAccessException {
157+
return query(sql, EmptySqlParameterSource.INSTANCE, rse);
158+
}
159+
152160
public void query(String sql, SqlParameterSource paramSource, RowCallbackHandler rch)
153161
throws DataAccessException {
154162

@@ -161,6 +169,10 @@ public void query(String sql, Map<String, ?> paramMap, RowCallbackHandler rch)
161169
query(sql, new MapSqlParameterSource(paramMap), rch);
162170
}
163171

172+
public void query(String sql, RowCallbackHandler rch) throws DataAccessException {
173+
query(sql, EmptySqlParameterSource.INSTANCE, rch);
174+
}
175+
164176
public <T> List<T> query(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper)
165177
throws DataAccessException {
166178

@@ -173,6 +185,10 @@ public <T> List<T> query(String sql, Map<String, ?> paramMap, RowMapper<T> rowMa
173185
return query(sql, new MapSqlParameterSource(paramMap), rowMapper);
174186
}
175187

188+
public <T> List<T> query(String sql, RowMapper<T> rowMapper) throws DataAccessException {
189+
return query(sql, EmptySqlParameterSource.INSTANCE, rowMapper);
190+
}
191+
176192
public <T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper)
177193
throws DataAccessException {
178194

@@ -206,20 +222,24 @@ public Map<String, Object> queryForMap(String sql, Map<String, ?> paramMap) thro
206222
return queryForObject(sql, paramMap, new ColumnMapRowMapper());
207223
}
208224

225+
@Deprecated
209226
public long queryForLong(String sql, SqlParameterSource paramSource) throws DataAccessException {
210227
Number number = queryForObject(sql, paramSource, Long.class);
211228
return (number != null ? number.longValue() : 0);
212229
}
213230

231+
@Deprecated
214232
public long queryForLong(String sql, Map<String, ?> paramMap) throws DataAccessException {
215233
return queryForLong(sql, new MapSqlParameterSource(paramMap));
216234
}
217235

236+
@Deprecated
218237
public int queryForInt(String sql, SqlParameterSource paramSource) throws DataAccessException {
219238
Number number = queryForObject(sql, paramSource, Integer.class);
220239
return (number != null ? number.intValue() : 0);
221240
}
222241

242+
@Deprecated
223243
public int queryForInt(String sql, Map<String, ?> paramMap) throws DataAccessException {
224244
return queryForInt(sql, new MapSqlParameterSource(paramMap));
225245
}

0 commit comments

Comments
 (0)