英文:
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 = "up_Getdata(?)"
public List<String> fetchResult(@Param("inParam") 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如下:
- 
创建带有这3个字段的实体类。
 - 
将
fetchResult()的返回类型更改为您新创建的实体类。 - 
将
addRepository从class更改为interface。 - 
确保您选择了正确的过程名称,您可以在以下几种方式之间进行选择:
@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:
- 
Create your entity class with the 3 fields.
 - 
Change the return type of
fetchResult()to your newly created entity. - 
Change
addRepositoryfrom aclassto aninterface - 
Be sure that you chose the correct procedure name, you can choose between one of the following ways
@Procedure("up_Getdata")@Procedure(procedureName = "up_Getdata")@Procedure(value = "up_Getdata")- I am not sure if 
@Procedure(name = "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<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();
	}
}
Your Mapper:
@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";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论