我想知道是否可以调用存储过程而不必将其绑定到表/模型?
就我而言,我在发送邮件的数据库中有一个存储过程。我想从 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
and注释界面@Component
)