Spring Batch Read From File And Write To Database From Form

As seen in below code in step1 I'm reading users.xml and writing to database now in step2 I'm reading from userdetails.xml and writing to database but I need step1 auto generated key of tbl_user for step2. How Can I do that?

Spring Batch full example to read from MySQL database using JdbcCursorItemReader and write to a Flat file using FlatFileItemWriter.

users.xml

userdetails.xml

TaherTTaherT

1 Answer

You need to pass data to a future step. For explantory documentation see http://docs.spring.io/spring-batch/trunk/reference/html/patterns.html#passingDataToFutureSteps

I have implemented the example from the documentation and adjusted it to your configuration with some assumptions here and there.

During the read (or the write, it depends when you get the data that you want to pass) in step 1 you need to store the data in the StepExecution. Add to your xmlItemReader the following:

Your xml will look like this:

Add the promotionListener bean:

Spring Batch Read From File And Write To Database From Form

And finally you need to retrieve the value in step 2. Again asuming you need it in the reader of step 2 you reader in step 2 needs the following code added:

Now you have acces to the value read in step 1.

EDIT - adding some example configuration for an extra read step:

After step 1 add a simple step 2 with the following reader to get the new value from the database

Create A Text File And Write To It In Java

And a simple rowMapper bean

Form

You will have to write your exampleRowMapper obviously to reflect the data your fetching. For example:

In your dummywriter you add the stepexecution and you will store your value in the step execution context.:

And the bean for the writer:

And wrap the reader and writer in a step.

Sander_MSander_M

Not the answer you're looking for? Browse other questions tagged mysqlxmlspringspring-batch or ask your own question.

I'm trying to read some records from a database using loops then do some calculations on the records (updating a field called total).

And

But i'm new to spring batch so please can anyone provide me with some tips.

user1744446user1744446

1 Answer

How To Create A File And Write To It

this sounds like something the chunk pattern would address. you can use re-use existing Spring Batch components to read from the database, compose your own processor, then pass back to a Spring Batch component to store.

say the use case is like this;- read a record- record.setTotalColumn(record.getColumn2() + record.getColumn3())- update

this configuration might look like this

Spring Batch Read From File And Write To Database From Formatting

this means that the only thing you have to 'write' from scratch would be the calculator processor which could e something like this;

incomplete-co.deincomplete-co.de

Not the answer you're looking for? Browse other questions tagged spring-batchspring-batch-admin or ask your own question.

Posted on