MVC pattern and three-tier architecture

MVC pattern and three-tier architecture

The MVC pattern and the three-tier architecture are some theoretical knowledge. In the future, we will use them for code development, which will make our code maintainability and scalability better.

MVC pattern

MVC is a pattern of layered development where:

  • M: Model, business model, processing business
  • V: View, view, interface display
  • C: Controller, controller, processing requests, calling models and views

insert image description here
The controller (serlvet) is used to receive the request sent by the browser, and the controller calls the model (javaBean) to obtain data, such as querying data from the database; after the controller obtains the data, it is handed over to the view (JSP) for data display.
MVC benefits:

  • Single responsibility. Each character does its own thing and performs its duties.

  • Conducive to division of labor and cooperation.

  • Facilitates component reuse

Three-tier architecture

The three-tier architecture divides our project into three levels, namely the data layer, business logic layer, and data access layer.
insert image description here

  • Data access layer: CRUD basic operations on the database
  • Business logic layer: Encapsulate the business logic, combine the basic functions in the data access layer to form complex business logic functions. For example 注册业务功能, we will first 数据访问层call selectByName()the method to determine whether the user name exists, and then 数据访问层call insert()the method to add data if it does not exist
  • Presentation layer: receive requests, encapsulate data, call business logic layer, and respond to data

The whole process is that the browser sends a request, the Servlet of the presentation layer receives the request and calls the method of the business logic layer for business logic processing, and the method of the business logic layer calls the method of the data access layer for data operation, returns to the servlet in turn, and then the servlet Hand over the data to JSP for display.

Each layer of the three-tier architecture has a unique package name:

  • presentation layer: com.itheima.controllerorcom.itheima.web
  • Business logic layer:com.itheima.service
  • Data Access Layer: com.itheima.daoorcom.itheima.mapper

Later we will learn some frameworks, different frameworks encapsulate different layers
insert image description here

MVC and three-tier architecture

Through the study of MVC and three-tier architecture, some people must be confused. So what is their difference and connection?
insert image description here
As shown in the upper part of the figure above is the MVC model, and the lower part of the figure above is the three-tier architecture. MVC 模式C (controller) and V (view) in are the presentation layer 三层架构in , and MVC 模式M (model) in is the business logic layer and data access layer 三层架构in .

It can be MVC 模式understood as a big concept, but the idea 三层架构of MVC 模式​​implementing architecture. Then we will write the codes of different layers under different packages according to the requirements in the future, and the functional responsibility of each layer should be single. If the technology of the presentation layer is replaced in the future, the codes of the business logic layer and data access layer do not need to be generated. Variety.


Guess you like

Origin blog.csdn.net/qq_53037676/article/details/126060936