0

I am new to Spring. I have a use case where I need to execute same multiple sql queries and return same POJO for every query. I would like to write one Item reader and change the query in each step. Is there a way to do this?

1
  • what do you mean by in each step ? is it a multi step job? In my opinion, if you are going to write a custom reader, you can code it the way you want. It might not be possible with API provided readers. Commented Jul 26, 2017 at 5:23

1 Answer 1

1

You can use spring batch late binding by adding @StepScope in your reader

Sample code

@StepScope
@Bean
public ItemReader<Pojo> myReader() {
    JdbcCursorItemReader<Pojo> reader = new JdbcCursorItemReader<>();
    reader.setDataSource(basicDataSource);
    //You can inject sql as per you need 
    //Some expamles 
    //using #{jobParameters['']}
    //using {jobExecutionContext['input.file.name']}" 
    //using #{stepExecutionContext['input.file.name']}"
    reader.setSql("Your-SQL");      
    reader.setRowMapper(new MyMapper());
    return reader;
}

check section 5.4 https://docs.spring.io/spring-batch/reference/html/configureStep.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.