Spring Batch Example Reading/Write from Database to File and File to Database

In this Article you will learn how to read date from data base to csv or xml file an xml file to Database using Spring batch.

it is very simple example we are using my sql data base and csv and xml files

Pre Requisites:

1.Java 1.7

2. Spring

3.Maven

4. MySQL

Pom.xml

2.  Create Table in Database

3. insert data into userdetails table.

4. Write Java Class to map  user details table

5. Write Mapper to map data from db to userDetails object

6. Write Context.xml file to define beans jobRepository ,transactionManager, jobLauncher

7.Write database.xml file to define datasource about your mysql

8.define job xml file where we can define all job related reader and Writer

Item Reader which reads data from Db based on Age criteria

 

flatFileItemWriter it writes data into .csv file file:csv/outputs/users.all.csv

Similar way it will write data into xml fil also

Final Class Execute application

Out put data will write into Flat file

users.all.csv

if you switch write from flatFileItemWriter to ItemWriter in testJob it writes data into xml file

Source Code Download 

Posted in Spring.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.