Skip to content

Commit 2d67b7e

Browse files
kjuyoungfmbenhassine
authored andcommitted
Add Data class support in JdbcCursorItemReaderBuilder & JdbcPagingItemReaderBuilder
Resolves #4578
1 parent f6dcea8 commit 2d67b7e

File tree

4 files changed

+115
-6
lines changed

4 files changed

+115
-6
lines changed

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

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.jdbc.core.BeanPropertyRowMapper;
2626
import org.springframework.jdbc.core.PreparedStatementSetter;
2727
import org.springframework.jdbc.core.RowMapper;
28+
import org.springframework.jdbc.core.DataClassRowMapper;
2829
import org.springframework.util.Assert;
2930
import org.springframework.util.StringUtils;
3031

@@ -38,6 +39,7 @@
3839
* @author Ankur Trapasiya
3940
* @author Parikshit Dutta
4041
* @author Fabio Molignoni
42+
* @author Juyoung Kim
4143
* @since 4.0
4244
*/
4345
public class JdbcCursorItemReaderBuilder<T> {
@@ -312,6 +314,18 @@ public JdbcCursorItemReaderBuilder<T> beanRowMapper(Class<T> mappedClass) {
312314
return this;
313315
}
314316

317+
/**
318+
* Creates a {@link DataClassRowMapper} to be used as your {@link RowMapper}.
319+
* @param mappedClass the class for the row mapper
320+
* @return this instance for method chaining
321+
* @see DataClassRowMapper
322+
*/
323+
public JdbcCursorItemReaderBuilder<T> dataRowMapper(Class<T> mappedClass) {
324+
this.rowMapper = new DataClassRowMapper<>(mappedClass);
325+
326+
return this;
327+
}
328+
315329
/**
316330
* Set whether "autoCommit" should be overridden for the connection used by the
317331
* cursor. If not set, defaults to Connection / Datasource default configuration.

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2023 the original author or authors.
2+
* Copyright 2017-2024 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.
@@ -36,6 +36,7 @@
3636
import org.springframework.batch.item.database.support.SybasePagingQueryProvider;
3737
import org.springframework.batch.support.DatabaseType;
3838
import org.springframework.jdbc.core.BeanPropertyRowMapper;
39+
import org.springframework.jdbc.core.DataClassRowMapper;
3940
import org.springframework.jdbc.core.RowMapper;
4041
import org.springframework.jdbc.support.MetaDataAccessException;
4142
import org.springframework.util.Assert;
@@ -52,6 +53,7 @@
5253
* @author Drummond Dawson
5354
* @author Mahmoud Ben Hassine
5455
* @author Minsoo Kim
56+
* @author Juyoung Kim
5557
* @since 4.0
5658
* @see JdbcPagingItemReader
5759
*/
@@ -186,6 +188,18 @@ public JdbcPagingItemReaderBuilder<T> beanRowMapper(Class<T> mappedClass) {
186188
return this;
187189
}
188190

191+
/**
192+
* Creates a {@link DataClassRowMapper} to be used as your {@link RowMapper}.
193+
* @param mappedClass the class for the row mapper
194+
* @return this instance for method chaining
195+
* @see DataClassRowMapper
196+
*/
197+
public JdbcPagingItemReaderBuilder<T> dataRowMapper(Class<T> mappedClass) {
198+
this.rowMapper = new DataClassRowMapper<>(mappedClass);
199+
200+
return this;
201+
}
202+
189203
/**
190204
* A {@link Map} of values to set on the SQL's prepared statement.
191205
* @param parameterValues Map of values

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

+41-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 the original author or authors.
2+
* Copyright 2016-2024 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.
@@ -47,6 +47,7 @@
4747
* @author Ankur Trapasiya
4848
* @author Parikshit Dutta
4949
* @author Mahmoud Ben Hassine
50+
* @author Juyoung Kim
5051
*/
5152
class JdbcCursorItemReaderBuilderTests {
5253

@@ -334,6 +335,29 @@ private void validateFoo(Foo item, int first, String second, String third) {
334335
assertEquals(third, item.getThird());
335336
}
336337

338+
@Test
339+
void testDataRowMapper() throws Exception {
340+
JdbcCursorItemReader<Bar> reader = new JdbcCursorItemReaderBuilder<Bar>()
341+
.name("barReader")
342+
.dataSource(this.dataSource)
343+
.currentItemCount(1)
344+
.maxItemCount(2)
345+
.sql("SELECT ID, FIRST, SECOND, THIRD FROM BAR ORDER BY ID DESC")
346+
.dataRowMapper(Bar.class)
347+
.build();
348+
349+
reader.afterPropertiesSet();
350+
351+
reader.open(new ExecutionContext());
352+
Bar item1 = reader.read();
353+
assertNull(reader.read());
354+
355+
assertEquals(3, item1.id());
356+
assertEquals(10, item1.first());
357+
assertEquals("11", item1.second());
358+
assertEquals("12", item1.third());
359+
}
360+
337361
public static class Foo {
338362

339363
private int first;
@@ -368,6 +392,8 @@ public void setThird(String third) {
368392

369393
}
370394

395+
public record Bar(int id, int first, String second, String third) {}
396+
371397
@Configuration
372398
public static class TestDataSourceConfiguration {
373399

@@ -376,12 +402,24 @@ CREATE TABLE FOO (
376402
ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
377403
FIRST BIGINT ,
378404
SECOND VARCHAR(5) NOT NULL,
379-
THIRD VARCHAR(5) NOT NULL);""";
405+
THIRD VARCHAR(5) NOT NULL);
406+
407+
CREATE TABLE BAR (
408+
ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
409+
FIRST BIGINT ,
410+
SECOND VARCHAR(5) NOT NULL,
411+
THIRD VARCHAR(5) NOT NULL) ;""";
380412

381413
private static final String INSERT_SQL = """
382414
INSERT INTO FOO (FIRST, SECOND, THIRD) VALUES (1, '2', '3');
383415
INSERT INTO FOO (FIRST, SECOND, THIRD) VALUES (4, '5', '6');
384-
INSERT INTO FOO (FIRST, SECOND, THIRD) VALUES (7, '8', '9');""";
416+
INSERT INTO FOO (FIRST, SECOND, THIRD) VALUES (7, '8', '9');
417+
418+
INSERT INTO BAR (FIRST, SECOND, THIRD) VALUES (1, '2', '3');
419+
INSERT INTO BAR (FIRST, SECOND, THIRD) VALUES (4, '5', '6');
420+
INSERT INTO BAR (FIRST, SECOND, THIRD) VALUES (7, '8', '9');
421+
INSERT INTO BAR (FIRST, SECOND, THIRD) VALUES (10, '11', '12');
422+
INSERT INTO BAR (FIRST, SECOND, THIRD) VALUES (13, '14', '15');""";
385423

386424
@Bean
387425
public DataSource dataSource() {

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

+45-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2023 the original author or authors.
2+
* Copyright 2017-2024 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.
@@ -47,6 +47,7 @@
4747
* @author Michael Minella
4848
* @author Drummond Dawson
4949
* @author Mahmoud Ben Hassine
50+
* @author Juyoung Kim
5051
*/
5152
class JdbcPagingItemReaderBuilderTests {
5253

@@ -262,6 +263,34 @@ void testBeanRowMapper() throws Exception {
262263
assertEquals("12", item1.getThird());
263264
}
264265

266+
@Test
267+
void testDataRowMapper() throws Exception {
268+
Map<String, Order> sortKeys = new HashMap<>(1);
269+
sortKeys.put("ID", Order.DESCENDING);
270+
271+
JdbcPagingItemReader<Bar> reader = new JdbcPagingItemReaderBuilder<Bar>()
272+
.name("barReader")
273+
.dataSource(this.dataSource)
274+
.currentItemCount(1)
275+
.maxItemCount(2)
276+
.selectClause("SELECT ID, FIRST, SECOND, THIRD")
277+
.fromClause("BAR")
278+
.sortKeys(sortKeys)
279+
.dataRowMapper(Bar.class)
280+
.build();
281+
282+
reader.afterPropertiesSet();
283+
284+
reader.open(new ExecutionContext());
285+
Bar item1 = reader.read();
286+
assertNull(reader.read());
287+
288+
assertEquals(3, item1.id());
289+
assertEquals(10, item1.first());
290+
assertEquals("11", item1.second());
291+
assertEquals("12", item1.third());
292+
}
293+
265294
@Test
266295
void testValidation() {
267296
var builder = new JdbcPagingItemReaderBuilder<Foo>();
@@ -354,6 +383,8 @@ public void setThird(String third) {
354383

355384
}
356385

386+
public record Bar(int id, int first, String second, String third) {}
387+
357388
@Configuration
358389
public static class TestDataSourceConfiguration {
359390

@@ -362,14 +393,26 @@ CREATE TABLE FOO (
362393
ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
363394
FIRST BIGINT ,
364395
SECOND VARCHAR(5) NOT NULL,
396+
THIRD VARCHAR(5) NOT NULL) ;
397+
398+
CREATE TABLE BAR (
399+
ID BIGINT IDENTITY NOT NULL PRIMARY KEY ,
400+
FIRST BIGINT ,
401+
SECOND VARCHAR(5) NOT NULL,
365402
THIRD VARCHAR(5) NOT NULL) ;""";
366403

367404
private static final String INSERT_SQL = """
368405
INSERT INTO FOO (FIRST, SECOND, THIRD) VALUES (1, '2', '3');
369406
INSERT INTO FOO (FIRST, SECOND, THIRD) VALUES (4, '5', '6');
370407
INSERT INTO FOO (FIRST, SECOND, THIRD) VALUES (7, '8', '9');
371408
INSERT INTO FOO (FIRST, SECOND, THIRD) VALUES (10, '11', '12');
372-
INSERT INTO FOO (FIRST, SECOND, THIRD) VALUES (13, '14', '15');""";
409+
INSERT INTO FOO (FIRST, SECOND, THIRD) VALUES (13, '14', '15');
410+
411+
INSERT INTO BAR (FIRST, SECOND, THIRD) VALUES (1, '2', '3');
412+
INSERT INTO BAR (FIRST, SECOND, THIRD) VALUES (4, '5', '6');
413+
INSERT INTO BAR (FIRST, SECOND, THIRD) VALUES (7, '8', '9');
414+
INSERT INTO BAR (FIRST, SECOND, THIRD) VALUES (10, '11', '12');
415+
INSERT INTO BAR (FIRST, SECOND, THIRD) VALUES (13, '14', '15');""";
373416

374417
@Bean
375418
public DataSource dataSource() {

0 commit comments

Comments
 (0)