Read a CSV File and write to MySQL Database with Spring Boot Batch Tutorial


In this tutorial, we show you how to read information from a CSV file and write to MySQL Database using Spring Batch Boot and Spring tools Eclipse. Using Spring Batch Flat file reader to read CSV file and Jdbc Batch Item Writer to write MySQL Database.

Read a CSV File and write to MySQL Database with Spring Batch

Follow the steps mentioned below to develop the Spring batch sample application.

Spring Batch video tutorial

Setting up Database

Execute the following MySQL script in order to create a database named springbatch with a table named user.

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`)

Project Structure

The following screenshot shows final structure of the project.

Project Structure Spring Batch CSV to Database

Creating the Project

Open Eclipse Oxygen java and install the Spring STS 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

config 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

Maven Dependencies 

Add the following to your project's pom.xml 

The pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


 <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 and write the following content in it.


Create a User class under com.jackrutorial.bean package and write the following code in it.

package com.jackrutorial.bean;

public class User {
 private String name;

 public String getName() {
  return name;

 public void setName(String 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.bean.User;

public class UserItemProcessor implements ItemProcessor<User, User> {

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


Spring Batch Annotations Configuration

Create a Job which will read users CSV file and write into Mysql Database. Create a BatchConfiguration class under com.jackrutorial package and write the following code in it.

package com.jackrutorial;

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.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import com.jackrutorial.bean.User;

public class BatchConfiguration {

 public JobBuilderFactory jobBuilderFactory;
 public StepBuilderFactory stepBuilderFactory;
 public DataSource dataSource;
 public DataSource dataSource() {
  final DriverManagerDataSource dataSource = new DriverManagerDataSource();
  return dataSource;
 public FlatFileItemReader<User> reader(){
  FlatFileItemReader<User> reader = new FlatFileItemReader<User>();
  reader.setResource(new ClassPathResource("users.csv"));
  reader.setLineMapper(new DefaultLineMapper<User>() {{
   setLineTokenizer(new DelimitedLineTokenizer() {{
    setNames(new String[] { "name" });
   setFieldSetMapper(new BeanWrapperFieldSetMapper<User>() {{
  return reader;
 public UserItemProcessor processor(){
  return new UserItemProcessor();
 public JdbcBatchItemWriter<User> writer(){
  JdbcBatchItemWriter<User> writer = new JdbcBatchItemWriter<User>();
  writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<User>());
  writer.setSql("INSERT INTO user(name) VALUES (:name)");
  return writer;
 public Step step1() {
  return stepBuilderFactory.get("step1").<User, User> chunk(3)
 public Job importUserJob() {
  return jobBuilderFactory.get("importUserJob")
    .incrementer(new RunIdIncrementer())

Run Spring Boot Batch Job

Right click to the SpringBatchCsvToDatabaseApplication class, select Run As -> Java Application. This application will read data from users.csv file and write records in user table.
And finally, check the user table in database. Below is the snapshot from database after this job.

query result


Previous Post
Next Post

post written by: