存储过程的Mysql ResultsetMapping

时间:2017-07-10 11:24:44

标签: mysql sql hibernate jpa

我在spring mvc中使用了一个程序。我想为程序返回的结果做一个sqlresultsetmapping。该程序不会返回任何相关结果。我该如何为该过程执行sqlresultsetmapping。

用声明的类emp:

编写的Sql Mapping
@SqlResultSetMapping(name = "sampleemp", classes = {
 @ConstructorResult(targetClass = emp.class,
 columns = { @ColumnResult(name = "id"),
             @ColumnResult(name = "name"),
             @ColumnResult(name = "department"),
           })
 })

NamedStoredProcedureQuery写在同一个类emp:

@NamedStoredProcedureQuery(name = "employee", procedureName = "sample", parameters =
 { @StoredProcedureParameter(mode = ParameterMode.IN, name = "id", type = Integer.class),
   @StoredProcedureParameter(mode = ParameterMode.IN, name = "name", type = String.class),
   @StoredProcedureParameter(mode  = ParameterMode.IN, name= "department", type = String.class)
 },
 resultSetMappings = { "sampleemp" })

如何使用以下代码调用存储过程时从DAO层映射这些:

StoredProcedureQuery spQuery = (StoredProcedureQuery) entityManager.createStoredProcedureQuery("sample")
 .registerStoredProcedureParameter("id", Integer.class, ParameterMode.IN)
 .registerStoredProcedureParameter("name", String.class, ParameterMode.IN)
 .registerStoredProcedureParameter("department", String.class, ParameterMode.IN)
 .setParameter("id", id)
 .setParameter("name", name)
 .setParameter("department", dept);

spQuery.execute();

1 个答案:

答案 0 :(得分:0)

1)ConstructorResult表示您的构造函数具有相同的字段(订单项)

我更喜欢@EntityResult。这也需要一个构造函数,但没有参数。

2)我无法使其与createStoredProcedureQuery一起使用,所以我叫

final Query query1 = entityManager.createNativeQuery("call yourProcName(PARAMS)", "NAME_OF_SqlResultSetMapping");
 List<Object[]> results = query.getResultList();

https://www.baeldung.com/jpa-sql-resultset-mapping