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

发布于 2024-07-20 07:22:38 字数 138 浏览 3 评论 0原文

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

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

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

谢谢。

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

Can anybody suggest how can I achieve this.

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

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

画尸师 2024-07-27 07:22:38

假设您指的是 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
        }
    }
);

调用函数几乎相同:

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

Assuming you are referring to 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
        }
    }
);
爱格式化 2024-07-27 07:22:38

在 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;
    }
}

像这样调用它。

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

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

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

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.

廻憶裏菂餘溫 2024-07-27 07:22:38

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

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

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);
        }
}
自在安然 2024-07-27 07:22:38

使用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);

使用JdbcTemplate调用过程:

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

使用SimpleJdbcCall调用函数:

Map<String, Object> inParameters = new HashMap<>();
inParameters.put("arg1", 55); // arg1 value
inParameters.put("arg2", 20); // arg2 value
MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource(inParameters);
BigDecimal result = new SimpleJdbcCall(dataSource)
        .withCatalogName("MY_PACKAGE")
        .withSchemaName("MY_SCHEMA")
        .withFunctionName("MY_FUNCTION")
        .executeFunction(BigDecimal.class, mapSqlParameterSource);

使用SimpleJdbcCall调用过程:

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

Calling function using 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:

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

Calling function using SimpleJdbcCall:

Map<String, Object> inParameters = new HashMap<>();
inParameters.put("arg1", 55); // arg1 value
inParameters.put("arg2", 20); // arg2 value
MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource(inParameters);
BigDecimal result = new SimpleJdbcCall(dataSource)
        .withCatalogName("MY_PACKAGE")
        .withSchemaName("MY_SCHEMA")
        .withFunctionName("MY_FUNCTION")
        .executeFunction(BigDecimal.class, mapSqlParameterSource);

Calling procedure using SimpleJdbcCall:

new SimpleJdbcCall(dataSource)
        .withCatalogName("MY_PACKAGE")
        .withProcedureName("MY_PROCEDURE")
        .execute("arg1", arg2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文