This post will show how to read data from a database and write to excel using Spring Batch. First, we will read data from the database, and using Spring Batch we will write it to excel. For reading the data from the database we will use JdbcCursorItemReader. Consider we have the below records in the database.
Example for how to upload an excel file to DB.
A high-level overview of Spring Batch Example to read Data from the Database and write to excel.
Define RowMapper i.e StudentResultRowMapper
@Component
public class StudentResultRowMapper implements RowMapper<Student> {
@Override
public Student mapRow(ResultSet rs, int i) throws SQLException {
Student student = new Student();
student.setId(rs.getLong("id"));
student.setRollNumber(rs.getString("roll_number"));
student.setName(rs.getString("name"));
return student;
}
}
Changes need to be made in the SpringBatchConfig file as below.
Configure JdbcCursorItemReader.
@Bean
public JdbcCursorItemReader<Student> reader(){
JdbcCursorItemReader<Student> reader = new JdbcCursorItemReader<>();
reader.setDataSource(dataSource);
reader.setSql("select id, roll_number, name from student");
reader.setRowMapper(new StudentResultRowMapper());
return reader;
}
Configure FlatFileItemReader
@Bean
public FlatFileItemWriter<Student> writer(){
FlatFileItemWriter<Student> writer = new FlatFileItemWriter<>();
writer.setResource(new FileSystemResource("C://data/batch/data.csv"));
DelimitedLineAggregator<Student> aggregator = new DelimitedLineAggregator<>();
writer.setLineAggregator(getDelimitedLineAggregator());
return writer;
}
private DelimitedLineAggregator<Student> getDelimitedLineAggregator() {
BeanWrapperFieldExtractor<Student> beanWrapperFieldExtractor = new BeanWrapperFieldExtractor<Student>();
beanWrapperFieldExtractor.setNames(new String[] {"id", "rollNumber", "name"});
DelimitedLineAggregator<Student > aggregator = new DelimitedLineAggregator<Student>();
aggregator.setDelimiter(",");
aggregator.setFieldExtractor(beanWrapperFieldExtractor);
return aggregator;
}
Configure step
@Bean
public Step getDbToCsvStep() {
StepBuilder stepBuilder = stepBuilderFactory.get("getDbToCsvStep");
SimpleStepBuilder<Student, Student> simpleStepBuilder = stepBuilder.chunk(1);
return simpleStepBuilder.reader(reader()).writer(writer()).build();
}
Configure Job
@Bean
public Job dbToCsvJob() {
JobBuilder jobBuilder = jobBuilderFactory.get("dbToCsvJob");
jobBuilder.incrementer(new RunIdIncrementer());
FlowJobBuilder flowJobBuilder = jobBuilder.flow(getDbToCsvStep()).end();
Job job = flowJobBuilder.build();
return job;
}
Let’s put all code together in SpringBatchConfig.java file
package com.springbatchexample.config;
import com.springbatchexample.entity.Student;
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.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowJobBuilder;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.builder.SimpleStepBuilder;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.LineMapper;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
import org.springframework.batch.item.file.transform.DelimitedLineAggregator;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.validator.ValidatingItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import javax.sql.DataSource;
@EnableBatchProcessing
@Configuration
public class SpringBatchConfig {
@Autowired
private DataSource dataSource;
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public JdbcCursorItemReader<Student> reader(){
JdbcCursorItemReader<Student> reader = new JdbcCursorItemReader<>();
reader.setDataSource(dataSource);
reader.setSql("select id, roll_number, name from student");
reader.setRowMapper(new StudentResultRowMapper());
return reader;
}
@Bean
public FlatFileItemWriter<Student> writer(){
FlatFileItemWriter<Student> writer = new FlatFileItemWriter<>();
writer.setResource(new FileSystemResource("C://data/batch/data.csv"));
DelimitedLineAggregator<Student> aggregator = new DelimitedLineAggregator<>();
writer.setLineAggregator(getDelimitedLineAggregator());
return writer;
}
private DelimitedLineAggregator<Student> getDelimitedLineAggregator() {
BeanWrapperFieldExtractor<Student> beanWrapperFieldExtractor = new BeanWrapperFieldExtractor<Student>();
beanWrapperFieldExtractor.setNames(new String[] {"id", "rollNumber", "name"});
DelimitedLineAggregator<Student > aggregator = new DelimitedLineAggregator<Student>();
aggregator.setDelimiter(",");
aggregator.setFieldExtractor(beanWrapperFieldExtractor);
return aggregator;
}
@Bean
public Step getDbToCsvStep() {
StepBuilder stepBuilder = stepBuilderFactory.get("getDbToCsvStep");
SimpleStepBuilder<Student, Student> simpleStepBuilder = stepBuilder.chunk(1);
return simpleStepBuilder.reader(reader()).writer(writer()).build();
}
@Bean
public Job dbToCsvJob() {
JobBuilder jobBuilder = jobBuilderFactory.get("dbToCsvJob");
jobBuilder.incrementer(new RunIdIncrementer());
FlowJobBuilder flowJobBuilder = jobBuilder.flow(getDbToCsvStep()).end();
Job job = flowJobBuilder.build();
return job;
}
}
Let’s see the complete Example to read Data from the Database and write to Excel
Create a new maven project and update pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javatute.com</groupId>
<artifactId>springbatch</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
Directory structure Spring Batch Example to read Data from the Database and write to Excel
Student.java
package com.springbatchexample.entity;
public class Student {
private Long id;
private String name;
private String rollNumber;
public Student() {
}
public Student(Long id, String name, String rollNumber) {
this.id = id;
this.name = name;
this.rollNumber = rollNumber;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRollNumber() {
return rollNumber;
}
public void setRollNumber(String rollNumber) {
this.rollNumber = rollNumber;
}
}
StudentItemProcessor.java
package com.springbatchexample.config;
import com.springbatchexample.entity.Student;
import org.springframework.batch.item.ItemProcessor;
public class StudentItemProcessor implements ItemProcessor<Student, Student> {
@Override
public Student process(Student student) throws Exception {
return student;
}
}
StudentResultRowMapper.java
package com.springbatchexample.config;
import com.springbatchexample.entity.Student;
import org.springframework.http.HttpHeaders;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
@Component
public class StudentResultRowMapper implements RowMapper<Student> {
@Override
public Student mapRow(ResultSet rs, int i) throws SQLException {
Student student = new Student();
student.setId(rs.getLong("id"));
student.setRollNumber(rs.getString("roll_number"));
student.setName(rs.getString("name"));
return student;
}
}
SpringMain.java
@SpringBootApplication
@ComponentScan(basePackages = "com.springbatchexample.*")
public class SpringMain {
public static void main(String[] args) {
SpringApplication.run(SpringMain.class, args);
}
}
services.properties
spring.datasource.url=jdbc:mysql://localhost:3306/springbootcrudexample
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
server.port = 9091
spring.batch.initialize-schema=always
Once we run the application, we should able to see records in excel file.
Download the source code from GitHub.
You may like other Spring Batch tutorials.
- Spring Batch FlatFileItemReader Example
- JpaPagingItemReader Example
- JdbcPagingItemReader spring batch example
- StaxEventItemReader Example
- JsonItemReader Spring Batch Example
- JdbcCursorItemReader Spring Batch Example
- CompositeItemProcessor Spring Batch Example
- Spring Batch ItemReader Example
- StoredProcedureItemReader example
- AsyncItemProcessor Spring Batch Example