Spring Batch Boot Read From MySQL Database and Write into a CSV file Tutorial - Spring Boot Batch Tutorial


In this tutorial, we show you how to configure Spring Batch Boot Job to read information from a CSV file and write to MySQL Database using Eclipse Oxygen Java. Spring Batch Boot to read from MySQL database using JdbcCursorItemReader and write to a Flat file using FlatFileItemWriter.

Spring Boot Batch Read From MySQL Database and Write into a CSV file

Follow the steps mentioned below to develop this spring boot batch application 
Watch Tutorial

Create Database Table and populate it with sample data

Execute the following MySQL script in order to create a database named springbatch with a table named user and import user sample data
CREATE DATABASE `springbatch` /*!40100 DEFAULT CHARACTER SET utf8 */;

DROP TABLE IF EXISTS `springbatch`.`user`;
CREATE TABLE  `springbatch`.`user` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(45) NOT NULL default '',
  PRIMARY KEY  (`id`)

INSERT INTO user(id,name) VALUES(1, 'Jack Rutorial demo 1');
INSERT INTO user(id,name) VALUES(2, 'Jack Rutorial demo 2');
INSERT INTO user(id,name) VALUES(3, 'Jack Rutorial demo 3');

Project Structure

The following screenshot shows final structure of the project.
Project Structure spring batch db to csv file

Creating the Project

Open Eclipse IDE and install Spring STS Eclipse - Spring tools Eclipse.

Go to File -> New -> Other... Select Spring Starter Project under Spring Boot category then click Next as shown below

Spring Starter Project

In the next screen, you enter the content as shown below then click Next

spring batch db to csv new spring starter project

In the next step, you choose Spring Boot Version is 1.5.10 and choose the Batch dependencies I/O -> Batch, then click Finish

new spring starter project dependencies

Update pom.xml

Add the following to your project's pom.xml 

The pom.xml file
<?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">


 <description>Demo project for Spring Boot</description>

  <relativePath/> <!-- lookup parent from repository -->






Create the CSV file

We create the users.csv file in src/main/resources folder. Model
Create a User class under com.jackrutorial.model package and write the following code in it.
package com.jackrutorial.model;

public class User {
 private Integer id;
 private String name;
 public Integer getId() {
  return id;
 public void setId(Integer id) {
  this.id = id;
 public String getName() {
  return name;
 public void setName(String name) {
  this.name = name;


A custom user processor class that will process each and every User object. Create a UserItemProcessor class under com.jackrutorial package and write the following code in it.
package com.jackrutorial;

import org.springframework.batch.item.ItemProcessor;

import com.jackrutorial.model.User;

public class UserItemProcessor implements ItemProcessor<User, User> {

 public User process(User user) throws Exception {
  return user;

Spring Batch Boot Configuration
Create a Job which will read from database and write into csv file. Create a BatchConfiguration class under com.jackrutorial package and write the following code in it.
package com.jackrutorial;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

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.launch.support.RunIdIncrementer;
import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
import org.springframework.batch.item.file.transform.DelimitedLineAggregator;
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.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import com.jackrutorial.model.User;

public class BatchConfiguration {

 public JobBuilderFactory jobBuilderFactory;
 public StepBuilderFactory stepBuilderFactory;
 public DataSource dataSource;
 public DataSource dataSource() {
  final DriverManagerDataSource dataSource = new DriverManagerDataSource();
  return dataSource;
 public JdbcCursorItemReader<User> reader(){
  JdbcCursorItemReader<User> reader = new JdbcCursorItemReader<User>();
  reader.setSql("SELECT id,name FROM user");
  reader.setRowMapper(new UserRowMapper());
  return reader;
 public class UserRowMapper implements RowMapper<User>{

  public User mapRow(ResultSet rs, int rowNum) throws SQLException {
   User user = new User();
   return user;
 public UserItemProcessor processor(){
  return new UserItemProcessor();
 public FlatFileItemWriter<User> writer(){
  FlatFileItemWriter<User> writer = new FlatFileItemWriter<User>();
  writer.setResource(new ClassPathResource("users.csv"));
  writer.setLineAggregator(new DelimitedLineAggregator<User>() {{
   setFieldExtractor(new BeanWrapperFieldExtractor<User>() {{
    setNames(new String[] { "id", "name" });
  return writer;
 public Step step1() {
  return stepBuilderFactory.get("step1").<User, User> chunk(10)
 public Job exportUserJob() {
  return jobBuilderFactory.get("exportUserJob")
    .incrementer(new RunIdIncrementer())

Run Spring Batch Boot 

Right click to the SpringBatchDatabaseToCsvApplication class, select Run As -> Java Application. This application will read data from user table and write into users.csv file.

Below is the generated users.csv found in {PROJECT_FOLDER}\SpringBatchDatabaseToCsv\target\classes folder
1,Jack Rutorial demo 1
2,Jack Rutorial demo 2
3,Jack Rutorial demo 3


Previous Post
Next Post

post written by: