假设我有一个MySQL存储过程,其中包含以下选择:
select * from users;
如何在中使用@Procedure注释,JpaRepository以便获得结果?就像是:
@Procedure
JpaRepository
public UserRepository extenda JpaRepository<User, Long>{ @Procedure('get_users') List<User> getUsers(); }
假设这是您的过程:
CREATE OR REPLACE PROCEDURE get_users(data out SYS_REFCURSOR) AS BEGIN OPEN data FOR SELECT * FROM USERS; END;
您定义一个@NamedStoredProcedureQuery:
@NamedStoredProcedureQuery
@NamedStoredProcedureQuery( name="getUsers", procedureName="get_users", resultClass=User.class, parameters={ @StoredProcedureParameter(queryParameter="data", name="data", direction=Direction.OUT_CURSOR) } ) @Entity public class User { ... }
然后您可以像这样调用过程:
public UserRepository extends JpaRepository<User, Long>{ @Procedure('User.getUsers') List<User> getUsers(); }
您也可以使用EntityManager进行检查,如下所示:
Query query = entitymanager.createNamedQuery("getUsers"); List users = query.getResultList();