Spring Batch CompositeItemWriter,在遍历每个委托编写器结束时执行Batch以提高性能
在SpringBatch中,我的CompositeItemWriter使用多个ItemWriter将记录插入到多个表中,但目前这些ItemWriter中的每一个都在批量插入,如下所示Spring Batch CompositeItemWriter,在遍历每个委托编写器结束时执行Batch以提高性能,spring,spring-batch,spring-jdbc,jdbctemplate,Spring,Spring Batch,Spring Jdbc,Jdbctemplate,在SpringBatch中,我的CompositeItemWriter使用多个ItemWriter将记录插入到多个表中,但目前这些ItemWriter中的每一个都在批量插入,如下所示 public class Item{ public List<WarningObject> warnings public List<ErrorObject> errors public Long Id; public String data1; public Strin
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
}
公共类项目{
公共列表警告
公开列表错误
公共长Id;
公共字符串数据1;
公共字符串数据2;
公共字符串数据3;
//接球手和接球手
}
我的写入程序配置
<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>
公共类WarningItemWriter实现ItemWriter{
@自动连线
字符串sql;
@自动连线
jdbc模板;
@自动连线
ItemPreparedStatementSetter ItemPreparedStatementSetter;
@凌驾
public void write(final List只需将外部循环移动到匿名类的内部:
jdbcTemplate.execute(sql, new PreparedStatementCallBack<int []> {
@Override
public int[] doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
for (Item item: items) {
for (WarningObject warning: item.getWarnings) {
itemPreparedStatementSetter.setValues(warning, ps);
ps.addBatch();
}
}
return ps.executeBatch();
}
});
jdbcTemplate.execute(sql,new PreparedStatementCallBack{
@凌驾
public int[]doInPreparedStatement(PreparedStatement ps)抛出SQLException、DataAccessException{
用于(项目:项目){
用于(警告对象警告:item.getWarnings){
itemPreparedStatementSetter.setValues(警告,ps);
ps.addBatch();
}
}
返回ps.executeBatch();
}
});
jdbcTemplate.execute(sql, new PreparedStatementCallBack<int []> {
@Override
public int[] doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
for (Item item: items) {
for (WarningObject warning: item.getWarnings) {
itemPreparedStatementSetter.setValues(warning, ps);
ps.addBatch();
}
}
return ps.executeBatch();
}
});