【问题标题】:Spring batch with spring data带有弹簧数据的弹簧批处理
【发布时间】:2017-03-24 15:20:25
【问题描述】:

有没有办法将 Spring Batch 与 Spring 数据集成?我在 spring 文档中看到 RepositoryItemReaderRepositoryItemWriter

【问题讨论】:

  • 就是这样。你还有什么要找的吗?
  • 好吧,我以不同的方式这样做,我将服务注入到作业配置中并调用 JpaRepository 上可用的方法,就像这个例子 @Bean @ StepScope public ItemWriter customerItemWriter2() { return items -> { for (Customer item : items) { customerService.save(item); } }; }
  • 当我们有 RepositoryItemWriter 给你时,你为什么要这样做?
  • 我今天偶然发现了它,使用它会更好,我要感谢你在 O'Reilly 上所做的所有课程,它真的帮助了我很多。
  • @MichaelMinella 你能回答我的其他问题吗谢谢stackoverflow.com/questions/43008684/…

标签: spring spring-data spring-batch


【解决方案1】:

你完全正确。 Spring Batch 可以很容易地与 Spring 数据集成。这里是项目阅读器的例子:

    @Bean(name = "lotteryInfoReader")
    @StepScope
    public RepositoryItemReader<LotteryInfo> reader() {
        RepositoryItemReader<LotteryInfo> reader = new RepositoryItemReader<>();
        reader.setRepository(lotteryInfoRepository);
        reader.setMethodName("findAll");
        reader.setSort(Collections.singletonMap("name", Sort.Direction.ASC));
        return reader;
    }

这是另一个在没有 spring 数据的情况下使用 hibernate 的示例:

    @Bean(name = "drawsWriter")
    @StepScope
    public ItemWriter<? super List<Draw>> writer() {
        return items -> items.stream()
                .flatMap(Collection::stream)
                .forEach(entityManager::merge);
    }

【讨论】:

    【解决方案2】:

    我正在以不同的方式这样做,我将服务注入到作业配置中并调用 JpaRepository 上可用的方法,就像这个例子

        @Bean
        @StepScope
        public ItemWriter<Customer> customerItemWriter2() {
            return items -> {
                for (Customer item : items) {
    
                    customerService.save(item);
                }
            };
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-03
      • 2017-12-21
      • 2015-02-08
      • 2019-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多