如何使用 Spring 持久化框架调用 Oracle 函数或存储过程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/862694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:51:59  来源:igfitidea点击:

How to call Oracle function or stored procedure using spring persistence framework?

springoraclejunit

提问by non sequitor

I am using Spring persistence framework for my project. I want to call oracle function or stored procedure from this framework.

我正在为我的项目使用 Spring 持久性框架。我想从这个框架调用 oracle 函数或存储过程。

Can anybody suggest how can I achieve this.

任何人都可以建议我如何实现这一目标。

Please give solution for both * oracle function and *stored procedure.

请给出* oracle函数和*存储过程的解决方案。

Thanks.

谢谢。

回答by Adam Paynter

Assuming you are referring to JdbcTemplate:

假设您指的是 JdbcTemplate:

jdbcTemplate.execute(
    new CallableStatementCreator() {
        public CallableStatement createCallableStatement(Connection con) throws SQLException{
            CallableStatement cs = con.prepareCall("{call MY_STORED_PROCEDURE(?, ?, ?)}");
            cs.setInt(1, ...); // first argument
            cs.setInt(2, ...); // second argument
            cs.setInt(3, ...); // third argument
            return cs;
        }
    },
    new CallableStatementCallback() {
        public Object doInCallableStatement(CallableStatement cs) throws SQLException{
            cs.execute();
            return null; // Whatever is returned here is returned from the jdbcTemplate.execute method
        }
    }
);

Calling a function is almost identical:

调用函数几乎相同:

jdbcTemplate.execute(
    new CallableStatementCreator() {
        public CallableStatement createCallableStatement(Connection con) {
            CallableStatement cs = con.prepareCall("{? = call MY_FUNCTION(?, ?, ?)}");
            cs.registerOutParameter(1, Types.INTEGER); // or whatever type your function returns.
            // Set your arguments
            cs.setInt(2, ...); // first argument
            cs.setInt(3, ...); // second argument
            cs.setInt(4, ...); // third argument
            return cs;
        }
    },
    new CallableStatementCallback {
        public Object doInCallableStatement(CallableStatement cs) {
            cs.execute();
            int result = cs.getInt(1);
            return result; // Whatever is returned here is returned from the jdbcTemplate.execute method
        }
    }
);

回答by non sequitor

Simpler way of calling a Oracle function in Spring is subclassing StoredProcedure like below

在 Spring 中调用 Oracle 函数的更简单方法是将 StoredProcedure 子类化,如下所示

public class MyStoredProcedure extends StoredProcedure{
    private static final String SQL = "package.function";

    public MyStoredProcedure(DataSource ds){
        super(ds,SQL);
        declareParameter(new SqlOutParameter("param_out",Types.NUMERIC));
        declareParameter(new SqlParameter("param_in",Types.NUMERIC));
        setFunction(true);//you must set this as it distinguishes it from a sproc
        compile();
    }

    public String execute(Long rdsId){
        Map in = new HashMap();
        in.put("param_in",rdsId);
        Map out = execute(in);
        if(!out.isEmpty())
            return out.get("param_out").toString();
        else
            return null;
    }
}

And call it like this

并这样称呼它

@Autowired DataSource ds;
MyStoredProcedure sp = new MyStoredProcedure(ds);
String i = sp.execute(1l);

The Oracle function used here just takes in a numeric parameter and returns a numeric paramter.

这里使用的 Oracle 函数只接受一个数字参数并返回一个数字参数。

回答by Deniss M.

In my opinion this is one of the easiest approaches:

在我看来,这是最简单的方法之一:

public class ServRepository {

    private JdbcTemplate jdbcTemplate;
    private SimpleJdbcCall functionGetServerErrors;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.setResultsMapCaseInsensitive(true);
        this.functionGetServerErrors = new SimpleJdbcCall(jdbcTemplate).withFunctionName("THIS_IS_YOUR_DB_FUNCTION_NAME").withSchemaName("OPTIONAL_SCHEMA_NAME");
    }

        public String callYourFunction(int parameterOne, int parameterTwo) {
            SqlParameterSource in = new MapSqlParameterSource().addValue("DB_FUNCTION_INCOMING_PARAMETER_ONE", parameterOne).addValue("DB_FUNCTION_INCOMING_PARAMETER_TWO", parameterTwo);
            return functionGetServerErrors.executeFunction(String.class, in);
        }
}

回答by Alex78191

Calling function using NamedParameterJdbcTemplate:

调用函数使用NamedParameterJdbcTemplate

final String query = "select MY_FUNCTION(:arg1, :arg2, :arg3) from dual";
Map<String, Object> argMap = new HashMap<>();
argMap.put("arg1", "value1");
argMap.put("arg2", 2);
argMap.put("arg3", "value3");
final String result = new NamedParameterJdbcTemplate(dataSource)
        .queryForObject(query, argMap, String.class);

Calling procedure using JdbcTemplate:

调用过程使用JdbcTemplate

final String query = "call MY_PROCEDURE(?, ?, ?)";
final Object[] args = {"arg1", "arg2", "arg3"};
new JdbcTemplate(dataSource).execute(query, args, String.class);

Calling function using SimpleJdbcCall:

调用函数使用SimpleJdbcCall

final String result = new SimpleJdbcCall(dataSource)
        .withCatalogName("MY_PACKAGE")
        .withFunctionName("MY_FUNCTION")
        .executeFunction(String.class, "arg1", "arg2");

Calling procedure using SimpleJdbcCall:

调用过程使用SimpleJdbcCall

new SimpleJdbcCall(dataSource)
        .withCatalogName("MY_PACKAGE")
        .withProcedureName("MY_PROCEDURE")
        .execute("arg1", arg2);