0

我有以下代码

@Bean
public JdbcBatchItemWriter<QuotationDto> writer1() {
    return new JdbcBatchItemWriterBuilder<QuotationDto>()
            .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
            .sql("INSERT INTO ...")
            .dataSource(dataSource)
            .build();
}

@Bean
public JdbcBatchItemWriter<QuotationDto> writer2() {
    return new JdbcBatchItemWriterBuilder<QuotationDto>()
            .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
            .sql("INSERT INTO ...")
            .dataSource(dataSource)
            .build();
}

@Bean
public CompositeItemWriter<QuotationDto> compositeItemWriter() {
    CompositeItemWriter writer = new CompositeItemWriter();
    writer.setDelegates(Arrays.asList(writer1(), writer2()));
    return writer;
}

@Bean
public Step step1() {
    return stepBuilderFactory.get("step1")
            .<QuotationDto, QuotationDto>chunk(5)
            .reader(reader())
            .processor(processor())
            .writer(compositeItemWriter())
            .stream(writer1())
            .stream(writer2())
            .build();
}

我在将 writer1 设置为流时遇到 IntelliJ 错误,因为 id 没有实现 ItemStream。

我究竟做错了什么?有没有人有解决方案?我没有找到太多关于基于 java 的复合编写器配置的信息。

4

1 回答 1

0

JdbcBatchItemWriter没有实现ItemStream,因此它不能在面向块的步骤中用作流。

如果要组合两个 JDBC 项目编写器,可以创建一个委托给JdbcTemplate. 这是一个简单的例子:

class MyItemWriter implements ItemWriter<QuotationDto> {

    private JdbcTemplate jdbcTemplate;

    public MyItemWriter(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public void write(List<? extends QuotationDto> items) throws Exception {
        for (QuotationDto dto : items) {
            // use jdbcTemplate to batch insert items.
            // can do multiple inserts here, they will be part of
            // the same transaction driven by Spring Batch
        }
    }
}
于 2019-09-17T13:09:24.867 回答