added vararg variants of query methods to JdbcTemplate (as known from SimpleJdbcTemplate; SPR-6858)

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2998 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Juergen Hoeller 2010-02-17 22:19:49 +00:00
parent 5f7fc7e1dd
commit c02c111826
2 changed files with 127 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -380,6 +380,21 @@ public interface JdbcOperations {
*/
<T> T query(String sql, Object[] args, ResultSetExtractor<T> rse) throws DataAccessException;
/**
* Query given SQL to create a prepared statement from SQL and a list
* of arguments to bind to the query, reading the ResultSet with a
* ResultSetExtractor.
* @param sql SQL query to execute
* @param rse object that will extract results
* @param args arguments to bind to the query
* (leaving it to the PreparedStatement to guess the corresponding SQL type);
* may also contain {@link SqlParameterValue} objects which indicate not
* only the argument value but also the SQL type and optionally the scale
* @return an arbitrary result object, as returned by the ResultSetExtractor
* @throws DataAccessException if the query fails
*/
<T> T query(String sql, ResultSetExtractor<T> rse, Object... args) throws DataAccessException;
/**
* Query using a prepared statement, reading the ResultSet on a per-row
* basis with a RowCallbackHandler.
@ -437,6 +452,20 @@ public interface JdbcOperations {
*/
void query(String sql, Object[] args, RowCallbackHandler rch) throws DataAccessException;
/**
* Query given SQL to create a prepared statement from SQL and a list of
* arguments to bind to the query, reading the ResultSet on a per-row basis
* with a RowCallbackHandler.
* @param sql SQL query to execute
* @param rch object that will extract results, one row at a time
* @param args arguments to bind to the query
* (leaving it to the PreparedStatement to guess the corresponding SQL type);
* may also contain {@link SqlParameterValue} objects which indicate not
* only the argument value but also the SQL type and optionally the scale
* @throws DataAccessException if the query fails
*/
void query(String sql, RowCallbackHandler rch, Object... args) throws DataAccessException;
/**
* Query using a prepared statement, mapping each row to a Java object
* via a RowMapper.
@ -497,6 +526,21 @@ public interface JdbcOperations {
*/
<T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper) throws DataAccessException;
/**
* Query given SQL to create a prepared statement from SQL and a list
* of arguments to bind to the query, mapping each row to a Java object
* via a RowMapper.
* @param sql SQL query to execute
* @param rowMapper object that will map one object per row
* @param args arguments to bind to the query
* (leaving it to the PreparedStatement to guess the corresponding SQL type);
* may also contain {@link SqlParameterValue} objects which indicate not
* only the argument value but also the SQL type and optionally the scale
* @return the result List, containing mapped objects
* @throws DataAccessException if the query fails
*/
<T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) throws DataAccessException;
/**
* Query given SQL to create a prepared statement from SQL and a list
* of arguments to bind to the query, mapping a single result row to a
@ -533,6 +577,24 @@ public interface JdbcOperations {
<T> T queryForObject(String sql, Object[] args, RowMapper<T> rowMapper)
throws DataAccessException;
/**
* Query given SQL to create a prepared statement from SQL and a list
* of arguments to bind to the query, mapping a single result row to a
* Java object via a RowMapper.
* @param sql SQL query to execute
* @param rowMapper object that will map one object per row
* @param args arguments to bind to the query
* (leaving it to the PreparedStatement to guess the corresponding SQL type);
* may also contain {@link SqlParameterValue} objects which indicate not
* only the argument value but also the SQL type and optionally the scale
* @return the single mapped object
* @throws IncorrectResultSizeDataAccessException if the query does not
* return exactly one row
* @throws DataAccessException if the query fails
*/
<T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args)
throws DataAccessException;
/**
* Query given SQL to create a prepared statement from SQL and a
* list of arguments to bind to the query, expecting a result object.
@ -572,6 +634,25 @@ public interface JdbcOperations {
*/
<T> T queryForObject(String sql, Object[] args, Class<T> requiredType) throws DataAccessException;
/**
* Query given SQL to create a prepared statement from SQL and a
* list of arguments to bind to the query, expecting a result object.
* <p>The query is expected to be a single row/single column query; the returned
* result will be directly mapped to the corresponding object type.
* @param sql SQL query to execute
* @param requiredType the type that the result object is expected to match
* @param args arguments to bind to the query
* (leaving it to the PreparedStatement to guess the corresponding SQL type);
* may also contain {@link SqlParameterValue} objects which indicate not
* only the argument value but also the SQL type and optionally the scale
* @return the result object of the required type, or <code>null</code> in case of SQL NULL
* @throws IncorrectResultSizeDataAccessException if the query does not return
* exactly one row, or does not return exactly one column in that row
* @throws DataAccessException if the query fails
* @see #queryForObject(String, Class)
*/
<T> T queryForObject(String sql, Class<T> requiredType, Object... args) throws DataAccessException;
/**
* Query given SQL to create a prepared statement from SQL and a
* list of arguments to bind to the query, expecting a result Map.
@ -725,6 +806,25 @@ public interface JdbcOperations {
*/
<T> List<T> queryForList(String sql, Object[] args, Class<T> elementType) throws DataAccessException;
/**
* Query given SQL to create a prepared statement from SQL and a
* list of arguments to bind to the query, expecting a result list.
* <p>The results will be mapped to a List (one entry for each row) of
* result objects, each of them matching the specified element type.
* @param sql SQL query to execute
* @param elementType the required type of element in the result list
* (for example, <code>Integer.class</code>)
* @param args arguments to bind to the query
* (leaving it to the PreparedStatement to guess the corresponding SQL type);
* may also contain {@link SqlParameterValue} objects which indicate not
* only the argument value but also the SQL type and optionally the scale
* @return a List of objects that match the specified element type
* @throws DataAccessException if the query fails
* @see #queryForList(String, Class)
* @see SingleColumnRowMapper
*/
<T> List<T> queryForList(String sql, Class<T> elementType, Object... args) throws DataAccessException;
/**
* Query given SQL to create a prepared statement from SQL and a
* list of arguments to bind to the query, expecting a result list.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -673,6 +673,10 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
return query(sql, newArgPreparedStatementSetter(args), rse);
}
public <T> T query(String sql, ResultSetExtractor<T> rse, Object... args) throws DataAccessException {
return query(sql, newArgPreparedStatementSetter(args), rse);
}
public void query(PreparedStatementCreator psc, RowCallbackHandler rch) throws DataAccessException {
query(psc, new RowCallbackHandlerResultSetExtractor(rch));
}
@ -689,6 +693,10 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
query(sql, newArgPreparedStatementSetter(args), rch);
}
public void query(String sql, RowCallbackHandler rch, Object... args) throws DataAccessException {
query(sql, newArgPreparedStatementSetter(args), rch);
}
public <T> List<T> query(PreparedStatementCreator psc, RowMapper<T> rowMapper) throws DataAccessException {
return query(psc, new RowMapperResultSetExtractor<T>(rowMapper));
}
@ -705,6 +713,10 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
return query(sql, args, new RowMapperResultSetExtractor<T>(rowMapper));
}
public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args) throws DataAccessException {
return query(sql, args, new RowMapperResultSetExtractor<T>(rowMapper));
}
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper)
throws DataAccessException {
@ -717,6 +729,11 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
return DataAccessUtils.requiredSingleResult(results);
}
public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) throws DataAccessException {
List<T> results = query(sql, args, new RowMapperResultSetExtractor<T>(rowMapper, 1));
return DataAccessUtils.requiredSingleResult(results);
}
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, Class<T> requiredType)
throws DataAccessException {
@ -727,6 +744,10 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
return queryForObject(sql, args, getSingleColumnRowMapper(requiredType));
}
public <T> T queryForObject(String sql, Class<T> requiredType, Object... args) throws DataAccessException {
return queryForObject(sql, args, getSingleColumnRowMapper(requiredType));
}
public Map<String, Object> queryForMap(String sql, Object[] args, int[] argTypes) throws DataAccessException {
return queryForObject(sql, args, argTypes, getColumnMapRowMapper());
}
@ -763,6 +784,10 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
return query(sql, args, getSingleColumnRowMapper(elementType));
}
public <T> List<T> queryForList(String sql, Class<T> elementType, Object... args) throws DataAccessException {
return query(sql, args, getSingleColumnRowMapper(elementType));
}
public List<Map<String, Object>> queryForList(String sql, Object[] args, int[] argTypes) throws DataAccessException {
return query(sql, args, argTypes, getColumnMapRowMapper());
}