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

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

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

问题

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

以下是我编写的Repository类

@Repository
public class addRepository {

@Procedure(name = "up_Getdata(?)"
public List<String> fetchResult(@Param("inParam") String inParam){

}

我的模型类有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

@Repository
public class addRepository {

@Procedure(name = &quot;up_Getdata(?)&quot;
public List&lt;String&gt; fetchResult(@Param(&quot;inParam&quot;) String inParam){

}

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中同样有效:

@Repository
public class YourRepository {

    @PersistenceContext
    private EntityManager entityManager;

    public List<YourModelClass> executeStoredProcedure(String inParam) {
        StoredProcedureQuery query = entityManager.createStoredProcedureQuery(
            "up_Getdata", YourMapper.MAPPING_NAME);

        query.registerStoredProcedureParameter("@inParam", String.class, ParameterMode.IN);
        query.setParameter("@inParam", inParam);

        return query.getResultList();
    }
}

你的映射器:

@MappedSuperclass
@SqlResultSetMapping(name = YourMapper.MAPPING_NAME, classes = {
    @ConstructorResult(targetClass = YourModelClass.class, columns = {
        @ColumnResult(name = "name"),
        @ColumnResult(name = "age"),
        @ColumnResult(name = "departName")
    })
})
public abstract class YourMapper {
    public static final String MAPPING_NAME = "MappingName";
}
英文:

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

    @Repository
    public class YourRepository {

	@PersistenceContext
	private EntityManager entityManager;

	public List&lt;YourModelClass&gt; executeStoredProcedure(String inParam) {
		StoredProcedureQuery query = entityManager.createStoredProcedureQuery(
				&quot;up_Getdata&quot;, YourMapper.MAPPING_NAME);

		query.registerStoredProcedureParameter(&quot;@inParam&quot;, String.class, ParameterMode.IN);
		query.setParameter(&quot;@inParam&quot;, inParam);

		return query.getResultList();
	}
}

Your Mapper:

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

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:

确定