问题描述:

I am new to Spring Batch.

I just want to know whether can we make database calls(jdbccursoritemreader) from Itemprocessor?

I have a requirement to read the db(ItemReader), send the records for processing(ItemProcessor), at the time of processing I need to call other database (Just like reference data) to update the records which I got from ItemReader and finally send the final ones to writer.

Any workaround and suggestions are appreciated.

Thanks.

网友答案:

Yes, you can do this.

You would need to inject a class into your processor to do the database reading for you. I'd probably go with a JdbcTemplate for simplicity.

Something like this:

public class MyProcessor implements ItemProcessor<Foo, Bar> {

  private JdbcTemplate jdbcTemplate;

  @Override
  public Foo process(Bar bar) throws Exception {
    //use JdbcTemplate here
  }

  public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
  }

}

When you configure your processor, inject the JdbcTemplate:

<bean class="com.example.MyProcessor" id="myProcessor">
  <property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
相关阅读:
Top