Home  >  Article  >  Backend Development  >  The role of Java Dao layer

The role of Java Dao layer

Guanhui
GuanhuiOriginal
2020-05-30 12:03:0412439browse

The role of Java Dao layer

The role of Java Dao layer

The Dao layer is called the data access layer. It is a relatively low-level and basic operation. It can be specific to For the addition, deletion, modification and query of a certain table or an entity, the function of the Dao layer is to encapsulate the access to the database, so that it does not involve business and achieves decoupling.

Calling process

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

Extended information

MVC pattern

MVC pattern is a software framework pattern that is widely used in JavaEE The project is under development. The MVC pattern is easy to understand, but it is also very important. We cannot do without it in future web application development.

Before, we have studied Servlet and JSP. It seems that they can complete many functions independently, but each has its own characteristics. MVC provides a way to maximize strengths and avoid weaknesses, and combine both swords.

1. What is MVC

MVC is Model, View, and Controller.

Model

The model is the part used to process data logic.

The so-called data logic refers to the mapping of data and the addition, deletion, modification and query of data. Beans, DAO (data access object, data access object), etc. all belong to the model part.

View (View)

The view is responsible for the display of data and other information, which is the page that the user sees.

html, jsp and other pages can be used as views.

Controller (controller)

The controller is the bridge between the model and the view, controlling the interaction between data and users.

The controller is usually responsible for reading data from the view, processing user input, and sending data to the model. It can also read data from the model and then send it to the view for display by the view.

The Servlet learned earlier can be used as a controller.

2. Why use MVC

I believe that the MVC model has the following three major benefits.

Looks comfortable

When we studied Servlet and JSP earlier, we found that these two things are a mixture of functions and displays, and their readability is very poor. With MVC, Servlet has It is logic code, and JSP is display code, which is clear at a glance.

It’s easy to write

The MVC model is conducive to the division of labor and cooperation among personnel. Different developers can exert their respective expertise and develop different parts at the same time.

Easy to change

MVC can focus on one aspect at a time, making application testing and iteration more convenient. Therefore, most complex applications will choose to use the MVC pattern.

The above benefits, in a jargon, are called "high cohesion and low coupling".

3. Design Pattern and Framework Pattern

When collecting information, I found that MVC is called a "design pattern" in many places. At first, I thought there was nothing wrong with it, literally. It makes sense, but if you think about it carefully, the word "design pattern" is a term that refers to things like factory pattern and singleton pattern, and it cannot be used indiscriminately. MVC should be called the "framework pattern".

So what is the difference between the two?

Design patterns are for "code", just like the stacking of bricks and tiles when building a house, they are the cornerstone of software development.

Frame pattern is for "framework" and is a reusable solution for certain application scenarios, such as the layout of buildings with different functions such as hospitals and shopping malls. It is a more macro concept than design pattern.

In future articles, I will learn from you the current mainstream frameworks.

4.MVC practice

In JavaEE, the most classic MVC pattern is

Bean(Model) JSP(View) Servlet(Controller)

If there is a requirement: to edit the information on the web page and synchronize it with the database

In terms of implementation, it can be divided into the following steps:

Use Bean to map the data table and write a DAO

Use the Servlet responsible for querying to call DAO to query the content that needs to be edited and send it to the jsp page for editing

Submit the modified value of the editing page to the Servlet responsible for updating and call DAO Update the database

Use another Servlet responsible for querying and send the updated content queried to the JSP responsible for display

Recommended tutorial: "Java Tutorial"

The above is the detailed content of The role of Java Dao layer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn