在 Spring 中实现 JpaRepostiory 时使用存储库注释 [英] Using repository annotation when implementing JpaRepostiory in Spring

查看:26
本文介绍了在 Spring 中实现 JpaRepostiory 时使用存储库注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否理解正确,所以想澄清一下.如果我想为我的实体创建一个存储库,例如:

I'm not sure if I understand it correctly so want to clarify. If I want to create a repository for my entity eg.:

public interface BookRepository extends JpaRepository<Book, Id> {}

我应该用@Repository 注释它吗?根据这个问题 @Repository 注释将异常从SQL 到持久性,但 JpaRepostiory 不是已经这样做了吗?最佳实践是什么 - 注释与否?

Should I annotate it with @Repository? According to this question @Repository annotation translates exceptions from SQL to persistence ones but doesn't JpaRepostiory already do that? What's the best practice - to annotate or not?

推荐答案

在使用 JpaRepository 时,您不需要使用 @Repository

While using JpaRepository you don't need to annotate the interface with @Repository

它只是一个接口,具体的实现是由 Spring 作为代理对象动态创建的,JDBC 异常在那里处理.

It is just an interface and the concrete implementation is created dynamically as a proxy object by Spring and the JDBC Exceptions are handled there.

创建Custom DAO时需要使用@Repository,以便spring创建bean并正确处理异常.

You need to use @Repository when you create a Custom DAO, so that spring creates a bean and handles the exception properly.

这篇关于在 Spring 中实现 JpaRepostiory 时使用存储库注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆