i’m trying to pass a parameter to my query in spring batch. I decided to create a tasklet and use JdbcTemplate as follows …
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext)
throws EpsilonBatchBusinessException {
LOGGER.debug("Enter execute.");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.query(queryString,
new PreparedStatementSetter() {
public void setValues(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setInt(1, runNumber);
}
},
rowMapper);
LOGGER.debug("Exit execute.");
return RepeatStatus.FINISHED;
}
So am injecting to this bean a dataSource, queryString, rowMapper object, and the parameter (runNumber) .. This tasklet will be called within a step to create a list. I usually pass the row mapper to JdbcCursorItemReader spring bean and wouldn’t write a tasklet, but my query string needs a parameter hence am writing this tasklet. Am just not sure if this tasklet will do the trick as with JdbcCursorItemReader? Your input wil be appreciated
A better option would be to use the
JdbcCursorItemReaderand write a custom PreparedStatementSetter.The
PreparedStatementSetterinterface is very simple; pretty much all the code you’d need to write is below. Once the setter is written, all you need to do is configure it as a new bean with therunNumbervalue injected in the config, and then inject that bean into aJdbcCursorItemReader. This allows you to use all the usualItemReaders andItemWriters instead of having to implement everything by hand in aTasklet.