简体   繁体   English

Java 中的服务层或仅存储库

[英]Service layer in Java or Only Repository

If i have Spring data repository which includes save, delete and modify operations.如果我有包含保存、删除和修改操作的 Spring 数据存储库。 Should I create the following methods operations in service layer?我应该在服务层创建以下方法操作吗? And adding annotation @Transactional ?并添加注释@Transactional Or leave them as is?还是让他们保持原样? Or should I have them in repository layer?或者我应该将它们放在存储库层中吗?

Example service示例服务

@Service
public class RepositoryOperations{

@Autowired
ProductRepository productRepository;

@Transactional
public void saveProduct(){
    productRepository.save();
    }

...

}

I think that you are using a good approach for your code.我认为您正在为您的代码使用一种好的方法。 You can create a service layer that will invoke the product repository methods in order to separate responsibilities (business from data logic).您可以创建一个服务层,它将调用产品存储库方法以分离职责(业务与数据逻辑)。

A 3-layered architecture describes your approach as follows: 3 层架构描述您的方法如下:

  • Controllers: Handle all the input values that enter to the system (from endpoins, for example) and passes them to the service.控制器:处理所有进入系统的输入值(例如,从端点)并将它们传递给服务。 In Spring you can use the @Controller class annotation in order to differentiate them from other components.在 Spring 中,您可以使用 @Controller 类注释来将它们与其他组件区分开来。
  • Services: Components that handle all the business logic related to your application.服务:处理与您的应用程序相关的所有业务逻辑的组件。 You can use the @Service annotation.您可以使用@Service 注释。
  • DAO (Data Access Objects): Classes that contains the data logic (in your case, the repositories). DAO(数据访问对象):包含数据逻辑的类(在您的例子中是存储库)。 You can use the @Repository annotation.您可以使用@Repository 注释。

In order to prevent any inconsistent in your saveProduct() service method when a problem occurs, you can use the @Transactional annotation in order to rollback all the changes made by your method in the database, so i consider it's a good practice use that annotation when you are working with writing operations in database.为了防止出现问题时您的 saveProduct() 服务方法不一致,您可以使用 @Transactional 注释来回滚您的方法在数据库中所做的所有更改,所以我认为使用该注释是一个很好的做法当您在数据库中进行写入操作时。

We keep three layers generally.我们一般保留三层。

  1. The controller which defines API endpoints and calls appropriate service to process the request.定义 API 端点并调用适当服务来处理请求的控制器。
  2. The service layer contains business logic and is meant to do heavy processing as we call it.服务层包含业务逻辑,我们称之为繁重的处理。 If service layer needs to save or retrieve data from the database, it will use the dao layer to achieve this.如果服务层需要保存或从数据库中检索数据,它将使用 dao 层来实现。
  3. Dao layer's only role is to save or retrieve data from the database and provide it back to the service layer. Dao 层的唯一作用是从数据库中保存或检索数据,并将其提供回服务层。 It should not contain any business logic.它不应包含任何业务逻辑。

Annotate your dao layer with annotation @Repository使用注释@Repository注释您的 dao 层
Annotate your service layer with annotation @Service使用注释@Service注释您的服务层
Annotate your controllers with annotation @Controller or @RestController(in case you have REST APIs)使用注释@Controller或 @RestController 注释您的控制器(如果您有 REST API)

@Transactional annotation is used when you want a code to be rolled back if it fails. @Transactional注解在你希望代码失败时回滚时使用。 If your service places three dao calls for three different operations and you want all or none of those three operations to be performed, annotate your service method with @Transactional如果您的服务为三个不同的操作放置了三个 dao 调用,并且您希望执行这三个操作中的全部或不执行,请使用@Transactional注释您的服务方法
Same applies for dao methods.同样适用于 dao 方法。
So you have now fair amount of information to decide upon.所以你现在有相当多的信息可以决定。

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

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