mysql 存储过程调用 mybatis/hibernate

来源:互联网 发布:怎么创造软件 编辑:程序博客网 时间:2024/06/05 15:54

创建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 args17      * @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>

<selectid="count"parameterType="emp"useCache="false"statementType="CALLABLE">

<![CDATA[

callfindEmpById(

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

]]>

</select>

</mapper>



相关命令:
call findEmpById()

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


          原创粉丝点击