diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcPagingItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcPagingItemReaderBuilder.java index 80ce964661..80817f3f19 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcPagingItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcPagingItemReaderBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 the original author or authors. + * Copyright 2017-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +33,7 @@ import org.springframework.batch.item.database.support.SqlitePagingQueryProvider; import org.springframework.batch.item.database.support.SybasePagingQueryProvider; import org.springframework.batch.support.DatabaseType; +import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.support.MetaDataAccessException; import org.springframework.util.Assert; @@ -46,6 +47,7 @@ * * @author Michael Minella * @author Glenn Renfro + * @author Drummond Dawson * @since 4.0 * @see JdbcPagingItemReader */ @@ -175,6 +177,20 @@ public JdbcPagingItemReaderBuilder rowMapper(RowMapper rowMapper) { return this; } + /** + * Creates a {@link BeanPropertyRowMapper} to be used as your + * {@link RowMapper}. + * + * @param mappedClass the class for the row mapper + * @return this instance for method chaining + * @see BeanPropertyRowMapper + */ + public JdbcPagingItemReaderBuilder beanRowMapper(Class mappedClass) { + this.rowMapper = new BeanPropertyRowMapper<>(mappedClass); + + return this; + } + /** * A {@link Map} of values to set on the SQL's prepared statement. * diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JdbcPagingItemReaderBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JdbcPagingItemReaderBuilderTests.java index 944dbb1a03..30af5db5dc 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JdbcPagingItemReaderBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JdbcPagingItemReaderBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 the original author or authors. + * Copyright 2017-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,6 +46,7 @@ /** * @author Michael Minella + * @author Drummond Dawson */ public class JdbcPagingItemReaderBuilderTests { @@ -254,6 +255,34 @@ public void testParameters() throws Exception { assertEquals("9", item1.getThird()); } + @Test + public void testBeanRowMapper() throws Exception { + Map sortKeys = new HashMap<>(1); + sortKeys.put("ID", Order.DESCENDING); + + JdbcPagingItemReader reader = new JdbcPagingItemReaderBuilder() + .name("fooReader") + .currentItemCount(1) + .dataSource(this.dataSource) + .maxItemCount(2) + .selectClause("SELECT ID, FIRST, SECOND, THIRD") + .fromClause("FOO") + .sortKeys(sortKeys) + .beanRowMapper(Foo.class) + .build(); + + reader.afterPropertiesSet(); + + reader.open(new ExecutionContext()); + Foo item1 = reader.read(); + assertNull(reader.read()); + + assertEquals(3, item1.getId()); + assertEquals(10, item1.getFirst()); + assertEquals("11", item1.getSecond()); + assertEquals("12", item1.getThird()); + } + @Test public void testValidation() { @@ -342,6 +371,8 @@ public static class Foo { private String second; private String third; + public Foo() {} + public Foo(int id, int first, String second, String third) { this.id = id; this.first = first;