Add beanRowMapper to JdbcCursorItemReaderBuilder

Sets the rowMapper of the JdbcCursorItemReader as a BeanPropertyRowMapper.

Resolves BATCH-2710
This commit is contained in:
Drummond Dawson
2018-04-10 22:55:25 -04:00
committed by Mahmoud Ben Hassine
parent 24ab792da7
commit 32692f9583
2 changed files with 18 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2018 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.
@@ -24,6 +24,7 @@ import org.springframework.batch.item.database.support.ListPreparedStatementSett
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
import org.springframework.jdbc.core.ArgumentTypePreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.Assert;
@@ -310,6 +311,20 @@ public class JdbcCursorItemReaderBuilder<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 JdbcCursorItemReaderBuilder<T> beanRowMapper(Class<T> mappedClass) {
this.rowMapper = new BeanPropertyRowMapper<>(mappedClass);
return this;
}
/**
* Validates configuration and builds a new reader instance.
*

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2018 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.
@@ -301,15 +301,7 @@ public class JdbcCursorItemReaderBuilderTests {
.ignoreWarnings(true)
.driverSupportsAbsolute(true)
.useSharedExtendedConnection(true)
.rowMapper((rs, rowNum) -> {
Foo foo = new Foo();
foo.setFirst(rs.getInt("FIRST"));
foo.setSecond(rs.getString("SECOND"));
foo.setThird(rs.getString("THIRD"));
return foo;
})
.beanRowMapper(Foo.class)
.build();
assertEquals(1, ReflectionTestUtils.getField(reader, "fetchSize"));