问题
I start working with Spring and have some confusions about its conventions.
- Is it fine to put Repositories in Controller?
- In a Service class, If I want to reuse code could I inject other
Services
and other Reposities? - is it the best practice to name
Service
AndRepository
class is based onEntity
name i.e: User ->UserRepository
->UserService
?
回答1:
No, don't use Repositories in the Controller. Only in the Services. And don't use Entities in your Controller. Create Dto (Data Transfer Objects) Object from the Entities and work with this in your frontend
Yes you can use other services and respositories in your service class
Yes it is. Name the interfache UserService and the implementation UserServiceImpl
回答2:
Check also answer:
It explains the Persistence Bussines and Presentation layers
https://stackoverflow.com/a/34084807/2218992
来源:https://stackoverflow.com/questions/34058238/spring-service-and-repository-layer-convention