Dao layer, service layer and controller layer in MVC

Do a graduation project to consolidate MVC and their relationship

1 , dao layer

The dao layer mainly does the work of the data persistence layer. Some tasks responsible for communicating with the database are encapsulated here. The design of the dao layer is to design the interface of the dao layer first, and then define the implementation class of this interface in the Spring configuration file, and then You can call this interface in the module to process data services, without having to care about which class is the specific implementation class of this interface. The structure is very clear. The data source configuration of the dao layer and related database connection parameters are in the Spring configuration file. Configuration.

2. Service layer

The service layer is mainly responsible for the application logic application design of the business module. The same is to first design the interface, then design its implementation class, and then configure its implementation association in the Spring configuration file. In this way, we can call the service interface in the application to perform business processing. The business of the service layer specifically calls the dao layer interface that has been defined, and encapsulating the business logic of the service layer is conducive to the independence and reusability of the general business logic. The program is very simple.

3. Controller layer

The controller layer is responsible for the control of specific business module processes. At this layer, the interface of the service layer is called to control the business process. The control configuration is also performed in the Spring configuration file. For specific business processes, there will be different controls. Device. Our specific design process can abstract the process and design a subunit process module that can be reused. This not only makes the program structure clear, but also greatly reduces the amount of code.

4. View layer

The view layer and the control layer are more closely combined, and they need to be combined for collaborative development. The view layer is mainly responsible for displaying the jsp page at the front desk.

 

5. The relationship between them:

The Service layer is built on top of the DAO layer. After the DAO layer is established, the Service layer can be built, and the Service layer is under the Controller layer. Therefore, the Service layer should both call the interface of the DAO layer and provide the interface to Controller class to call, it is just right in the middle of a position. Each model has a Service interface, and each interface encapsulates its own business processing method. 

 

Both the DAO layer and the Service layer can be developed independently. The mutual coupling is very low and can be carried out independently. Such a model is especially advantageous in the process of developing large projects. The Controller and View layers are relatively different because of the coupling degree. High, so it must be developed together, but it can also be seen as a whole developed independently of the first two layers. In this way, we only need to know the definition of the interface before the layer and the layer, and the required logic unit application can be completed by calling the interface, everything seems very clear and simple.

 

 

The methods in the interface defined by the DAO layer are similar. This is determined by the operation of the database access in the DAO layer. For the operation of the database, we basically need to add, update, delete, query and other methods . Therefore, the operations corresponding to these methods should basically be covered in the DAO layer. In addition, you can define some custom special access methods to the database. 

 

 

The calling process of each object in the project is as follows:

JSP page-Action-Service-DAO-(Hibernate framework)-Database

 

For example, JSP is like a customer in a restaurant; Action is a waiter, the customer orders anything and serves it to the customer; Service is the chef, and the dishes on the menu sent by Action are all made by it And DAO is a small worker in the kitchen, helping to wash vegetables and pick vegetables, etc. It is dry when dealing with raw materials.

When I think about it: front-end ---- action --- service --- dao ---- database when
writing code: database --- dao --- service --- action /

 

Guess you like

Origin www.cnblogs.com/SallyShan/p/12721443.html