hibernate 调用存储过程

来源:互联网 发布:上海大数据交易中心 编辑:程序博客网 时间:2024/06/04 01:04

笔者参考原文:http://www.iteye.com/topic/176032

使用hibernate 调用存储过程。

[java] view plaincopy
  1. public class StuInfo {  
  2.     private int id;  
  3.     private String stuName;  
  4.     private String stuNo;  
  5.     private int stuAge;  
  6.     private String stuId;  
  7.     private String stuSeat;  
  8.     private String stuAddress;  
[java] view plaincopy
  1. setters();getters();  
[java] view plaincopy
  1. }  

对应的数据库表:

[sql] view plaincopy
  1. if exists(select * from sysobjects where name='stuInfo')  
  2. drop table stuInfo  
  3. create table stuInfo /*创建学员信息表**/  
  4. (  
  5.  stuName varchar(20) not null,-- 姓名,非空  
  6.  stuNo char(6) not null,-- 学号,非空  
  7.  stuAge int not null,-- 年齡,int 默认为4个长度  
  8.  stuId numeric(18,0),  
  9.  stuSeat smallint ,-- 坐位车,使用自增  
  10.  stuAddress text -- 住址 可以为空  
  11. )  
  12. -- 给stuInfo添加一列  
  13. alter table stuInfo add id int identity(1,1) primary key;  

创建存储过程:

[sql] view plaincopy
  1. -- 存储过程  
  2. if exists(select name from sysobjects where name='proc_stuInfo' and type='p')  
  3. drop proc proc_stuInfo  
  4. go  
  5. create proc proc_stuInfo  
  6. as  
  7.  select * from stuInfo  
  8. go  
  9. -- 调用存储过程  
  10. exec proc_stuInfo;  

在hibernate 中调用存储过程的几种方法。

第一种:命名查询

[html] view plaincopy
  1. <sql-query name="getStuInfo" callable="true">    
  2.    <return alias="stuInfo" class="com.hkrt.domain.StuInfo">    
  3.       <return-property name="id" column="id" />    
  4. <return-property name="stuName" column="stuName" />  
  5. <return-property name="stuAge" column="stuAge" />  
  6. <return-property name="stuNo" column="stuNo"/>  
  7. <return-property name="stuSeat" column="stuSeat" />  
  8. <return-property name="stuAddress" column="stuAddress"/>  
  9. <return-property name="stuId" column="stuId"/>  
  10.    </return>    
  11.    {call proc_stuInfo()}    
  12. </sql-query>   

[java] view plaincopy
  1. List li=session.getNamedQuery("getStuInfo").list();    
  2.     System.out.println(li.get(0));  
第二种:类型于jdbc

[java] view plaincopy
  1. System.out.println("jdbc 调用-------------");  
  2.     Connection conn = session.connection();     
  3.     ResultSet rs =null;    
  4.     CallableStatement call;  
  5.     try {  
  6.         call = conn.prepareCall("{Call proc_stuInfo()}");  
  7.         rs = call.executeQuery();   
  8.         while(rs.next()){  
  9.             System.out.println(rs.getString(1));  
  10.             System.out.println(rs.getString(2));  
  11.             System.out.println(rs.getString(3));  
  12.             System.out.println(rs.getString(4));  
  13.             System.out.println(rs.getString(5));  
  14.             System.out.println(rs.getString(6));  
  15.             System.out.println(rs.getString(7));  
  16.             System.out.println("------------------");  
  17.         }  
  18.     } catch (SQLException e) {  
  19.         e.printStackTrace();  
  20.     }    


第三种:最简单的一种

[java] view plaincopy
  1. SQLQuery query =  session.createSQLQuery("{call proc_stuInfo()}").addEntity(StuInfo.class);    
  2. List list =query.list();   
  3. System.out.println(list.get(0));  

注:在第三种调用时,一定要加上addEntity();否则没有数据返回。

hibenate 调用带参的存储程

[sql] view plaincopy
  1. -- 带参数据的存储过程  
  2. if exists(select name from sysobjects where name='proc_find_stu' and type='p')  
  3. drop proc  proc_find_stu  
  4. go  
  5. create proc  proc_find_stu(@startId int,@endId int)  
  6. as  
  7.  select * from stuInfo where id between @startId and @endId;  
  8. go  
  9. exec proc_find_stu 1,4;  

[java] view plaincopy
  1. /**hibernate 调用带参的存储过程*/  
  2. @SuppressWarnings("unchecked")  
  3. @Test  
  4. public void msTest2(){  
  5.     SessionFactory sf = SessionFactoyUtil.getSessionFactory();  
  6.     Session session = sf.openSession();  
  7.     SQLQuery query =   session.createSQLQuery("{CALL proc_find_stu(?,?)}").addEntity(StuInfo.class);  
  8.     query.setLong(02);    
  9.     query.setLong(14);    
  10.     List<StuInfo> list =query.list();   
  11.      for(int i =0;i<list.size();i++){  
  12.          System.out.println(list.get(i));  
  13.      }  
  14. }  


结果信息:
[java] view plaincopy
  1. Hibernate:   
  2.     {CALL proc_find_stu(?,?)}  
  3. StuInfo [id=2, stuAddress=北京, stuAge=12, stuId=123456789012345687, stuName=李四, stuNo=112345, stuSeat=2]  
  4. StuInfo [id=4, stuAddress=北京, stuAge=12, stuId=123456789012345688, stuName=王五, stuNo=112345, stuSeat=3]  


0 0
原创粉丝点击