Jdbc writer fixed to allow Map, named params, and ItemSqlParameterSourceProvider
In merging BATCH-1955, we accidentally prevented the ability to use a Map as an item, named parameters, and an ItemSqlParameterSourceProvider. This commit re-enables that capability by checking if the ItemSqlParameterSourceProvider is null. If it's not, it's used.
This commit is contained in:
@@ -171,7 +171,7 @@ public class JdbcBatchItemWriter<T> implements ItemWriter<T>, InitializingBean {
|
||||
int[] updateCounts;
|
||||
|
||||
if (usingNamedParameters) {
|
||||
if(items.get(0) instanceof Map) {
|
||||
if(items.get(0) instanceof Map && this.itemSqlParameterSourceProvider == null) {
|
||||
updateCounts = namedParameterJdbcTemplate.batchUpdate(sql, items.toArray(new Map[items.size()]));
|
||||
} else {
|
||||
SqlParameterSource[] batchArgs = new SqlParameterSource[items.size()];
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.mockito.ArgumentCaptor;
|
||||
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
||||
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
||||
|
||||
@@ -34,6 +35,7 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
|
||||
@@ -150,6 +152,33 @@ public class JdbcBatchItemWriterNamedParameterTests {
|
||||
assertEquals("bar", results.get("foo"));
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "serial", "unchecked" })
|
||||
@Test
|
||||
public void testWriteAndFlushMapWithItemSqlParameterSourceProvider() throws Exception {
|
||||
JdbcBatchItemWriter<Map<String, Object>> mapWriter = new JdbcBatchItemWriter<>();
|
||||
|
||||
mapWriter.setSql(sql);
|
||||
mapWriter.setJdbcTemplate(namedParameterJdbcOperations);
|
||||
mapWriter.setItemSqlParameterSourceProvider(new ItemSqlParameterSourceProvider<Map<String, Object>>() {
|
||||
@Override
|
||||
public SqlParameterSource createSqlParameterSource(Map<String, Object> item) {
|
||||
return new MapSqlParameterSource(item);
|
||||
}
|
||||
});
|
||||
mapWriter.afterPropertiesSet();
|
||||
|
||||
ArgumentCaptor<SqlParameterSource []> captor = ArgumentCaptor.forClass(SqlParameterSource[].class);
|
||||
|
||||
when(namedParameterJdbcOperations.batchUpdate(any(String.class),
|
||||
captor.capture()))
|
||||
.thenReturn(new int[] {1});
|
||||
mapWriter.write(Collections.singletonList(new HashMap<String, Object>() {{put("foo", "bar");}}));
|
||||
|
||||
assertEquals(1, captor.getValue().length);
|
||||
SqlParameterSource results = captor.getValue()[0];
|
||||
assertEquals("bar", results.getValue("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteAndFlushWithEmptyUpdate() throws Exception {
|
||||
when(namedParameterJdbcOperations.batchUpdate(eq(sql),
|
||||
|
||||
Reference in New Issue
Block a user