Spring Batch Scheduler CSV to Mysql with Spring Boot, easy in 10 minutes

Today, I show you how to create a Spring Batch Scheduler CSV to Mysql Example. It includes a job scheduler that is triggered periodically with a cron expression. This job scheduler will read data from a CSV file then insert data into Mysql Database. It is an interesting example, isn’t it?. Let’s dive into it.

Spring Batch Scheduler CSV to Mysql Example

Project Setup

Tools and frameworks that we use:
Spring Boot 2.2.2 RELEASE
Spring Batch 4.2.1
Maven 3.6
Java 8
Mysql 5.7

Project Directory

Our project will have a structure like below

springbatch csv mysql

Project Dependencies

We’re using the dependencies like below

DataSource

It’s really easy to configure data source with spring boot

Creating the Model

Here, we assume that we need to read employee information from CSV file named employees.csv, its format looks like below:

This file is located in the project folder resources /csv/employees.csv

Then, we create an Entity class that its object map with data in the above CSV file

Configuring Spring Batch Jobs

We assume that we have a data CSV file that contains a list of employees, its format looks like below.

Create BatchConfig class and add the @EnableBatchProcessing annotation for this class to get the support of Spring Batch features.

Processing the Data

In this post, we only create a simple ItemProcessor below.

Configuring SpringBootApplication class

To configure Spring Boot application we create a simple class below. If you haven’t had knowledge about Spring Boot yet. Let’s check out the post Spring Boot Tutorial for Beginners

Dig deeper: Above, we implement the job scheduler with cron patternĀ */10 * * * * *? . That means it runs every ten seconds. If you want to know more cron pattern, check out the post Spring Batch Scheduler with Spring Boot Example.

Running Application

To demo our Spring Batch Scheduler CSV to Mysql Example, we run the below command in the console.

Note: You must be in your project directory before run that command.
While running that command, you will see the output like below

Spring Batch Scheduler CSV to Mysql

Now, we have a look into Mysql database

Spring Batch Scheduler CSV to Mysql

That’s all about Spring Batch Scheduler CSV to Mysql Example.

If you find this post useful, don't hesitate to share it with your friends or other people for growing knowledge together and is an effort to contribute us.

References

Spring Boot Tutorial for Beginners
Spring Batch Frameworks
Spring Batch Scheduler with Spring Boot Example

Download the complete source code

SpringBatch-CSV-Mysql-Database.zip (41 downloads)
Please share it if you found this useful
Hide Buttons