简体   繁体   English

使用spring数据jpa执行具有多个参数的存储过程,并将ResultSet映射到非实体类

[英]Execute stored procedures with multiple parameters and map ResultSet to non-entity class using spring data jpa

We moved to spring data JPA recently (using Spring-boot 2.1.5-RELEASE) and in need to execute stored procedures with multiple input parameters and map to the non-entity POJO (I will be setting the value to the entity class while saving the objects). 我们最近迁移到了Spring数据JPA(使用Spring-boot 2.1.5-RELEASE),并且需要执行具有多个输入参数的存储过程并映射到非实体POJO(我将在保存时将其设置为实体类的值)对象)。 Can anyone please provide an example/description on how can it be achieved? 谁能提供一个示例/说明如何实现? I didn't get much out of documentation or may be I missed. 我没有太多的文档资料,或者可能会想念我。

Any help would be appreciated. 任何帮助,将不胜感激。

The most direct, and simplest solution is to user SimpleJDBCCall and forget about JPA for this data resource. 最直接,最简单的解决方案是使用用户SimpleJDBCCallSimpleJDBCCall为此数据资源使用JPA。 The documentation covers parameters extensively. 该文档广泛涵盖了参数。

Something like this will work: 这样的事情会起作用:

public class MyDao implements SomeDao {

    private JdbcTemplate jdbcTemplate;
    private SimpleJdbcCall procReadStuff;

    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
        this.procReadStuff = new SimpleJdbcCall(dataSource)
                .withProcedureName("my_procedure");
    }

    public void readStuff(Long id) {
        SqlParameterSource in = new MapSqlParameterSource()
                .addValue("in_id", id);
        Map out = procReadStuff.execute(in);
        Stuff stuff = new Stuff();
        stuff.setId(id);
        stuff.setSomeString((String) out.get("out_some_string"));

        // ...do something with Stuff POJO
    }

}

This sample was pulled/modified from the sample in the docs: Documentation 该示例是从docs中的示例中提取/修改的: 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM