Skip to content
Merged
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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<java.test.version>17</java.test.version>
<java.test.release.version>17</java.test.release.version>
<junit.jupiter.version>6.0.1</junit.jupiter.version>
<spring.batch.version>5.2.4</spring.batch.version>
<spring.batch.version>6.0.0</spring.batch.version>

<checkstyle.config>checkstyle-override.xml</checkstyle.config>

Expand Down Expand Up @@ -142,7 +142,7 @@
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.5</version>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,26 @@

import javax.sql.DataSource;

import java.util.Objects;

import examples.springbatch.common.PersonRecord;
import examples.springbatch.mapper.PersonDynamicSqlSupport;
import examples.springbatch.mapper.PersonMapper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.dynamic.sql.insert.InsertDSL;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.batch.MyBatisBatchItemWriter;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.job.Job;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.job.parameters.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.Step;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.infrastructure.item.ItemProcessor;
import org.springframework.batch.infrastructure.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
Expand All @@ -45,10 +50,6 @@
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.transaction.PlatformTransactionManager;

import examples.springbatch.common.PersonRecord;
import examples.springbatch.mapper.PersonDynamicSqlSupport;
import examples.springbatch.mapper.PersonMapper;

@EnableBatchProcessing
@Configuration
@ComponentScan("examples.springbatch.bulkinsert")
Expand All @@ -59,9 +60,6 @@ public class BulkInsertConfiguration {
@Autowired
private JobRepository jobRepository;

@Autowired
private PlatformTransactionManager transactionManager;

@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
Expand All @@ -76,7 +74,7 @@ public DataSource dataSource() {
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
return sessionFactory.getObject();
return Objects.requireNonNull(sessionFactory.getObject());
}

@Bean
Expand Down Expand Up @@ -104,7 +102,7 @@ public MyBatisBatchItemWriter<PersonRecord> writer(SqlSessionFactory sqlSessionF
@Bean
public Step step1(ItemProcessor<PersonRecord, PersonRecord> processor, ItemWriter<PersonRecord> writer) {
return new StepBuilder("step1", jobRepository)
.<PersonRecord, PersonRecord>chunk(10, transactionManager)
.<PersonRecord, PersonRecord>chunk(10)
.reader(new TestRecordGenerator())
.processor(processor)
.writer(writer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,28 @@
import static examples.springbatch.mapper.PersonDynamicSqlSupport.*;
import static org.assertj.core.api.Assertions.assertThat;

import examples.springbatch.mapper.PersonMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.Test;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.CountDSL;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.core.job.JobExecution;
import org.springframework.batch.core.step.StepExecution;
import org.springframework.batch.infrastructure.item.ExecutionContext;
import org.springframework.batch.test.JobOperatorTestUtils;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import examples.springbatch.mapper.PersonMapper;

@SpringBatchTest
@SpringJUnitConfig(classes = BulkInsertConfiguration.class)
class SpringBatchBulkInsertTest {

@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
private JobOperatorTestUtils jobOperatorTestUtils;

@Autowired
private SqlSessionFactory sqlSessionFactory;
Expand All @@ -50,7 +49,7 @@ void testThatRowsAreInserted() throws Exception {
// starting condition
assertThat(rowCount()).isZero();

JobExecution execution = jobLauncherTestUtils.launchJob();
JobExecution execution = jobOperatorTestUtils.startJob();
assertThat(execution.getExitStatus()).isEqualTo(ExitStatus.COMPLETED);
assertThat(numberOfRowsProcessed(execution)).isEqualTo(TestRecordGenerator.recordCount());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@
*/
package examples.springbatch.bulkinsert;

import org.springframework.batch.item.ItemReader;

import examples.springbatch.common.PersonRecord;
import org.jspecify.annotations.Nullable;
import org.springframework.batch.infrastructure.item.ItemReader;

public class TestRecordGenerator implements ItemReader<PersonRecord> {

private int index = 0;

private static final PersonRecord[] testRecords = {
new PersonRecord("Fred", "Flintstone"),
new PersonRecord("Wilma", "Flintstone"),
new PersonRecord("Pebbles", "Flintstone"),
new PersonRecord("Barney", "Rubble"),
new PersonRecord("Betty", "Rubble"),
new PersonRecord("Bamm Bamm", "Rubble")
new PersonRecord(null, "Fred", "Flintstone"),
new PersonRecord(null, "Wilma", "Flintstone"),
new PersonRecord(null, "Pebbles", "Flintstone"),
new PersonRecord(null, "Barney", "Rubble"),
new PersonRecord(null, "Betty", "Rubble"),
new PersonRecord(null, "Bamm Bamm", "Rubble")
};

@Override
public PersonRecord read() {
public @Nullable PersonRecord read() {
if (index < testRecords.length) {
return (testRecords[index++]);
} else {
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/examples/springbatch/bulkinsert/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2016-2025 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@NullMarked
package examples.springbatch.bulkinsert;

import org.jspecify.annotations.NullMarked;
16 changes: 6 additions & 10 deletions src/test/java/examples/springbatch/common/PersonProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package examples.springbatch.common;

import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.BeforeChunk;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.core.step.StepExecution;
import org.springframework.batch.infrastructure.item.Chunk;
import org.springframework.batch.infrastructure.item.ExecutionContext;
import org.springframework.batch.infrastructure.item.ItemProcessor;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -32,11 +32,7 @@
public PersonRecord process(PersonRecord person) {
incrementRowCount();

PersonRecord transformed = new PersonRecord();
transformed.setId(person.getId());
transformed.setFirstName(person.getFirstName().toUpperCase());
transformed.setLastName(person.getLastName().toUpperCase());
return transformed;
return new PersonRecord(person.id(), person.firstName().toUpperCase(), person.lastName().toUpperCase());
}

@BeforeStep
Expand All @@ -45,7 +41,7 @@
}

@BeforeChunk
public void beforeChunk(ChunkContext chunkContext) {
public void beforeChunk(Chunk<PersonRecord> chunk) {
incrementChunkCount();
}

Expand Down
39 changes: 2 additions & 37 deletions src/test/java/examples/springbatch/common/PersonRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,6 @@
*/
package examples.springbatch.common;

public class PersonRecord {
private Integer id;
private String firstName;
private String lastName;
import org.jspecify.annotations.Nullable;

public PersonRecord() {
super();
}

public PersonRecord(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}
}
public record PersonRecord(@Nullable Integer id, String firstName, String lastName) {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static examples.springbatch.mapper.PersonDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

import java.util.Objects;

import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
Expand All @@ -30,9 +32,9 @@ public class UpdateStatementConvertor implements Converter<PersonRecord, UpdateS
@Override
public UpdateStatementProvider convert(PersonRecord source) {
return UpdateDSL.update(person)
.set(firstName).equalTo(source::getFirstName)
.set(lastName).equalTo(source::getLastName)
.where(id, isEqualTo(source::getId))
.set(firstName).equalTo(source::firstName)
.set(lastName).equalTo(source::lastName)
.where(id, isEqualTo(Objects.requireNonNull(source.id())))
.build()
.render(RenderingStrategies.MYBATIS3);
}
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/examples/springbatch/common/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2016-2025 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@NullMarked
package examples.springbatch.common;

import org.jspecify.annotations.NullMarked;
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

import javax.sql.DataSource;

import java.util.Objects;

import examples.springbatch.common.PersonRecord;
import examples.springbatch.mapper.PersonMapper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
Expand All @@ -31,16 +35,16 @@
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.batch.MyBatisBatchItemWriter;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.job.Job;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.job.parameters.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.Step;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.infrastructure.item.ItemProcessor;
import org.springframework.batch.infrastructure.item.ItemReader;
import org.springframework.batch.infrastructure.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
Expand All @@ -51,9 +55,6 @@
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.transaction.PlatformTransactionManager;

import examples.springbatch.common.PersonRecord;
import examples.springbatch.mapper.PersonMapper;

@EnableBatchProcessing
@Configuration
@ComponentScan("examples.springbatch.common")
Expand All @@ -63,9 +64,6 @@ public class CursorReaderBatchConfiguration {
@Autowired
private JobRepository jobRepository;

@Autowired
private PlatformTransactionManager transactionManager;

@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
Expand All @@ -81,7 +79,7 @@ public DataSource dataSource() {
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
return sessionFactory.getObject();
return Objects.requireNonNull(sessionFactory.getObject());
}

@Bean
Expand Down Expand Up @@ -117,7 +115,7 @@ public MyBatisBatchItemWriter<PersonRecord> writer(SqlSessionFactory sqlSessionF
@Bean
public Step step1(ItemReader<PersonRecord> reader, ItemProcessor<PersonRecord, PersonRecord> processor, ItemWriter<PersonRecord> writer) {
return new StepBuilder("step1", jobRepository)
.<PersonRecord, PersonRecord>chunk(10, transactionManager)
.<PersonRecord, PersonRecord>chunk(10)
.reader(reader)
.processor(processor)
.writer(writer)
Expand Down
Loading