Skip to content

Commit 8a6a2bd

Browse files
drumoniifmbenhassine
authored andcommitted
Add beanRowMapper to JdbcPagingItemReaderBuilder
Sets the rowMapper of the JdbcPagingItemReader as a BeanPropertyRowMapper. Issue #819
1 parent e3bc7de commit 8a6a2bd

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcPagingItemReaderBuilder.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
import org.springframework.batch.item.database.support.SqlitePagingQueryProvider;
3434
import org.springframework.batch.item.database.support.SybasePagingQueryProvider;
3535
import org.springframework.batch.support.DatabaseType;
36+
import org.springframework.jdbc.core.BeanPropertyRowMapper;
3637
import org.springframework.jdbc.core.RowMapper;
3738
import org.springframework.jdbc.support.MetaDataAccessException;
3839
import org.springframework.util.Assert;
@@ -46,6 +47,7 @@
4647
*
4748
* @author Michael Minella
4849
* @author Glenn Renfro
50+
* @author Drummond Dawson
4951
* @since 4.0
5052
* @see JdbcPagingItemReader
5153
*/
@@ -175,6 +177,20 @@ public JdbcPagingItemReaderBuilder<T> rowMapper(RowMapper<T> rowMapper) {
175177
return this;
176178
}
177179

180+
/**
181+
* Creates a {@link BeanPropertyRowMapper} to be used as your
182+
* {@link RowMapper}.
183+
*
184+
* @param mappedClass the class for the row mapper
185+
* @return this instance for method chaining
186+
* @see BeanPropertyRowMapper
187+
*/
188+
public JdbcPagingItemReaderBuilder<T> beanRowMapper(Class<T> mappedClass) {
189+
this.rowMapper = new BeanPropertyRowMapper<>(mappedClass);
190+
191+
return this;
192+
}
193+
178194
/**
179195
* A {@link Map} of values to set on the SQL's prepared statement.
180196
*

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JdbcPagingItemReaderBuilderTests.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@
4646

4747
/**
4848
* @author Michael Minella
49+
* @author Drummond Dawson
4950
*/
5051
public class JdbcPagingItemReaderBuilderTests {
5152

@@ -254,6 +255,34 @@ public void testParameters() throws Exception {
254255
assertEquals("9", item1.getThird());
255256
}
256257

258+
@Test
259+
public void testBeanRowMapper() throws Exception {
260+
Map<String, Order> sortKeys = new HashMap<>(1);
261+
sortKeys.put("ID", Order.DESCENDING);
262+
263+
JdbcPagingItemReader<Foo> reader = new JdbcPagingItemReaderBuilder<Foo>()
264+
.name("fooReader")
265+
.currentItemCount(1)
266+
.dataSource(this.dataSource)
267+
.maxItemCount(2)
268+
.selectClause("SELECT ID, FIRST, SECOND, THIRD")
269+
.fromClause("FOO")
270+
.sortKeys(sortKeys)
271+
.beanRowMapper(Foo.class)
272+
.build();
273+
274+
reader.afterPropertiesSet();
275+
276+
reader.open(new ExecutionContext());
277+
Foo item1 = reader.read();
278+
assertNull(reader.read());
279+
280+
assertEquals(3, item1.getId());
281+
assertEquals(10, item1.getFirst());
282+
assertEquals("11", item1.getSecond());
283+
assertEquals("12", item1.getThird());
284+
}
285+
257286
@Test
258287
public void testValidation() {
259288

@@ -342,6 +371,8 @@ public static class Foo {
342371
private String second;
343372
private String third;
344373

374+
public Foo() {}
375+
345376
public Foo(int id, int first, String second, String third) {
346377
this.id = id;
347378
this.first = first;

0 commit comments

Comments
 (0)