Skip to content

BATCH-2785: Add beanRowMapper to JdbcPagingItemReaderBuilder #679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -46,6 +47,7 @@
*
* @author Michael Minella
* @author Glenn Renfro
* @author Drummond Dawson
* @since 4.0
* @see JdbcPagingItemReader
*/
Expand Down Expand Up @@ -175,6 +177,20 @@ public JdbcPagingItemReaderBuilder<T> rowMapper(RowMapper<T> 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<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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -46,6 +46,7 @@

/**
* @author Michael Minella
* @author Drummond Dawson
*/
public class JdbcPagingItemReaderBuilderTests {

Expand Down Expand Up @@ -254,6 +255,34 @@ public void testParameters() throws Exception {
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() {

Expand Down Expand Up @@ -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;
Expand Down