简体   繁体   English

Spring数据存储库方法实现

[英]Spring data repository methods implementation

I'm working in a personal web project and I would like to use Spring Data and JPA. 我正在一个个人Web项目中工作,我想使用Spring Data和JPA。 I found this nice blog entry in springsource.com and I've spent several hours trying to understand how this work and why they do it this way. 我在springsource.com上找到了这个不错的博客条目 ,并且花了几个小时试图了解它的工作原理以及为什么这样做。 I need help with two questions mainly: 我主要需要解决两个问题:

  1. Why is necesary to use Service and ServiceImpl classes. 为什么需要使用Service和ServiceImpl类。 That seems like a typical JPA 2 implementation but using Spring Data underneath with an Autowired repository. 这似乎是一个典型的JPA 2实现,但是在带有自动装配的存储库的下面使用Spring Data。 This really confuse me. 这真的使我感到困惑。 Why not use the repository directly? 为什么不直接使用存储库?
  2. The writer defines a pair of new methods in CustomerRepository but there isn't any implementation. 编写器在CustomerRepository中定义了一对新方法,但是没有任何实现。 He continues implementing CustomerService that it's not related with the repository. 他继续实现与存储库无关的CustomerService。 I've seen this in many sites but I don't understand how it works. 我已经在许多站点中看到了这一点,但我不知道它是如何工作的。

As I said I've been looking for a good article to find answers to my questions but most of them seem that are following these one. 正如我说的那样,我一直在寻找一篇不错的文章来找到我的问题的答案,但大多数问题似乎都在追随这些问题。 I've also checked github code of the article's project but it's uncomplete. 我还检查了该项目的github代码,但这还不完整。 I hope you can help me. 我希望你能帮助我。

The point of the blog post is to talk about spring-data repositories. 博客文章的重点是讨论Spring数据存储库。 So it doesn't elaborate on why services are necessary. 因此,它没有详细说明为什么需要服务。

They are because a typical business method will deal with several DAOs/Repositories, in a single transaction. 这是因为一种典型的业务方法将在一次交易中处理多个DAO /存储库。 For example, transferring money might include calling the AccountRepository, the AudiRepository and the CustomerRepository. 例如,转帐可能包括调用AccountRepository,AudiRepository和CustomerRepository。 The service's main goal is to provide transactional business methods to the presentation layer. 该服务的主要目标是向表示层提供事务性业务方法。

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

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