Consistent parameter resolution for batch updates (IN clauses etc)

Includes deprecation of (NamedParameter)BatchUpdateUtils in favor of inlined code in (NamedParameter)JdbcTemplate itself.

Issue: SPR-17402
This commit is contained in:
Juergen Hoeller
2018-11-20 22:05:53 +01:00
parent 2a5d7690b6
commit a3d763d137
8 changed files with 138 additions and 15 deletions

View File

@@ -35,6 +35,7 @@ import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.InOrder;
import org.springframework.jdbc.Customer;
import org.springframework.jdbc.core.JdbcOperations;
@@ -50,6 +51,7 @@ import static org.mockito.BDDMockito.*;
* @author Juergen Hoeller
* @author Chris Beams
* @author Nikita Khateev
* @author Fedor Bobin
*/
public class NamedParameterJdbcTemplateTests {
@@ -468,6 +470,41 @@ public class NamedParameterJdbcTemplateTests {
verify(connection, atLeastOnce()).close();
}
@Test
public void testBatchUpdateWithInClause() throws Exception {
@SuppressWarnings("unchecked")
Map<String, Object>[] parameters = new Map[2];
parameters[0] = Collections.singletonMap("ids", Arrays.asList(1, 2));
parameters[1] = Collections.singletonMap("ids", Arrays.asList(3, 4));
final int[] rowsAffected = new int[] {1, 2};
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
given(connection.getMetaData()).willReturn(databaseMetaData);
JdbcTemplate template = new JdbcTemplate(dataSource, false);
namedParameterTemplate = new NamedParameterJdbcTemplate(template);
int[] actualRowsAffected = namedParameterTemplate.batchUpdate(
"delete sometable where id in (:ids)",
parameters
);
assertEquals("executed 2 updates", 2, actualRowsAffected.length);
InOrder inOrder = inOrder(preparedStatement);
inOrder.verify(preparedStatement).setObject(1, 1);
inOrder.verify(preparedStatement).setObject(2, 2);
inOrder.verify(preparedStatement).addBatch();
inOrder.verify(preparedStatement).setObject(1, 3);
inOrder.verify(preparedStatement).setObject(2, 4);
inOrder.verify(preparedStatement).addBatch();
inOrder.verify(preparedStatement, atLeastOnce()).close();
verify(connection, atLeastOnce()).close();
}
@Test
public void testBatchUpdateWithSqlParameterSourcePlusTypeInfo() throws Exception {
SqlParameterSource[] ids = new SqlParameterSource[2];