我创建了一个简单的Boot / Spring Batch 3.0.8.RELEASE作业。我创建了一个实现JobParametersIncrementer的简单类来转到数据库,查找查询应查找的天数并将它们放入JobParameters对象。
我在JdbcCursorItemReader中需要该值,因为它是基于查找的JobParameters之一选择数据,但我无法通过Java注释来解决这个问题。 XML示例很多,而不是Java。
下面是我运行作业的BatchConfiguration类。
`
@Autowired
SendJobParms jobParms; // this guy queries DB and puts data into JobParameters
@Bean
public Job job(@Qualifier("step1") Step step1, @Qualifier("step2") Step step2) {
return jobs.get("DW_Send").incrementer(jobParms).start(step1).next(step2).build();
}
@Bean
protected Step step2(ItemReader<McsendRequest> reader,
ItemWriter<McsendRequest> writer) {
return steps.get("step2")
.<McsendRequest, McsendRequest> chunk(5000)
.reader(reader)
.writer(writer)
.build();
}
@Bean
public JdbcCursorItemReader reader() {
JdbcCursorItemReader<McsendRequest> itemReader = new JdbcCursorItemReader<McsendRequest>();
itemReader.setDataSource(dataSource);
// want to get access to JobParameter here so I can pull values out for my sql query.
itemReader.setSql("select xxxx where rownum <= JobParameter.getCount()");
itemReader.setRowMapper(new McsendRequestMapper());
return itemReader;
}
`
答案 0 :(得分:2)
更改阅读器定义如下(类型Long
和名称paramCount
的参数示例):
@Bean
@StepScope
public JdbcCursorItemReader reader(@Value("#{jobParameters[paramCount]}") Long paramCount) {
JdbcCursorItemReader<McsendRequest> itemReader = new JdbcCursorItemReader<McsendRequest>();
itemReader.setDataSource(dataSource);
itemReader.setSql("select xxxx where rownum <= ?");
ListPreparedStatementSetter listPreparedStatementSetter = new ListPreparedStatementSetter();
listPreparedStatementSetter.setParameters(Arrays.asList(paramCount));
itemReader.setPreparedStatementSetter(listPreparedStatementSetter);
itemReader.setRowMapper(new McsendRequestMapper());
return itemReader;
}