SPR-6922 deprecated SimpleJdbcTemplate/SimpleJdbcOperations/SimpleJdbcDaoSupport in favor of JdbcTemplate/NamedParameterJdbcTemplate and related interfaces support classes

This commit is contained in:
Thomas Risberg 2011-04-14 18:44:40 +00:00
parent aa065e8310
commit f5f738f2b4
9 changed files with 116 additions and 10 deletions

View File

@ -987,6 +987,24 @@ public interface JdbcOperations {
*/
int[] batchUpdate(String sql, BatchPreparedStatementSetter pss) throws DataAccessException;
/**
* Execute a batch using the supplied SQL statement with the batch of supplied arguments.
* @param sql the SQL statement to execute
* @param batchArgs the List of Object arrays containing the batch of arguments for the query
* @return an array containing the numbers of rows affected by each update in the batch
*/
public int[] batchUpdate(String sql, List<Object[]> batchArgs);
/**
* Execute a batch using the supplied SQL statement with the batch of supplied arguments.
* @param sql the SQL statement to execute.
* @param batchArgs the List of Object arrays containing the batch of arguments for the query
* @param argTypes SQL types of the arguments
* (constants from <code>java.sql.Types</code>)
* @return an array containing the numbers of rows affected by each update in the batch
*/
public int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes);
//-------------------------------------------------------------------------
// Methods dealing with callable statements

View File

@ -922,6 +922,14 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
});
}
public int[] batchUpdate(String sql, List<Object[]> batchArgs) {
return batchUpdate(sql, batchArgs, new int[0]);
}
public int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes) {
return BatchUpdateUtils.executeBatchUpdate(sql, batchArgs, argTypes, this);
}
//-------------------------------------------------------------------------
// Methods dealing with callable statements

View File

@ -25,7 +25,7 @@ import org.springframework.jdbc.core.SqlParameter;
/**
* Interface specifying the API to be implemented by a class providing call metadata.
* This is intended for internal use by Spring's
* {@link org.springframework.jdbc.core.simple.SimpleJdbcTemplate}.
* {@link org.springframework.jdbc.core.simple.SimpleJdbcCall}.
*
* @author Thomas Risberg
* @since 2.5

View File

@ -28,7 +28,6 @@ import org.springframework.jdbc.core.SqlParameterValue;
*
* @author Thomas Risberg
* @since 2.5
* @see org.springframework.jdbc.core.simple.SimpleJdbcTemplate
*/
public class SqlParameterSourceUtils {

View File

@ -27,7 +27,11 @@ import org.springframework.jdbc.core.support.JdbcDaoSupport;
* @author Juergen Hoeller
* @since 2.0
* @see SimpleJdbcTemplate
* @deprecated since Spring 3.1 in favor of {@link org.springframework.jdbc.core.support.JdbcDaoSupport} and
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport}. The JdbcTemplate and
* NamedParameterJdbcTemplate now provide all the functionality of the SimpleJdbcTemplate.
*/
@Deprecated
public class SimpleJdbcDaoSupport extends JdbcDaoSupport {
private SimpleJdbcTemplate simpleJdbcTemplate;

View File

@ -37,7 +37,11 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource;
* @see org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
* @see SimpleJdbcTemplate
* @see org.springframework.jdbc.core.JdbcOperations
* @deprecated since Spring 3.1 in favor of {@link org.springframework.jdbc.core.JdbcOperations} and
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations}. The JdbcTemplate and
* NamedParameterJdbcTemplate now provide all the functionality of the SimpleJdbcTemplate.
*/
@Deprecated
public interface SimpleJdbcOperations {
/**

View File

@ -50,7 +50,11 @@ import org.springframework.util.ObjectUtils;
* @see ParameterizedRowMapper
* @see SimpleJdbcDaoSupport
* @see org.springframework.jdbc.core.JdbcTemplate
* @deprecated since Spring 3.1 in favor of {@link org.springframework.jdbc.core.JdbcTemplate} and
* {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}. The JdbcTemplate and
* NamedParameterJdbcTemplate now provide all the functionality of the SimpleJdbcTemplate.
*/
@Deprecated
public class SimpleJdbcTemplate implements SimpleJdbcOperations {
/** The NamedParameterJdbcTemplate that we are wrapping */

View File

@ -3,15 +3,14 @@
*
* Simplification layer over JdbcTemplate for Java 5 and above.
*
* <p>SimpleJdbcTemplate is a wrapper around JdbcTemplate that takes advantage
* of varargs and autoboxing. It also offers only a subset of the methods
* available on JdbcTemplate: Hence, it does not implement the JdbcOperations
* interface or extend JdbcTemplate, but implements the dedicated
* SimpleJdbcOperations interface.
* <p><code>SimpleJdbcInsert</code> and <code>SimpleJdbcCall</code> are classes that takes advantage
* of database metadata provided by the JDBC driver to simplify the application code. Much of the
* parameter specification becomes unnecessary since it can be looked up in the metadata.
*
* <P>If you need the full power of Spring JDBC for less common operations,
* use the <code>getJdbcOperations()</code> method of SimpleJdbcTemplate and work
* with the returned classic template, or use a JdbcTemplate instance directly.
* Note: The <code>SimpleJdbcOperations</code> and <code>SimpleJdbcTemplate</code>, which provides a wrapper
* around JdbcTemplate to take advantage of Java 5 features like generics, varargs and autoboxing, is now deprecated
* since Spring 3.1. All functionality is now available in the <code>JdbcOperations</code> and
* <code>NamedParametersOperations</code> respectively.
*
*/
package org.springframework.jdbc.core.simple;

View File

@ -42,6 +42,7 @@ import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.SQLWarningException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;
@ -1120,6 +1121,75 @@ public class JdbcTemplateTests extends AbstractJdbcTests {
ctrlDatabaseMetaData.verify();
}
public void testBatchUpdateWithListOfObjectArrays() throws Exception {
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
final List<Object[]> ids = new ArrayList<Object[]>();
ids.add(new Object[] {100});
ids.add(new Object[] {200});
final int[] rowsAffected = new int[] { 1, 2 };
MockControl ctrlDataSource = MockControl.createControl(DataSource.class);
DataSource mockDataSource = (DataSource) ctrlDataSource.getMock();
MockControl ctrlConnection = MockControl.createControl(Connection.class);
Connection mockConnection = (Connection) ctrlConnection.getMock();
MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class);
DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock();
BatchUpdateTestHelper.prepareBatchUpdateMocks(sql, ids, null, rowsAffected, ctrlDataSource, mockDataSource, ctrlConnection,
mockConnection, ctrlPreparedStatement, mockPreparedStatement, ctrlDatabaseMetaData,
mockDatabaseMetaData);
BatchUpdateTestHelper.replayBatchUpdateMocks(ctrlDataSource, ctrlConnection, ctrlPreparedStatement, ctrlDatabaseMetaData);
JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
int[] actualRowsAffected = template.batchUpdate(sql, ids);
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
assertEquals(rowsAffected[0], actualRowsAffected[0]);
assertEquals(rowsAffected[1], actualRowsAffected[1]);
BatchUpdateTestHelper.verifyBatchUpdateMocks(ctrlPreparedStatement, ctrlDatabaseMetaData);
}
public void testBatchUpdateWithListOfObjectArraysPlusTypeInfo() throws Exception {
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
final List<Object[]> ids = new ArrayList<Object[]>();
ids.add(new Object[] {100});
ids.add(new Object[] {200});
final int[] sqlTypes = new int[] {Types.NUMERIC};
final int[] rowsAffected = new int[] { 1, 2 };
MockControl ctrlDataSource = MockControl.createControl(DataSource.class);
DataSource mockDataSource = (DataSource) ctrlDataSource.getMock();
MockControl ctrlConnection = MockControl.createControl(Connection.class);
Connection mockConnection = (Connection) ctrlConnection.getMock();
MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class);
DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock();
BatchUpdateTestHelper.prepareBatchUpdateMocks(sql, ids, sqlTypes, rowsAffected, ctrlDataSource, mockDataSource, ctrlConnection,
mockConnection, ctrlPreparedStatement, mockPreparedStatement, ctrlDatabaseMetaData,
mockDatabaseMetaData);
BatchUpdateTestHelper.replayBatchUpdateMocks(ctrlDataSource, ctrlConnection, ctrlPreparedStatement, ctrlDatabaseMetaData);
JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
int[] actualRowsAffected = template.batchUpdate(sql, ids, sqlTypes);
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
assertEquals(rowsAffected[0], actualRowsAffected[0]);
assertEquals(rowsAffected[1], actualRowsAffected[1]);
BatchUpdateTestHelper.verifyBatchUpdateMocks(ctrlPreparedStatement, ctrlDatabaseMetaData);
}
public void testCouldntGetConnectionOrExceptionTranslator() throws SQLException {
SQLException sex = new SQLException("foo", "07xxx");