Create a sample project for spring batch
We can create a new spring project from site: start.spring.io with the following dependencies:
Lets add dependency for read/write excel file which we created in tasklet example
Now lets create pojo having fields username, phone and dept. We’ll create repository for this pojo. Also in schema.sql we’d have to create the table.
Lets specify h2 connectivity config in properties file and also specify the path of the excel file which we are going to read in this application
Since we are going to read the excel file, lets create the logic to read from the excel file and convert to pojo object
Constants:
Lets specify our job config:
In reader, we are calling the excelUtil method to read the file. Just for demo lets create processor which will just check for null usernames. If present, it will replace it with unknown. We’ll require implementation for ItemWriter which will insert the data into DB by calling the save method of JpaRepository.
Time to test. Looks like its working perfectly. We can see in logs that the processor is called every tie and after 3 repetitions, the data is saved in DB in the form of chunks of size 3.