本文介绍了从Spring Batch中使用IN和OUT参数调用StoreProcedure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Spring Batch执行一个StoredProcedure,该存储过程具有两个参数,一个IN参数和OUT参数.我想要的是在调用存储过程时获取结果集和out参数.

I am attempting to execute a StoredProcedure from Spring Batch, the stored procedure has two parameters, an IN parameter and OUT parameter. What I want is to get the result set and the out parameter when the stored procedure is called.

我引用了 StoredProcedureItemReader

StoredProcedureItemReaderBuilder

我可以使用它来调用仅具有IN参数的存储过程,但是,在注册OUT参数之后我将无法调用.

I can use this to call a stored procedure that has only IN parameter, however, I can't call after registering OUT parameter.

如果我们引用可以使用的原始JDBC模板,则可以使用 https://docs.oracle.com/javase/7/docs/api/java/sql/CallableStatement.html 而且我相信StoredProcedureItemReader或StoredProcedureIteamReaderBuilder在幕后使用CallableStatement.

If we refer raw JDBC template we could use, it is possible to call a Store Procedure with IN and OUT variables using https://docs.oracle.com/javase/7/docs/api/java/sql/CallableStatement.htmlAnd I believe that StoredProcedureItemReader or StoredProcedureIteamReaderBuilder uses CallableStatement behind the scenes.

我的问题是,如何使用 StoredProcedureItemReaderBuilder

My question is, how do I register OUT parameters to execute within Spring Batch using StoredProcedureItemReaderBuilder

这是我尝试过的示例代码

Here's a sample code I tried

@StepScope
    @Bean
    public StoredProcedureItemReader<MyRow> rowReader(@Value("#{stepExecutionContext[tableName]}") String tableName) {
        return new StoredProcedureItemReaderBuilder<MyRow>()
                .procedureName("GetNameCountByFname")
                .parameters(
                        new SqlParameter[]{
                                new SqlParameter("fname", Types.VARCHAR),
                                new SqlOutParameter("total", Types.INTEGER)
                        }).
                        preparedStatementSetter(
                                new PreparedStatementSetter() {
                                    @Override
                                    public void setValues(PreparedStatement ps)
                                            throws SQLException {
                                        ps.setString(1, "bob");
                                    }
                                }
                .rowMapper(new MyRowMapper(tableName))
                .name(tableName + "_read")
                .dataSource(dataSource)
                .build();
    }

给出以下错误:

Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.elementData(ArrayList.java:422) ~[na:1.8.0_251]
    at java.util.ArrayList.get(ArrayList.java:435) ~[na:1.8.0_251]
    at com.mysql.cj.jdbc.CallableStatement$CallableStatementParamInfo.getParameter(CallableStatement.java:283) ~[mysql-connector-java-8.0.20.jar:8.0.20]
    at com.mysql.cj.jdbc.CallableStatement.checkIsOutputParam(CallableStatement.java:634) ~[mysql-connector-java-8.0.20.jar:8.0.20]
    at com.mysql.cj.jdbc.CallableStatement.getObject(CallableStatement.java:1356) ~[mysql-connector-java-8.0.20.jar:8.0.20]

以下存储过程称为

DELIMITER $$

CREATE PROCEDURE GetNameCountByFname(
    IN  fname VARCHAR(25),
    OUT total INT
)
BEGIN
    SELECT COUNT(*)
    INTO total
    FROM `first`
    WHERE `name` = fname;
END$$

DELIMITER ;

推荐答案

那是不可能的.已请求此功能,但已被拒绝.请在> https://github.com/spring-projects/spring中找到更多详细信息-batch/issues/2024 .

That's not possible. This feature has been already requested but was rejected. Please find more details in https://github.com/spring-projects/spring-batch/issues/2024.

这篇关于从Spring Batch中使用IN和OUT参数调用StoreProcedure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:23