调用在Spring Boot应用中的存储过程,并将结果分配给RowMapper。

huangapple go评论100阅读模式
英文:

Invoking a Stored procedure in spring boot application and assign the result to RowMapper

问题

我有一个返回List<String>的存储过程,我需要调用它并将结果赋值给一个模型调用。

以下是我编写的Repository类

  1. @Repository
  2. public class addRepository {
  3. @Procedure(name = "up_Getdata(?)"
  4. public List<String> fetchResult(@Param("inParam") String inParam){
  5. }

我的模型类有3个字段,名称,年龄,部门名称

关于如何进行映射的任何帮助都将非常有用。

英文:

I have a stored procedure which returns a List<String>. I need to invoke it and assign the result to a model call.

Below is the Repository class i have written

  1. @Repository
  2. public class addRepository {
  3. @Procedure(name = &quot;up_Getdata(?)&quot;
  4. public List&lt;String&gt; fetchResult(@Param(&quot;inParam&quot;) String inParam){
  5. }

My model class has 3 fields name,age,departName

Any help on how to map this will he helpful

答案1

得分: 1

我认为最好的答案就是简单地按照这个Baeldung教程进行操作,tl;dr如下:

  1. 创建带有这3个字段的实体类。

  2. fetchResult()的返回类型更改为您新创建的实体类。

  3. addRepositoryclass更改为interface

  4. 确保您选择了正确的过程名称,您可以在以下几种方式之间进行选择:

    • @Procedure("up_Getdata")
    • @Procedure(procedureName = "up_Getdata")
    • @Procedure(value = "up_Getdata")
    • 我不确定@Procedure(name = "up_Getdata)是否有效。
英文:

I think the best answer would be to simply follow this tutorial on Baeldung, the tl;dr would be:

  1. Create your entity class with the 3 fields.

  2. Change the return type of fetchResult() to your newly created entity.

  3. Change addRepository from a class to an interface

  4. Be sure that you chose the correct procedure name, you can choose between one of the following ways

    • @Procedure(&quot;up_Getdata&quot;)
    • @Procedure(procedureName = &quot;up_Getdata&quot;)
    • @Procedure(value = &quot;up_Getdata&quot;)
    • I am not sure if @Procedure(name = &quot;up_Getdata) would be valid

答案2

得分: 0

另一种方法,不使用@Procedure,但在Spring Boot中同样有效:

  1. @Repository
  2. public class YourRepository {
  3. @PersistenceContext
  4. private EntityManager entityManager;
  5. public List<YourModelClass> executeStoredProcedure(String inParam) {
  6. StoredProcedureQuery query = entityManager.createStoredProcedureQuery(
  7. "up_Getdata", YourMapper.MAPPING_NAME);
  8. query.registerStoredProcedureParameter("@inParam", String.class, ParameterMode.IN);
  9. query.setParameter("@inParam", inParam);
  10. return query.getResultList();
  11. }
  12. }

你的映射器:

  1. @MappedSuperclass
  2. @SqlResultSetMapping(name = YourMapper.MAPPING_NAME, classes = {
  3. @ConstructorResult(targetClass = YourModelClass.class, columns = {
  4. @ColumnResult(name = "name"),
  5. @ColumnResult(name = "age"),
  6. @ColumnResult(name = "departName")
  7. })
  8. })
  9. public abstract class YourMapper {
  10. public static final String MAPPING_NAME = "MappingName";
  11. }
英文:

Another approach without @Procedure but works perfectly also in spring boot:

  1. @Repository
  2. public class YourRepository {
  3. @PersistenceContext
  4. private EntityManager entityManager;
  5. public List&lt;YourModelClass&gt; executeStoredProcedure(String inParam) {
  6. StoredProcedureQuery query = entityManager.createStoredProcedureQuery(
  7. &quot;up_Getdata&quot;, YourMapper.MAPPING_NAME);
  8. query.registerStoredProcedureParameter(&quot;@inParam&quot;, String.class, ParameterMode.IN);
  9. query.setParameter(&quot;@inParam&quot;, inParam);
  10. return query.getResultList();
  11. }
  12. }

Your Mapper:

  1. @MappedSuperclass
  2. @SqlResultSetMapping(name = YourMapper.MAPPING_NAME, classes = {
  3. @ConstructorResult(targetClass = YourModelClass.class, columns = { @ColumnResult(name = &quot;name&quot;),
  4. @ColumnResult(name = &quot;age&quot;), @ColumnResult(name = &quot;departName&quot;) }) })
  5. public abstract class YourMapper {
  6. public static final String MAPPING_NAME = &quot;MappingName&quot;;
  7. }

huangapple
  • 本文由 发表于 2020年8月17日 16:38:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63447398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定