如何使用Spring将ref cursor作为输出参数调用存储过程?

时间:2017-03-27 11:22:13

标签: java spring oracle stored-procedures jdbctemplate

我有一个存储过程,其体如: -

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

每行结果等同于某个用户定义类的实例。

我怎样才能在Spring中调用它。我经历了很多google和stackoverflow但是找不到合适的答案。

任何人都可以请我解决这个问题。提前谢谢。

2 个答案:

答案 0 :(得分:3)

以下是根据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")

答案 1 :(得分:2)

请看一次。 列出userData;

    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");