gpt4 book ai didi

spring - 使用 Spring @Procedure 调用 StoredProcedure 而不绑定(bind)到表

转载 作者:行者123 更新时间:2023-12-03 01:35:48 27 4
gpt4 key购买 nike

我想知道是否可以调用存储过程而不必将其绑定(bind)到表/模型?

就我而言,我的数据库中有一个发送邮件的存储过程。我想从 Spring 调用它:

public interface SendEmail extends org.springframework.data.repository.Repository<Email, Long> {
@Procedure(procedureName = "send_email")
void sendEmail(String sender, String recipient, String ccRecipient, String subject, String message);
}

上面的代码编译良好并且运行良好 - 没有问题。唯一的问题是我想摆脱 extends Repository<Email, Long> - 我该怎么做呢?如果我只是删除 <Email, Long> (因为 StoredProcedure 不返回任何内容,因此不需要类型):

public interface SendEmail extends org.springframework.data.repository.Repository {
@Procedure(procedureName = "send_email")
void sendEmail(String sender, String recipient, String ccRecipient, String subject, String message);
}

然后我收到以下错误:

Caused by: java.lang.IllegalArgumentException: Could not resolve id type of interface x.x.x.x.SendEmail!
at org.springframework.data.repository.core.support.DefaultRepositoryMetadata.resolveIdType(DefaultRepositoryMetadata.java:81) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.DefaultRepositoryMetadata.<init>(DefaultRepositoryMetadata.java:52) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.AbstractRepositoryMetadata.getMetadata(AbstractRepositoryMetadata.java:71) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepositoryMetadata(RepositoryFactorySupport.java:233) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:260) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
... 41 common frames omitted

如果我删除extends Repository<Email, Long>并有:

public interface SendEmail {
@Procedure(procedureName = "send_email")
void sendEmail(String sender, String recipient, String ccRecipient, String subject, String message);
}

然后我收到以下错误:

Description:

Field sendEmail in x.x.x.EmailService required a bean of type 'x.x.x.SendEmail' that could not be found.


Action:

Consider defining a bean of type 'x.x.x.SendEmail' in your configuration.

作为结论,我需要一个名为 Email 的模型映射到数据库中的表以便上面的工作。为什么会这样?

(我尝试用 @Repositories@Component 注释界面)

最佳答案

存储库基于存储库的域驱动设计概念:行为类似于聚合根集合的对象,但内容存储在某个持久存储中。因此,Repository 需要知道它负责的聚合根以及它的 id 类型,以便在存储中找到它。

对于 JPA 存储库,这意味着聚合必须是由 JPA 映射的实体。当然可以实现基于 POJO 实体并由存储过程支持的存储库。这个想法没有什么问题,只是它不是一个常见的用例,并且对于您的用例来说可能有点过分了。

如果您只想调用存储过程,您可能最好使用简单的 Spring bean 和 JdbcTemplate

关于spring - 使用 Spring @Procedure 调用 StoredProcedure 而不绑定(bind)到表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53228592/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com