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>