How to call a stored procedure with ref cursor as an output parameter using Spring?

前端 未结 2 747
灰色年华
灰色年华 2021-01-07 07:08

I have a stored procedure which has body like :-

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

Each

相关标签:
2条回答
  • 2021-01-07 07:26

    Here's something I put together based on this StackOverflow question and the 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;
        }
    }
    

    If your stored procedure is in another schema or in a package, you'll need to adjust the stored procedure name in the above. Also, you'll need to specify a row mapper to use in place of SomeRowMapper.

    To call it:

        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
    

    Alternatively, you can use a 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");
    

    If the stored procedure is in a package, you'll need to add a line

                .withCatalogName("PACKAGE_NAME")
    

    to the setup of jdbcCall. Similarly, if it's in a different schema, you'll need to add

                .withSchemaName("SCHEMA_NAME")
    
    0 讨论(0)
  • 2021-01-07 07:39

    Please have look a look once. List 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");
    
    0 讨论(0)
提交回复
热议问题