diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcBatchItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcBatchItemWriter.java index 79e5a4f5f..de37aa901 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcBatchItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcBatchItemWriter.java @@ -171,7 +171,7 @@ public class JdbcBatchItemWriter implements ItemWriter, 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()]; diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcBatchItemWriterNamedParameterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcBatchItemWriterNamedParameterTests.java index cbebebf37..b7b4d4cae 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcBatchItemWriterNamedParameterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcBatchItemWriterNamedParameterTests.java @@ -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> mapWriter = new JdbcBatchItemWriter<>(); + + mapWriter.setSql(sql); + mapWriter.setJdbcTemplate(namedParameterJdbcOperations); + mapWriter.setItemSqlParameterSourceProvider(new ItemSqlParameterSourceProvider>() { + @Override + public SqlParameterSource createSqlParameterSource(Map item) { + return new MapSqlParameterSource(item); + } + }); + mapWriter.afterPropertiesSet(); + + ArgumentCaptor captor = ArgumentCaptor.forClass(SqlParameterSource[].class); + + when(namedParameterJdbcOperations.batchUpdate(any(String.class), + captor.capture())) + .thenReturn(new int[] {1}); + mapWriter.write(Collections.singletonList(new HashMap() {{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),