Spring Batch Example CSV file to MySQL Database

In this tutorial, we will discuss about an application where we see how to configure a Spring Batch job to read CSV file by FlatFileReader library write into a MySQL database, and also we can filter out some of the records of employees which have less salary or under age employee before writing with ItemProcessor.

This is the main xml file to configure the Spring batch job. This simple-job.xml file define a job to read a employees.csv file, match it to employee plain pojo and write the data into MySQL database Employeet table.

7. Spring Batch ItemProcessor for filter data
In Spring batch, the wired Processor will be fired before writing to any resources, so, this is the best place to handle any conversion, filtering and business logic. In this example, we will be ignored all employees whose have salaried less than 70000 i.e. not write to database as well as ignored those employee whose age less than 22 years.

8. Launching Batch Job-
Spring Batch comes with a simple utility class called CommandLineJobRunner which has a main() method which accepts two arguments. First argument is the spring application context file containing job definition and the second is the name of the job to be executed.

Now run as a java application with both two arguments.org.springframework.batch.core.launch.support.CommandLineJobRunnersimple-job.xml simpleDojJob

Output. The Spring Batch metadata tables are created, and the content of employees.cvs is inserted into database table “EMPLOYEET”.