Skip to content

Commit 8b0007a

Browse files
committed
Add java configuration for the Jdbc paging sample
Issue #3663
1 parent 5f12a83 commit 8b0007a

File tree

10 files changed

+269
-120
lines changed

10 files changed

+269
-120
lines changed

spring-batch-samples/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ remote client (such as JConsole from the JDK) which does not have
101101
Spring Batch on the classpath. See the Spring Core Reference Guide
102102
for more details on how to customise the JMX configuration.
103103

104-
### Jdbc Cursor and Batch Update sample
104+
### Jdbc Readers and Batch Update sample
105105

106106
The purpose of this sample is to show to usage of the
107-
`JdbcCursorItemReader` and the `JdbcBatchItemWriter` to make
107+
`JdbcCursorItemReader`/`JdbcPagingItemReader` and the `JdbcBatchItemWriter` to make
108108
efficient updates to a database table.
109109

110-
[Jdbc Cursor and Batch Update sample](./src/main/java/org/springframework/batch/sample/jdbc/README.md)
110+
[Jdbc Readers and Batch Update sample](./src/main/java/org/springframework/batch/sample/jdbc/README.md)
111111

112112
### Amqp Job Sample
113113

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
import org.springframework.batch.core.job.builder.JobBuilder;
2323
import org.springframework.batch.core.repository.JobRepository;
2424
import org.springframework.batch.core.step.builder.StepBuilder;
25+
import org.springframework.batch.item.ItemReader;
2526
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
2627
import org.springframework.batch.item.database.JdbcBatchItemWriter;
27-
import org.springframework.batch.item.database.JdbcCursorItemReader;
2828
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
29-
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;
3029
import org.springframework.batch.sample.domain.trade.CustomerCredit;
3130
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
32-
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper;
3331
import org.springframework.context.annotation.Bean;
3432
import org.springframework.context.annotation.Configuration;
3533
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
@@ -41,17 +39,7 @@
4139
*/
4240
@Configuration
4341
@EnableBatchProcessing
44-
public class JdbcCursorReaderBatchWriterSampleJob {
45-
46-
@Bean
47-
public JdbcCursorItemReader<CustomerCredit> itemReader() {
48-
String sql = "select ID, NAME, CREDIT from CUSTOMER";
49-
return new JdbcCursorItemReaderBuilder<CustomerCredit>().name("customerReader")
50-
.dataSource(dataSource())
51-
.sql(sql)
52-
.rowMapper(new CustomerCreditRowMapper())
53-
.build();
54-
}
42+
public class JdbcReaderBatchWriterSampleJob {
5543

5644
@Bean
5745
public JdbcBatchItemWriter<CustomerCredit> itemWriter() {
@@ -64,12 +52,13 @@ public JdbcBatchItemWriter<CustomerCredit> itemWriter() {
6452
}
6553

6654
@Bean
67-
public Job job(JobRepository jobRepository, JdbcTransactionManager transactionManager) {
55+
public Job job(JobRepository jobRepository, JdbcTransactionManager transactionManager,
56+
ItemReader<CustomerCredit> itemReader, JdbcBatchItemWriter<CustomerCredit> itemWriter) {
6857
return new JobBuilder("ioSampleJob", jobRepository)
6958
.start(new StepBuilder("step1", jobRepository).<CustomerCredit, CustomerCredit>chunk(2, transactionManager)
70-
.reader(itemReader())
59+
.reader(itemReader)
7160
.processor(new CustomerCreditIncreaseProcessor())
72-
.writer(itemWriter())
61+
.writer(itemWriter)
7362
.build())
7463
.build();
7564
}

spring-batch-samples/src/main/java/org/springframework/batch/sample/jdbc/README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
### Jdbc Cursor and Batch Update sample
1+
### Jdbc Readers and Batch Update sample
22

33
## About
44

55
The purpose of this sample is to show to usage of the
6-
`JdbcCursorItemReader` and the `JdbcBatchItemWriter` to make
6+
`JdbcCursorItemReader`/`JdbcPagingItemReader` and the `JdbcBatchItemWriter` to make
77
efficient updates to a database table.
88

99
The `JdbcBatchItemWriter` accepts a special form of
@@ -18,13 +18,23 @@ which is used to configure the query for the writer.
1818

1919
## Run the sample
2020

21-
You can run the sample from the command line as following:
21+
You can run the cursor reader sample from the command line as following:
2222

2323
```
2424
$>cd spring-batch-samples
2525
# Launch the sample using the XML configuration
26-
$>../mvnw -Dtest=JdbcCursorFunctionalTests#testLaunchJobWithXmlConfiguration test
26+
$>../mvnw -Dtest=JdbcCursorFunctionalTests#testLaunchJobWithXmlConfig test
2727
# Launch the sample using the Java configuration
28-
$>../mvnw -Dtest=JdbcCursorFunctionalTests#testLaunchJobWithJavaConfiguration test
28+
$>../mvnw -Dtest=JdbcCursorFunctionalTests#testLaunchJobWithJavaConfig test
29+
```
30+
31+
You can run the paging reader sample from the command line as following:
32+
33+
```
34+
$>cd spring-batch-samples
35+
# Launch the sample using the XML configuration
36+
$>../mvnw -Dtest=JdbcPagingFunctionalTests#testLaunchJobWithXmlConfig test
37+
# Launch the sample using the Java configuration
38+
$>../mvnw -Dtest=JdbcPagingFunctionalTests#testLaunchJobWithJavaConfig test
2939
```
3040

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.sample.jdbc.cursor;
17+
18+
import javax.sql.DataSource;
19+
20+
import org.springframework.batch.item.database.JdbcCursorItemReader;
21+
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;
22+
import org.springframework.batch.sample.domain.trade.CustomerCredit;
23+
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper;
24+
import org.springframework.batch.sample.jdbc.JdbcReaderBatchWriterSampleJob;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
28+
/**
29+
* @author Mahmoud Ben Hassine
30+
*/
31+
@Configuration
32+
public class JdbcCursorReaderBatchWriterSampleJob extends JdbcReaderBatchWriterSampleJob {
33+
34+
@Bean
35+
public JdbcCursorItemReader<CustomerCredit> itemReader(DataSource dataSource) {
36+
String sql = "select ID, NAME, CREDIT from CUSTOMER";
37+
return new JdbcCursorItemReaderBuilder<CustomerCredit>().name("customerReader")
38+
.dataSource(dataSource)
39+
.sql(sql)
40+
.rowMapper(new CustomerCreditRowMapper())
41+
.build();
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.sample.jdbc.paging;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
import javax.sql.DataSource;
22+
23+
import org.springframework.batch.core.configuration.annotation.StepScope;
24+
import org.springframework.batch.item.database.JdbcPagingItemReader;
25+
import org.springframework.batch.item.database.Order;
26+
import org.springframework.batch.item.database.builder.JdbcPagingItemReaderBuilder;
27+
import org.springframework.batch.sample.domain.trade.CustomerCredit;
28+
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper;
29+
import org.springframework.batch.sample.jdbc.JdbcReaderBatchWriterSampleJob;
30+
import org.springframework.beans.factory.annotation.Value;
31+
import org.springframework.context.annotation.Bean;
32+
import org.springframework.context.annotation.Configuration;
33+
34+
/**
35+
* @author Mahmoud Ben Hassine
36+
*/
37+
@Configuration
38+
public class JdbcPagingReaderBatchWriterSampleJob extends JdbcReaderBatchWriterSampleJob {
39+
40+
@Bean
41+
@StepScope
42+
public JdbcPagingItemReader<CustomerCredit> itemReader(DataSource dataSource,
43+
@Value("#{jobParameters['credit']}") Double credit) {
44+
Map<String, Object> parameterValues = new HashMap<>();
45+
parameterValues.put("statusCode", "PE");
46+
parameterValues.put("credit", credit);
47+
parameterValues.put("type", "COLLECTION");
48+
49+
return new JdbcPagingItemReaderBuilder<CustomerCredit>().name("customerReader")
50+
.dataSource(dataSource)
51+
.selectClause("select NAME, ID, CREDIT")
52+
.fromClause("FROM CUSTOMER")
53+
.whereClause("WHERE CREDIT > :credit")
54+
.sortKeys(Map.of("ID", Order.ASCENDING))
55+
.rowMapper(new CustomerCreditRowMapper())
56+
.pageSize(2)
57+
.parameterValues(parameterValues)
58+
.build();
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,58 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="
5-
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
6-
7-
<bean id="itemReader" class="org.springframework.batch.item.database.JdbcPagingItemReader" scope="step">
8-
<property name="dataSource" ref="dataSource" />
9-
<property name="rowMapper">
10-
<bean class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper" />
11-
</property>
12-
<property name="queryProvider">
13-
<bean class="org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean">
14-
<property name="dataSource" ref="dataSource" />
15-
<property name="sortKeys">
16-
<map>
17-
<entry key="id" value="ASCENDING"/>
18-
</map>
19-
</property>
20-
<!-- Intentionally put sort key second in the query list as a test -->
21-
<property name="selectClause" value="select NAME, ID, CREDIT" />
22-
<property name="fromClause" value="FROM CUSTOMER" />
23-
<property name="whereClause" value="WHERE CREDIT > :credit" />
24-
</bean>
25-
</property>
26-
<property name="pageSize" value="2" />
27-
<property name="parameterValues">
28-
<map>
29-
<entry key="statusCode" value="PE" />
30-
<entry key="credit" value="#{jobParameters[credit]}" />
31-
<entry key="type" value="COLLECTION" />
32-
</map>
33-
</property>
34-
</bean>
35-
36-
<bean id="itemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
37-
<property name="assertUpdates" value="true" />
38-
<property name="itemSqlParameterSourceProvider">
39-
<bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
40-
</property>
41-
<property name="sql" value="UPDATE CUSTOMER set credit = :credit where id = :id" />
42-
<property name="dataSource" ref="dataSource" />
43-
</bean>
44-
</beans>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:batch="http://www.springframework.org/schema/batch"
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/batch https://www.springframework.org/schema/batch/spring-batch.xsd
7+
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
8+
9+
<batch:job id="ioSampleJob" xmlns="http://www.springframework.org/schema/batch">
10+
<batch:step id="step1">
11+
<batch:tasklet>
12+
<batch:chunk reader="itemReader" processor="itemProcessor" writer="itemWriter"
13+
commit-interval="2"/>
14+
</batch:tasklet>
15+
</batch:step>
16+
</batch:job>
17+
18+
<bean id="itemProcessor"
19+
class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor"/>
20+
21+
<bean id="itemReader" class="org.springframework.batch.item.database.JdbcPagingItemReader" scope="step">
22+
<property name="dataSource" ref="dataSource" />
23+
<property name="rowMapper">
24+
<bean class="org.springframework.batch.sample.domain.trade.internal.CustomerCreditRowMapper" />
25+
</property>
26+
<property name="queryProvider">
27+
<bean class="org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean">
28+
<property name="dataSource" ref="dataSource" />
29+
<property name="sortKeys">
30+
<map>
31+
<entry key="id" value="ASCENDING"/>
32+
</map>
33+
</property>
34+
<!-- Intentionally put sort key second in the query list as a test -->
35+
<property name="selectClause" value="select NAME, ID, CREDIT" />
36+
<property name="fromClause" value="FROM CUSTOMER" />
37+
<property name="whereClause" value="WHERE CREDIT > :credit" />
38+
</bean>
39+
</property>
40+
<property name="pageSize" value="2" />
41+
<property name="parameterValues">
42+
<map>
43+
<entry key="statusCode" value="PE" />
44+
<entry key="credit" value="#{jobParameters[credit]}" />
45+
<entry key="type" value="COLLECTION" />
46+
</map>
47+
</property>
48+
</bean>
49+
50+
<bean id="itemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
51+
<property name="assertUpdates" value="true" />
52+
<property name="itemSqlParameterSourceProvider">
53+
<bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
54+
</property>
55+
<property name="sql" value="UPDATE CUSTOMER set credit = :credit where id = :id" />
56+
<property name="dataSource" ref="dataSource" />
57+
</bean>
58+
</beans>

spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/JdbcPagingFunctionalTests.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

spring-batch-samples/src/test/java/org/springframework/batch/sample/jdbc/JdbcCursorFunctionalTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.batch.core.JobExecution;
2424
import org.springframework.batch.core.JobParameters;
2525
import org.springframework.batch.core.launch.JobLauncher;
26+
import org.springframework.batch.sample.jdbc.cursor.JdbcCursorReaderBatchWriterSampleJob;
2627
import org.springframework.batch.test.JobLauncherTestUtils;
2728
import org.springframework.beans.factory.annotation.Autowired;
2829
import org.springframework.context.ApplicationContext;

0 commit comments

Comments
 (0)