【问题标题】:spring batch calling stored procedurespring批量调用存储过程
【发布时间】:2018-07-20 03:04:24
【问题描述】:

我正在编写一个 Spring 批处理应用程序:

  1. 到read多个csv文件数据
  2. 对数据做一些process
  3. 然后write 到 sql server 数据库

我有一个存储过程,它接受输入数据、价格、营业结束日期和安全 ID。如果记录已经存在,则过程UPDATE,如果不存在则INSERT。

我想在我的insert() 方法中调用这个存储过程,见下文:

@Slf4j
public class PriceWriter implements ItemWriter<Price> {


    private final PriceDAO pd;

    public PriceDAO(PriceDAO pd) {
        this.pd = pd;
    }

    @Override
    public void write(List<? extends Price> items) throws Exception {
        log.info("Ready to write {} items " + items.size());
        pd.insert(items);
    }
}



@Repository
public class PriceDAOImpl implements PriceDAO {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void insert(List<? extends Price> prices) {
        prices.forEach(e -> {
            System.out.println("Calling Stored Procedure For Records:  " + e.toString());
        });
    }
}

我看到的示例都是INSERT 语句,并且没有遇到在ItemWriterwrite() 的上下文中调用存储过程的任何地方。

我应该如何在 insert() 方法中调用我的存储过程,或者我应该如何在 Spring 批处理应用程序中调用存储过程?有人可以演示如何并举例说明吗?

【问题讨论】:

    标签: java spring stored-procedures spring-batch spring-jdbc


    【解决方案1】:

    你可以做任何你喜欢调用存储过程来存储数据的事情。没有 ItemWriter 可以做到这一点,但您可以采用两种方法:

    • 在 writer 中直接调用您的 SP
    • 实现一个服务,为 SP 映射并调用它。

    【讨论】:

      猜你喜欢
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-07
      • 2011-05-02
      • 1970-01-01
      • 2011-10-14
      相关资源
      最近更新 更多