当前位置:网站首页>Relationship among controller, service and Dao
Relationship among controller, service and Dao
2022-06-29 17:51:00 【Two rasps!】
List of articles
Preface
Tips : Here you can add the general content to be recorded in this article :
I mentioned the code writing process in the last article
1、 Draw page , To write A.jsp
2、 Write the customer request address , Initiate customer request , To write B.jsp
3、 Write control layer code (xxxController.java)
4、 Write business layer code (xxxService.java)
5、 Write data persistence layer code (xxxDao.java and xxxDao.xml)
————————————————————————————————————————
Today, let's talk about their relationship
Tips : The following is the main body of this article , The following cases can be used for reference
One 、model
model Layer is the database entity layer , Also known as entity layer ,pojo layer
Generally, a table in a database corresponds to an entity class , Class attributes are the same as table fields One A corresponding .

Generally, the entity layer will automatically generate Getter、Setter、to String Methods and construction methods
Eclipse Quickly add Getter、Setter Method

Eclipse Quickly add to String Method

Eclipse Quickly add construction methods in

idea Quickly add Getter、Setter Method
Here we borrow the classic dynamic pictures on the Internet :
idea Quickly add to String Method


idea Quickly add construction methods in
Here we borrow the classic dynamic pictures on the Internet :
Two 、Controller Business control layer
controller The function of the layer is request and response control .
controller The layer does not allow direct operation of the database !controller The layer is responsible for the interaction between the front end and the back end , He is a bridge , Set up the communication between the outside world and the business layer , Accept front-end requests , call Serice Layer to control the business process , receive service The data returned by the layer , Finally, return the specific page and data to the client .
It's like a company's front-line staff wants to see the feedback from the company's boss , So the front-line staff will communicate their purpose through the manager and the boss , The manager then asked the boss for instructions , Finally, the feedback from the boss is transferred to the front-line staff .
in addition ,Service It will play a great role in future distributed deployment , It's like a waiter , Which table of guests need to order , Just call the waiter ! Corresponding , What kind of business does the outside world need to complete , Just through Controller To call different Service, Here's the thing to remember ,Controller Just a middleman or forwarder , Should not be in Controller Exposed in Service Business logic of , Instead, it should be forwarded directly Service Business processing results !
3、 ... and 、Dao Database persistence layer ;
dao Layers are also called mapper layer .
dao Layer is to deal with the database , Some of the tasks responsible for contacting the database are encapsulated here ,dao The layer sends data to the database SQL sentence , Complete the task of data addition, deletion, modification and query .
dao The design of the layer is first of all design dao The interface of , And then in Spring The implementation class of this interface is defined in the configuration file of , Then you can call this interface in the module for data service processing. , You don't need to worry about which implementation class of this interface is , Very clear structure ,DAO Data source configuration for layer , And the database connection parameters are Spring Configuration in the configuration file .
DAO The master plan of design needs and the table of design , And implementation classes One A corresponding .
DAO The methods in the interface defined by layer are similar , It's up to us to DAO Layer to the database access operation to determine , Operation on Database , What we basically need to use is to add , Delete , to update , Query methods .
thus DAO The layer should basically cover the operations corresponding to these methods . besides , You can define some custom special database access methods .
Four 、Service The business layer / Service layer ;
service Layers are built on dao Above the layers , Established dao You can set up after layer Service layer , and service Layer is again controller Below that layer ,service Layer calling dao Layer interface , But also provide an interface to controller Layer to call , It's just in the middle of a layer .
All internal business logic will be handled here ,
For example, user's addition, deletion, modification and query , Or send a verification code or email , Or do a lottery and so on , Will be in service In the middle of ,service Layer calls dao Layer interface , receive dao The data returned by the layer , Complete the basic function design of the project , therefore Dao Layers are essential ;
Every model has one service Interface , Each interface encapsulates its own business processing method .
summary
DBUtil.java
General , One Controller Corresponding to one Service, One Service Corresponding to one Dao, One Dao Corresponding to a database table ,
Of course, depending on the complexity of the project or business , One Controller You can call multiple Service, And one Service You can also call multiple Dao, however Controller Layer does not allow intermodulation ,Service Layers also do not allow intermodulation ,
Namely AController Can't call directly BController
AService You can't call directly BService, Follow the principle of high cohesion and low coupling
Part of the content of this article draws on the articles of other bloggers , But I forgot which article , I would like to thank the blogger of the original article
边栏推荐
- Cross border independent station language Unicode to Hebrew
- [webdriver] upload files using AutoIT
- Yurun multidimensional makes efforts in the charity field and bravely resists the corporate public welfare banner
- What value can SRM systems bring to the enterprise?
- R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, uses exp function and coef function to ob
- DevCloud加持下的青软,让教育“智”上云端
- How to create and delete MySQL triggers
- Uploading files using AutoIT
- 基于STM32F103ZET6库函数串口实验
- Web Scraping with Beautiful Soup for Data Scientist
猜你喜欢

阿里云不同账号新旧服务器镜像迁移数据迁移同步

Opencv+YOLO-V3实现目标跟踪

双亲委派机制

Bloom filter:

Visio annotation, annotation location

SRM supplier collaborative management system function introduction

从一个被应用商店坑了的BUG说起

YoloV6+TensorRT+ONNX:基于WIN10+TensorRT8+YoloV6+ONNX的部署

How MySQL queries character set codes of tables

How to create a virtual image
随机推荐
3h精通OpenCV(五)-透视变换
Younger sister Juan takes you to learn JDBC - 2-day dash Day1
js两个二维数组合并并去除相同项(整理)
Bloom filter:
Distributed | several steps of rapid read / write separation
selenium 文件上传方法
Professor of Cambridge University: eating breakfast often is harmful and dangerous. - you know what
2022春夏系列 KOREANO ESSENTIAL重塑时装生命力
What is a SCM system? What are the advantages of a supply chain management system?
What technology is an applet container? Can it help Internet of things enterprises break through the red sea?
Selenium upload file
Selenium file upload method
Prevent form resubmission based on annotations and interceptors
从一个被应用商店坑了的BUG说起
Inherit Chinese virtues, pay attention to the health of the middle-aged and the elderly, and Yurun milk powder has strong respect for the elderly
基于STM32F103ZET6库函数定时器中断实验
剖析下零拷贝机制的实现原理,适用场景和代码实现
Function independent watchdog (iwdg) experiment based on stm32f103zet6 Library
selenium上传文件
lodash深拷贝使用