如何使用jdbcpagingitemreader返回列表

pieyvz9o  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(342)

我需要在我的项目中使用jdbcpagingitemreader,因为分页要求是强制性的
如何批量处理这些对象而不是逐个处理?
我希望读取器向处理器返回对象列表,而不是单个对象
我需要jdbcpagingitemreader将对象列表返回给处理器
我不知道如何让它工作,你有什么想法或例子吗?
下面是我的例子

@Bean
public Job batchUpdateInsiteGoodsJob(){
    return jobBuilderFactory.get("batchUpdateInsiteGoodsJob")
        .incrementer(new RunIdIncrementer())
        .listener(importGoodByExcelJobListener)
        .start(batchUpdateInsiteGoodsStep())
        .build();
}

@Bean
public Step batchUpdateInsiteGoodsStep() {
    return stepBuilderFactory.get("batchUpdateInsiteGoodsJob")
        .<Long,List<Goods>>chunk(10)
        .reader(batchUpdateInsiteGoodsReader())
        .processor(batchUpdateInsiteGoodsPorcessor)
        .writer(batchUpdateInsiteGoodsWriter)
        .faultTolerant().skip(Exception.class).skipLimit(100)
        .build();
}

@Bean
public JdbcPagingItemReader<Long> batchUpdateInsiteGoodsReader() {
    JdbcPagingItemReader<Long> reader = new JdbcPagingItemReader<>();
    reader.setDataSource(dataSource);
    reader.setFetchSize(100);

    reader.setRowMapper(new RowMapper<Long>() {
        @Override
        public Long mapRow(ResultSet rs, int i) throws SQLException {
            Console.log("get id"+rs.getLong("id"));//here is the question,how can I make it return a List not a singe
            return rs.getLong("id");
        }
    });
    MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
    provider.setSelectClause("id");
    provider.setFromClause("from tb_goods");
    provider.setWhereClause("status = 2 and is_remove = 0 and is_live = 1 and type = 1 and tb_commission_ration = 10");
    Map<String, Order> sort = new HashMap<>(1);
    sort.put("id", Order.ASCENDING);
    provider.setSortKeys(sort);
    reader.setQueryProvider(provider);
    return reader;

@Service
public class BatchUpdateInsiteGoodsPorcessor implements ItemProcessor<Long,List<Goods>> {
@Override
public List<Goods> process(Long aLong) throws Exception {
    Console.log("get a Long"+aLong);
    return null;
}

}
如何使读取器向itemprocessor返回列表

@Service
public class BatchUpdateInsiteGoodsPorcessor implements ItemProcessor<List<Long>,List<Goods>> {
@Override
public List<Goods> process(List<Long> aLong) throws Exception {
    Console.log("get a LongList"+aLong);
    return null;
}

}

yhived7q

yhived7q1#

如果您正在寻找示例,通常一个好的开始方法是阅读相关类的文档。在本例中,它将我指向docs.spring.io,这有助于对
官方指南附有示例。

相关问题