如何获取Spring MVC中存储过程返回的结果集?

时间:2019-07-07 13:21:01

标签: java mysql spring-mvc stored-procedures spring-data-jpa

我正在尝试使用Spring Data JPA从存储过程中获取结果。 我不完全知道如何获取结果集。

如果将返回值设置为void,它将调用存储过程,并且控制台将显示以下内容:

[http-nio-8080-exec-3] DEBUG org.hibernate.loader.Loader  - Result set row: 0
[http-nio-8080-exec-3] DEBUG org.hibernate.loader.Loader  - Result row: 
[http-nio-8080-exec-3] DEBUG org.hibernate.loader.Loader  - Result set row: 1
[http-nio-8080-exec-3] DEBUG org.hibernate.loader.Loader  - Result row: 
[http-nio-8080-exec-3] DEBUG org.hibernate.loader.Loader  - Result set row: 2
[http-nio-8080-exec-3] DEBUG org.hibernate.loader.Loader  - Result row: 

我需要结果行的列表。这是我的代码如下:

Customer.java

@Entity
@NamedStoredProcedureQueries({
@NamedStoredProcedureQuery(name = "same_address", procedureName = "same_address")
})
public class Customer implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;
    private String address;

    //default empty constructor required by hibernate
    public Customer() {

    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Customer [id=" + id + ", name=" + name + ", email=" + email + ", address=" + address + "]";
    }

}

CustomerRepository.java

public interface CustomerRepository extends JpaRepository<Customer,Long>{   
    @Procedure(name = "same_address")
    void sameAddress();
}

CustomerService.java

@Service
public class CustomerService {

@Autowired
private CustomerRepository repo;

public void callProcedure() {
    System.out.println(1);
    repo.sameAddress();
    System.out.println(2);
}
}

MySQL过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `same_address`()
BEGIN
select * from customer WHERE Address = "Karachi";
END

0 个答案:

没有答案