Browse Source

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

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4225 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/merge
Thomas Risberg 15 years ago
parent
commit
9c39084b62
  1. 18
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java
  2. 8
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
  3. 2
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataProvider.java
  4. 1
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java
  5. 4
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcDaoSupport.java
  6. 4
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcOperations.java
  7. 4
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcTemplate.java
  8. 15
      org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/package-info.java
  9. 70
      org.springframework.jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateTests.java

18
org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java

@ -987,6 +987,24 @@ public interface JdbcOperations { @@ -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

8
org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java

@ -922,6 +922,14 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { @@ -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

2
org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/metadata/CallMetaDataProvider.java

@ -25,7 +25,7 @@ import org.springframework.jdbc.core.SqlParameter; @@ -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

1
org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java

@ -28,7 +28,6 @@ import org.springframework.jdbc.core.SqlParameterValue; @@ -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 {

4
org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcDaoSupport.java

@ -27,7 +27,11 @@ import org.springframework.jdbc.core.support.JdbcDaoSupport; @@ -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;

4
org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcOperations.java

@ -37,7 +37,11 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource; @@ -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 {
/**

4
org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcTemplate.java

@ -50,7 +50,11 @@ import org.springframework.util.ObjectUtils; @@ -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 */

15
org.springframework.jdbc/src/main/java/org/springframework/jdbc/core/simple/package-info.java

@ -3,15 +3,14 @@ @@ -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;

70
org.springframework.jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateTests.java

@ -42,6 +42,7 @@ import org.springframework.jdbc.BadSqlGrammarException; @@ -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 { @@ -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");

Loading…
Cancel
Save