DB2中简单存储过程 selectAllUsers
CREATE procedure selectAllUsers
DYNAMIC RESULT SETS 1
BEGIN
DECLARE temp_cursor1 CURSOR WITH RETURN TO CLIENT FOR
SELECT * FROM test;
OPEN temp_cursor1;
END;
映射文件中关于存储过程内容如下
<hibernate-mapping package="com.unmi.vo">
<class name="Test" table="TEST">
............
</class>
<sql-query callable="true" name="selectAllUsers">
<return alias="aa" class="Test">
<return-property name="oborqt" column="OBORQT"/>
<return-property name="moorqt" column="MOORQT"/>
<return-property name="roschn" column="ROSCHN"/>
<return-property name="plandate" column="PLANDATE"/>
</return>
{ ? = call selectAllUsers() }
</sql-query>
</hibernate-mapping>
{ ? = call selectAllUsers() } 也可以写成{ call selectAllUsers() },
如果有参数就写成
{ ? = call selectAllUsers(?,?,?) }
代码中对query设置相应位置上的值就OK
Java调用关键代码如下
Session session = HibernateUtil.currentSession();
Query query = session.getNamedQuery("selectAllUsers");
List list = query.list();
System.out.println(list);
要求你的存储过程必须能返回记录集,否则要出错
如果你的存储过程是完成非查询任务就应该在配置文件用以下三个标签
<sql-insert callable="true">{call createPerson (?, ?)}</sql-insert>
<sql-delete callable="true">{? = call deletePerson (?)}</sql-delete>
<sql-update callable="true">{? = call updatePerson (?, ?)}</sql-update>
有一点不好的地方就是调用存储过程获取记录集时,不能对Query使用
setFirstResult(int)和setMaxResults(int)方法来分页
要求你的存储过程必须能返回记录集,否则要出错
上面这句话是关键。
==========================================================================
hibernate调用mysql5.0存储过程
关键代码:
数据库存储过程
Create procedure SP_get_UserInfo()
as
begin
select *
from user
order by Id
end
hibernate相对应的配置文件User.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.test.hibernate.db.User" table="user"
catalog="user">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="500" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" length="10" />
</property>
<!--
<property name="born" type="java.util.Date">
<column name="born" length="0" />
</property>
-->
<property name="born" type="java.lang.String">
<column name="born" length="0" />
</property>
<property name="address" type="java.lang.String">
<column name="address" />
</property>
</class>
<!--调用存储过程就在这里配 -->
<sql-query name="getUser" callable="true">
<return alias="user" class="com.test.hibernate.db.User">
<return-property name="id" column="ID" />
<return-property name="name" column="name" />
<return-property name="sex" column="sex" />
<return-property name="born" column="born" />
<return-property name="address" column="address"/>
</return>
{ call SP_get_UserInfo() }
</sql-query>
</hibernate-mapping>
调用存储过程的Java代码:
Session querySession = HibernateSessionFactory.getSession();
querySession.clear();
List lst = uerySession.getNamedQuery("getUser").list();
for(int i=0;i<lst.size();i++)
{
usr=(User)lst.get(i);
System.out
.println(usr.getId() + " " + usr.getName() + " "
+ usr.getSex() + " " + usr.getBorn() + " "
+ usr.getAddress());
}
HibernateSessionFactory.closeSession();
一个简单的存储过程调用就这样完成了!