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
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.
- 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数据访问层
callselectByName()
the method to determine whether the user name exists, and then数据访问层
callinsert()
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.controller
orcom.itheima.web
- Business logic layer:
com.itheima.service
- Data Access Layer:
com.itheima.dao
orcom.itheima.mapper
Later we will learn some frameworks, different frameworks encapsulate different layers
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?
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.