In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly analyzes the relevant knowledge points of how to divide the functions of Action, Service and Dao in Java Web. The content is detailed and easy to understand, and the operation details are reasonable, so it has a certain reference value. If you are interested, you might as well follow the editor to have a look, and follow the editor to learn more about "how to divide the functions of Action, Service and Dao in Java Web".
1. Introduction to Action/Service/DAO:
Action is a management service (Service) scheduling and management jump.
Service manages specific functions.
Action is only responsible for management, while Service is responsible for implementation.
DAO only completes the addition, deletion, modification and search, although it can be 1-1 related, fuzzy, dynamic and sub-query. But no matter how complex the query is, dao only encapsulates additions, deletions, modifications and queries. As for how to add, delete, check and modify how to achieve a function, dao does not care.
Summarize these three and explain them through examples:
Action is like a waiter. It is the duty of ta to order what the customer orders and which table to give on the dish.
Service is the chef. All the dishes on the menu sent by action are made by ta.
Dao is a kitchen handyman, and ta is in charge of everything he does with raw materials.
The interrelationship is that the dao's job is to meet the requirements of the chef (service), and the chef has to meet the requirements of the customer (page user) conveyed by the waiter (action), and the waiter is naturally to serve the customer.
Now the most basic hierarchical approach is combined with the SSH architecture. The Model layer is the entity class of the corresponding database table. The Dao layer uses hibernate to connect to the database and operate the database (add, delete, modify and check). Service layer: reference the corresponding Dao database operation. Action layer: reference the corresponding Service layer. Here, combined with the configuration file of Struts, you can jump to the specified page. Of course, you can also accept the request data passed by the page, and you can also do some calculation.
The above Hibernate and Struts need to be injected into the configuration file of spring, and Spring links them together as a whole.
two。 Three frames Struts/Hibernate/Spring
To put it simply:
For Struts-- control
Hibernate-- operates on the database
Spring-- decoupling.
To speak in detail:
Struts plays a controlling role in the SSH framework, and its core is Controller, that is, ActionServlet, while the core of ActionServlet is Struts-config.xml, which mainly controls the processing of logic relations.
Hibernate is a data persistence layer and a new mapping tool for objects and relationships. It not only provides mapping from Java classes to data tables, but also provides mechanisms such as data query and recovery, which greatly reduces the complexity of data access. Convert the direct operation on the database into the operation on persistent objects.
Spring is a lightweight IoC and AOP-oriented container framework. Interface-oriented programming is controlled by the container to control the dependencies between programs, rather than being directly controlled by program code in traditional implementations. This is the concept of so-called "control inversion": control is transferred from the application code to the external container, and the transfer of control is the so-called inversion. Dependency injection, that is, the dependency relationship between components is determined by the container at run time, that is, the container dynamically injects a certain dependency into the component, which plays a main role in decoupling.
The role of Struts, Spring and Hibernate in each layer:
(1) Struts is responsible for the Web layer: ActionFormBean receives the data submitted by the form in the web page, then processes it through Action, and then Forward to the corresponding web page. Defined in Struts-config.xml, ActionServlet is loaded.
(2) Spring is responsible for business layer management, that is, Service (or Manager).
Service provides a statistical call interface for action, encapsulating the DAO of the persistence layer
You can write your own business methods.
Unified Javabean management approach
Declarative transaction management
Integrate Hibernate.
(3) Hibernate, responsible for the persistence layer and completing the crud operation to the database. Provide OR/Mapping. It consists of a set of .hbm. XML files and POJO, which corresponds to the tables in the database. Then define DAO, these are the classes that deal with the database, and they use PO.
3. Framework business logic analysis:
In the Struts + Spring + Hibernate system
The process of calling the object is: JSP-Action-Service-DAO-Hibernate.
The flow of data is as follows: ActionFormBean accepts the user's data, Action takes the data out of ActionFormBean and encapsulates it into VO or PO, and then calls the Bean class of the business layer, and then Forward after completing various business processing. After receiving the PO object, the business layer Bean will call the DAO interface method for persistence operation.
Advantages of the SSH framework:
The biggest advantage of Hibernate is that it generates entity classes in reverse according to the tables of the database, and there are relationships in it, and it is also very convenient for data manipulation.
Spring, omitting the process of new objects in the class, shows the relationship between the call and the called directly in the configuration file, making it easy to do anything.
Examples of a simple process:
After the program framework is built, and various jar packages are imported, business logic analysis begins--
Suppose a basic registration function: the page has two text boxes, a user name (username) and a password (password). Take the description of the QQ registration page, which is represented by nicknames and passwords.
The first is the action layer: it is responsible for transferring data between the page and the program, and the function is to jump to the page. On the page, the user fills in the form data and clicks the submit button. The form data of the page is automatically encapsulated by Hibernate to the ActionFrom corresponding to the form on the page (ActionFrom is not the same thing as the entity class. ActionFrom is the value of the page, and the attribute is written in the class, which is used to encapsulate the form data. The entity class is generated entirely according to the fields of the database, and the entity class can be used as an ActionFrom, but ActionFrom can never be used as an entity class), so that the form data exists in the form of an ActionFrom object in the method executed by clicking the "submit button" in Action. All you need to do at this time is to store the form data in the database. At this point, the function of Action is over, and then the data is passed into the BIZ layer.
BIZE layer (business logic layer): responsible for the processing of data. If there is no data processing task, this layer only acts as a simple data transfer, and then goes to the Dao layer.
DAO layer (database operation layer): responsible for adding, deleting, modifying and querying data to the database.
In this registered framework, if Spring is not used, data transfer between each tier requires new an instance of a class that invokes that layer's data. If you use Spring, what you need to do is to write an interface class for each class in the DAO layer and BIZ layer, and write the method to implement the class in the interface class. Instead of new the object, you can directly use the object point (.) Method is fine, don't forget to add the set/get method to each object.
This is the end of the introduction on "how to divide the functions of Action, Service and Dao in Java Web". More related content can be searched for previous articles, hoping to help you answer questions and questions, please support the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.