test
CREATEprocedureselectAllUsersDYNAMICRESULTSETS1BEGINDECLAREtemp_cursor1CURSORWITHRETURNTOCLIENTFORSELECT*FROMtest;...
·
- 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,如
query.setInteger(1,100);
query.setString(2,"Unmi");
query.setDate(3,new Date());
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)方法来分页
转载于:https://blog.51cto.com/367837/84354
更多推荐
已为社区贡献249条内容
所有评论(0)