【问题标题】:How to call a stored procedure with ref cursor as an output parameter using Spring?如何使用 Spring 调用以 ref 游标作为输出参数的存储过程?
【发布时间】:2017-03-27 11:22:12
【问题描述】:

我有一个存储过程,它的主体如下:-

PROCEDURE PROC_NAME(param1 in varchar2,param2 in varchar2,results_cursor OUT CURSOR_TYPE);

每一行结果都相当于某个用户定义类的一个实例。

我如何在 Spring 中调用它。我浏览了很多 google 和 stackoverflow,但找不到合适的答案。

谁能给我一个解决方案。提前致谢。

【问题讨论】:

  • 我不确定如何使用 JdbcTemplate 来完成,但我已经使用普通的 JDBC 完成了。如果您可以使用 JDBC,我可以发布一些代码。
  • @dsp_user:我不能在我的代码中使用 JDBC。
  • 好吧,JDBCTemplate 已经在内部使用了它,但这完全取决于你。

标签: java spring oracle stored-procedures jdbctemplate


【解决方案1】:

这是我根据this StackOverflow questionthe Spring documentation 整理的:

import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import oracle.jdbc.OracleTypes;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;

public class SampleStoredProcedure extends StoredProcedure {

    public SampleStoredProcedure(DataSource dataSource) {
        super(dataSource, "PROC_NAME");
        declareParameter(new SqlParameter("param1", Types.VARCHAR));
        declareParameter(new SqlParameter("param2", Types.VARCHAR));
        declareParameter(new SqlOutParameter("results_cursor", OracleTypes.CURSOR, new SomeRowMapper()));
        compile();
    }

    public Map<String, Object> execute(String param1, String param2) {
        Map<String, Object> inParams = new HashMap<>();
        inParams.put("param1", param1);
        inParams.put("param2", param2);
        Map output = execute(inParams);
        return output;
    }
}

如果您的存储过程在另一个架构或包中,则需要在上面调整存储过程名称。此外,您需要指定一个行映射器来代替SomeRowMapper

称呼它:

    DataSource dataSource = ... ; // get this from somewhere
    SampleStoredProcedure sp = new SampleStoredProcedure(dataSource);
    Map<String, Object> result = sp.execute("some string", "some other string");
    // Do something with 'result': in particular, result.get("results_cursor")
    // will be the list of objects returned

或者,您可以使用SimpleJdbcCall

    DataSource dataSource = ... ; // get this from somewhere
    SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource);
    Map<String, Object> result =
        jdbcCall.withProcedureName("PROC_NAME")
            .declareParameters(
                    new SqlParameter("param1", Types.VARCHAR),
                    new SqlParameter("param2", Types.VARCHAR),
                    new SqlOutParameter("results_cursor", OracleTypes.CURSOR, new SomeRowMapper()))
            .execute("some string", "some other string");

如果存储过程在包中,则需要添加一行

            .withCatalogName("PACKAGE_NAME")

jdbcCall的设置。同样,如果它在不同的架构中,则需要添加

            .withSchemaName("SCHEMA_NAME")

【讨论】:

  • 你能分享一下 SomeRowMapper 的示例实现吗
【解决方案2】:

请看一次。 列出用户数据;

    SimpleJdbcCall  executor = new SimpleJdbcCall(jdbcTemplate)
                                .withProcedureName("SP_CL_USERPKS_FOLDER").withoutProcedureColumnMetaDataAccess()
                                .declareParameters(
                                    new SqlParameter("INparam1", Types.INTEGER),
                                    new SqlParameter("INparam2", Types.VARCHAR),
                                    new SqlOutParameter("OUTParam1", OracleTypes.CURSOR),
                                    new SqlOutParameter("OUTParam2", OracleTypes.VARCHAR));
    executor.compile();
    SqlParameterSource param = new MapSqlParameterSource()
            .addValue("INparam1", loginPk)
            .addValue("INparam2", email);

    Map map = executor.execute(param);
    userData = (List<Map>) map.get("OUTParam1");
    String msg = (String) map.get("OUTParam2");

【讨论】:

    猜你喜欢
    • 2015-01-24
    • 1970-01-01
    • 2019-03-18
    • 2017-07-19
    • 2018-01-07
    • 2012-05-10
    • 1970-01-01
    • 2014-10-01
    • 2020-12-12
    相关资源
    最近更新 更多