ishowcode.eth

ishowcode.eth

区块链小白

In-depth Explanation of the Three-tier Architecture of Spring MVC

SpringMVC Three-Tier Architecture#

The engineering structure of Java SpringMVC is generally divided into three tiers, from bottom to top: Model layer (data access layer), Controller layer (logic control layer), and View layer (page display layer). The Model layer is further divided into two layers: dao layer and service layer. The main purpose of the layered architecture in MVC is decoupling. The benefits of using a layered architecture are generally accepted to be system maintenance and system scalability. It enhances the system's maintainability and extensibility.

For frameworks like Spring, the View/Web layer calls the Controller layer, the Controller layer calls the Service layer, and the Service layer calls the Data Access layer (Dao).
Insert Image Description Here
Service layer: The business layer is used to implement business logic. It can call the Dao layer or the Service layer and return data objects (DO) or business objects (BO). BO is usually derived from the conversion and integration of DO and can contain multiple properties of DO or only partial properties of a single DO. Usually, for simplicity, if no conversion is required, the Service layer can directly return DO as well. External calls (HTTP, RPC) methods are also in this layer. For external calls, the Service layer generally converts the DTO returned by the external call into BO. It focuses on business logic and uses Dao to implement database operations that are needed. It is responsible for tasks such as obtaining connections, closing database connections, and transaction rollback. Complex logical business processes are usually placed in the Service layer.

DAO layer: Responsible for accessing the database and performing data operations, retrieving result sets, and then encapsulating the data from the result set into VO class objects and returning them to the Service layer. It is the data layer that directly performs database read and write operations, returning data objects (DO) that correspond to database tables one-to-one. The role of Dao is to encapsulate access to the database: CRUD operations. It does not involve business logic, only meets the requirements of obtaining specified data based on certain conditions.

Controller layer: Also known as the control layer, its main function is to handle user requests. It primarily handles external requests. It calls the Service layer, converts the BO/DO returned by the Service layer into DTO/VO, and encapsulates them into a unified return object to be returned to the caller. If the returned data is used for frontend template rendering, it returns VO; otherwise, it generally returns DTO. Whether it is DTO or VO, there is usually some data conversion and integration from BO/DO.

View layer: Also known as the presentation layer, it is mainly responsible for displaying data.

In actual development, the dao layer needs to define its own operation standards, that is, standard interfaces, in order to achieve decoupling.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.