mysql 存储过程调用 mybatis/hibernate

本文介绍如何使用Hibernate和MyBatis框架调用MySQL存储过程,并提供了具体的代码实现示例。

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

 

创建mysql存储过程:

 

1 CREATE PROCEDURE `findEmpById`(IN id INTEGER(11),OUT count INT)
2 begin 
3      select COUNT(*) INTO count from emp where empId=id;
4 end;

 

//in   输入

//out 输出

//into 昵称

hibernate调用方式

 

复制代码

 1 package com.test;
 2 
 3 import java.sql.CallableStatement;
 4 import java.sql.Connection;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 
 8 import org.hibernate.Session;
 9 import org.hibernate.SessionFactory;
10 import org.hibernate.cfg.Configuration;
11 
12 
13 public class 调用存储过程 {
14 
15     /**
16      * @param args
17      * @throws SQLException 
18      */
19     public static void main(String[] args) throws SQLException {
20         Configuration cfg = new Configuration().configure();
21         SessionFactory factory = cfg.buildSessionFactory();
22         Session session = factory.openSession();
23         Connection con = session.connection();
24         String sql = "{call findEmpById(?)}";
25         CallableStatement cs = con.prepareCall(sql);
26         cs.setObject(1, 2);
27         ResultSet rs = cs.executeQuery();
28         while(rs.next()){
29             int id = rs.getInt("empId");
30             String name = rs.getString("empName");
31             System.out.println(id+"\t"+name);
32         }
33     }
34 
35 }

 

 

 

Mybatis调用MySQL存储过程

 

<mapper>

<select id="count" parameterType="emp" useCache="false" statementType="CALLABLE">

<![CDATA[

call findEmpById(

#{deviceCount,mode=OUT,jdbcType=INTEGER});

]]>

</select>

 

</mapper>

 

 

相关命令:

call findEmpById()

 

drop procedure findEmpById//
show procedure status
show create procedure findEmpById

转载于:https://my.oschina.net/ruanjun/blog/1591229

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值