Spring服务和存储库层约定 [英] Spring service and repository layer convention

查看:77
本文介绍了Spring服务和存储库层约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Spring,并对它的约定有些困惑.

I start working with Spring and have some confusions about its conventions.

  1. 将存储库放入Controller很好吗?
  2. 在Service类中,如果我想重用代码,是否可以注入其他Services其他存储库?
  3. 命名Service是最佳实践吗?Repository类是基于Entity名称的,即:用户-> UserRepository-> UserService?
  1. Is it fine to put Repositories in Controller?
  2. In a Service class, If I want to reuse code could I inject other Services and other Reposities?
  3. is it the best practice to name Service And Repository class is based on Entity name i.e: User -> UserRepository->UserService?

推荐答案

  1. 否,请勿在Controller中使用存储库.仅在服务中. 并且不要在控制器中使用实体.创建Dto(数据传输 对象)来自实体的对象,并在您的 前端

  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

  1. 是的,您可以在服务类中使用其他服务和存储库

  1. Yes you can use other services and respositories in your service class

是的.将接口命名为UserService和实现UserServiceImpl

Yes it is. Name the interfache UserService and the implementation UserServiceImpl

这篇关于Spring服务和存储库层约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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