备忘:Hibernate5中调用存储过程

来源:互联网 发布:餐厅收银软件 免费 编辑:程序博客网 时间:2024/06/15 12:00

定义存储过程

说明:根据用户id,计算用户的账户总金额(totalMoney)和消费金额(usedMoney)。

CREATE PROCEDURE `calcMemberMoney`(IN userId VARCHAR(32),OUT totalMoney BIGINT,OUT usedMoney BIGINT)BEGIN......END$$

Java调用

String userId = "123456";Long totalMoney = 0L;Long usedMoney = 0L;// 取得数据库链接Connection conn = SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection();// 创建callableStatementCallableStatement call = conn.prepareCall("{Call calcMemberMoney(?,?,?)}");// 设置入参,参数index从1开始call.setString(1, userId);// 注册出参的类型call.registerOutParameter(2, java.sql.Types.BIGINT);call.registerOutParameter(3, java.sql.Types.BIGINT);// 执行call.execute();//取得出参的值,注意参数index从1开始,第23的值是出参totalMoney = call.getLong(2);usedMoney =  call.getLong(3);// 关闭连接call.close();conn.close();