新建存储过程和函数:
1,无返回值 test_proc_no_result:
create or replace procedure test_proc_no_result(in_name varchar2) is
begin
insert into test_table .....;
commit;
end test_proc_no_result;
2,有返回值 test_proc_has_result :
create or replace procedure test_proc_has_result(in_name in varchar2,out_name out varchar2) is
begin
out_name := 'hello ' || in_name;
end test_proc_has_result;
3,函数 test_func :
create or replace function test_func(in_name varchar2)
return varchar2 is
out_name varchar2(200) :='' ;
begin
out_name := 'hello ' || in_name;
return out_name;
end test_func;
调用代码如下:
@Autowired
private SessionFactory sessionFactory;
//调用无返回值存储过程
public void callProcNoResult(String name){
String sql="{call test_proc_no_result(?)}";
sessionFactory.getCurrentSession().createNativeQuery(sql).setParameter(1, name).executeUpdate();
}
//调用有返回值存储过程
public Object callProcHasResult(String name) throws SQLException{
ProcedureCall call=sessionFactory.getCurrentSession().createStoredProcedureCall("test_proc_has_result");
//注册输入参数和输出参数,可以使用参数下标或者参数名称
call.registerParameter(1, String.class, ParameterMode.IN).bindValue(name);
call.registerParameter(2, String.class, ParameterMode.OUT);
return call.getOutputs().getOutputParameterValue(2);
}
//调用函数
public Object callFunction(String name){
String sql="select test_func(?) from dual";
return sessionFactory.getCurrentSession().createNativeQuery(sql).setParameter(1, name).uniqueResult();
}