【发布时间】:2016-06-02 09:39:20
【问题描述】:
在 Spring 批处理中,我的 CompositeItemWriter 使用多个 ItemWriter 将记录插入到多个表中,但目前每个 ItemWriter 都在批量插入,如下所示
public class Item{
public List<WarningObject> warnings
public List<ErrorObject> errors
public Long Id;
public String data1;
public String data2;
public String data3;
// getters and setters
}
我的作家配置
<bean id="compositeItemWriter" class="org.springframework.batch.item.support.CompositeItemWriter">
<property name="delegates">
<list>
<ref bean="warningItemWriter"/>
<ref bean="errorItemWriter"/>
<ref bean="CustomJdbcItemWriter"/>
</list>
</property>
public class WarningItemWriter implements ItemWriter<Item>{
@Autowire
String sql;
@AutoWire
JdbcTemple jdbcTemplate;
@Autowire
ItemPreparedStatementSetter itemPreparedStatementSetter;
@Override
public void write(final List<? extends Item> items) throws Exception {
for(Item item: items){
jdbcTemplate.execute(sql,new PreparedStatementCallBack<int []>{
@Override
public int[] doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
for (WarningObject warning: item.getWarnings) {
itemPreparedStatementSetter.setValues(warning, ps);
ps.addBatch();
}
return ps.executeBatch();
});
}
}
}
所以这工作正常,但这并没有真正成为批量插入警告
目前它会批量插入我的一个项目的所有警告。但理想情况下,我希望将每个项目的警告添加到preparedstatement,一旦添加了所有项目的所有警告,我想致电ps.executeBatch()
有人可以帮我解决这个问题吗?
【问题讨论】:
标签: spring spring-batch spring-jdbc jdbctemplate