hibernate5 调用 oracle 存储过程和函数

本文介绍了如何在Hibernate5中调用Oracle数据库的存储过程和函数,包括无返回值、有返回值的存储过程以及函数。通过示例代码展示了如何注册参数并执行这些数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新建存储过程和函数:
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();
}




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值