备忘:Hibernate5中调用存储过程

xiaoxiao2021-02-28  89

定义存储过程

说明:根据用户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(); // 创建callableStatement CallableStatement 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();
转载请注明原文地址: https://www.6miu.com/read-38241.html

最新回复(0)