【发布时间】:2015-02-10 05:48:04
【问题描述】:
我是 Spring Batch 的新手。
我只是想知道我们是否可以从 Itemprocessor 进行数据库调用(jdbccursoritemreader)?
我需要读取数据库(ItemReader),发送记录以进行处理(ItemProcessor),在处理时我需要调用其他数据库(就像参考数据一样)来更新我从 ItemReader 获得的记录最后将最终的发送给作家。
感谢任何解决方法和建议。
谢谢。
【问题讨论】:
我是 Spring Batch 的新手。
我只是想知道我们是否可以从 Itemprocessor 进行数据库调用(jdbccursoritemreader)?
我需要读取数据库(ItemReader),发送记录以进行处理(ItemProcessor),在处理时我需要调用其他数据库(就像参考数据一样)来更新我从 ItemReader 获得的记录最后将最终的发送给作家。
感谢任何解决方法和建议。
谢谢。
【问题讨论】:
是的,你可以这样做。
您需要在处理器中注入一个类来为您读取数据库。为简单起见,我可能会使用 JdbcTemplate。
类似这样的:
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;
}
}
当你配置你的处理器时,注入 JdbcTemplate:
<bean class="com.example.MyProcessor" id="myProcessor">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
【讨论】: