The difference and usage of Controller, Service, Dao/Mapper layer in Java

The difference and usage of Controller, Service, Dao/Mapper layer in Java

In Java development, a three-tier architecture (or MVC architecture) is usually used to divide the responsibilities and functions of the program, which are Controller layer, Service layer, and Dao/Mapper layer.

1. Service layer: business layer –> control business

The logical function design of the business module, like the DAO layer, first designs the interface, then creates the class to be implemented, and then configures the association of its implementation in the configuration file. Next, the interface can be called in the service layer to process the business logic application.

Benefits: Encapsulating the business logic of the Service layer is conducive to the independence and reusability of business logic.

2. Controller layer: control layer –> call business layer methods to control business logic

The controller layer mainly calls the interface in the Service layer to control the specific business process, and the configuration of the control layer is also performed in the configuration file.

The difference between Controller and Service is: Controller is responsible for the control of specific business module processes; Service layer is responsible for the logical application design of business modules

3. Dao/Mapper layer: Persistence layer, mainly interacting with the database

The Dao layer will first create a Dao interface, and then define the implementation class of the interface in the configuration file; then you can call the Dao interface in the module to process the data business, regardless of which specific implementation class the interface is Classes, the data source of the Dao layer and the parameters of the database connection are all configured in the configuration file.

The following is a more professional detailed explanation

Controller layer

The Controller layer is the control layer in the Java Web application. It is mainly responsible for receiving requests sent by the client, scheduling various methods of the Service layer, and returning the processing results to the client. At the same time, the Controller layer can also verify the request parameters to prevent the processing and submission of wrong data.

The Controller layer mainly uses Spring MVC-related annotations to map requests and process request results. For example, @RequestMappingannotations are used to configure mappings based on request URLs, @Controllerannotations are used to identify the class as a controller class, @ResponseBodyand annotations are used to serialize the returned results into JSON and other formats and return them to the client.

Service layer

The Service layer is the business logic layer in the Java Web application, which is mainly responsible for processing business logic, realizing data verification, transaction control, authority control, etc. The Service layer operates the database by calling the interface of the Dao/Mapper layer to achieve specific business goals.

In order to achieve the independence and reusability of business logic, the Service layer is usually developed in the form of interface + implementation class. The Service layer mainly uses @Autowiredannotations to inject instances of the Dao/Mapper layer, and uses @Transactionalannotations to define transactions.

Dao/Mapper layer

The Dao/Mapper layer is the data access layer in the Java Web application. It is mainly responsible for interacting with the database and performing operations such as data reading, writing, modification, and deletion. The Dao/Mapper layer usually uses open source frameworks such as Spring Data JPA and MyBatis for ORM operations.

In the Dao/Mapper layer, there is usually a Java interface and a corresponding XML file (or annotations for mapping), which are used to define SQL statements to complete data addition, deletion, modification, and query operations. The methods in the Dao/Mapper layer will be called by the Service layer, and the interaction with the database will be completed by calling SQL statements.

In short, the Controller, Service, and Dao/Mapper layers are commonly used three-tier architecture in Java Web applications, which are responsible for receiving requests, processing business logic, and interacting with databases. Reasonable division and organization of these three layers of code can simplify program development and maintenance, and improve code reusability and maintainability.

Guess you like

Origin blog.csdn.net/weixin_46310452/article/details/130877544