Add beanRowMapper to JdbcPagingItemReaderBuilder

Sets the rowMapper of the JdbcPagingItemReader as
a BeanPropertyRowMapper.

Issue #819
This commit is contained in:
Drummond Dawson
2019-01-16 21:28:40 -05:00
committed by Mahmoud Ben Hassine
parent e3bc7ded37
commit 8a6a2bd19a
2 changed files with 49 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2020 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.SqlServerPagingQueryProvi
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 @@ import org.springframework.util.Assert;
*
* @author Michael Minella
* @author Glenn Renfro
* @author Drummond Dawson
* @since 4.0
* @see JdbcPagingItemReader
*/
@@ -175,6 +177,20 @@ public class JdbcPagingItemReaderBuilder<T> {
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<T> beanRowMapper(Class<T> mappedClass) {
this.rowMapper = new BeanPropertyRowMapper<>(mappedClass);
return this;
}
/**
* A {@link Map} of values to set on the SQL's prepared statement.
*

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2020 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 @@ import static org.junit.Assert.fail;
/**
* @author Michael Minella
* @author Drummond Dawson
*/
public class JdbcPagingItemReaderBuilderTests {
@@ -254,6 +255,34 @@ public class JdbcPagingItemReaderBuilderTests {
assertEquals("9", item1.getThird());
}
@Test
public void testBeanRowMapper() throws Exception {
Map<String, Order> sortKeys = new HashMap<>(1);
sortKeys.put("ID", Order.DESCENDING);
JdbcPagingItemReader<Foo> reader = new JdbcPagingItemReaderBuilder<Foo>()
.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 class JdbcPagingItemReaderBuilderTests {
private String second;
private String third;
public Foo() {}
public Foo(int id, int first, String second, String third) {
this.id = id;
this.first = first;