使用Spring Batch调用存储过程通过jdbc添加/更新记录

问题描述 投票:0回答:1

我有一个任务要调度并触发数据库调用,并且此数据库调用返回的数据必须批量发送以调用存储过程。如何使用带有 JDBC 模板的 Spring Batch 来实现此目的?

我已经阅读了一些关于如何实现此目的的教程,但没有一个专门针对使用 Spring Batch 并调用存储过程来更新数据。

spring-batch spring-jdbc
1个回答
0
投票

这是 Mkyoun 在 mkyong.com 网站上创建的示例

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter;

    public int[][] batchUpdate(List<Book> books, int batchSize) {

        int[][] updateCounts = jdbcTemplate.batchUpdate(
                "update books set price = ? where id = ?",
                books,
                batchSize,
                new ParameterizedPreparedStatementSetter<Book>() {
                    public void setValues(PreparedStatement ps, Book argument) 
                        throws SQLException {
                        ps.setBigDecimal(1, argument.getPrice());
                        ps.setLong(2, argument.getId());
                    }
                });
        return updateCounts;

    }
    
© www.soinside.com 2019 - 2024. All rights reserved.