Introduction to SSH framework and Action/service/dao function partitioning

Source: Internet
Author: User

1. Action/service/dao Introduction:

Action is the management of Business (service) scheduling and managing jumps.

Service is the management of specific functions.

DAO only complete the increase and deletion check, although can 1-n,n-n,1-1 Association, fuzzy, dynamic, sub-query can be. But no matter how complex the query, DAO just package additions and deletions. As for adding and deleting how to achieve a function, DAO is no matter.

Now the most basic layering method, combined with the SSH architecture:

The model layer is the entity class for the corresponding database table.

The DAO layer uses Hibernate to connect to the database, to manipulate the database (add and revise).

Service layer: references the corresponding DAO database operation.

Action layer: reference to the corresponding service layer, where the combination of struts configuration file, jump to the specified page, of course, can accept the page to pass the request data, can also do some calculation processing.

The above hibernate, Struts, all need to be injected into spring's configuration file, and spring ties these together as a whole.

2. Three major framework Struts/hibernate/spring

To put it simply:

Used for struts--control;

hibernate--operation of the database;

spring--for decoupling.

To say in detail:

Struts in the SSH framework to play the role of control, its core is controller, that is, Actionservlet, and the core of Actionservlet is Struts-config.xml, the main control of the relationship between the processing of logic.

Hibernate is a data persistence layer, a new mapping tool for objects and relationships, provides a mapping from Java classes to data tables, and provides mechanisms for querying and recovering data, which greatly reduces the complexity of data access. The direct operation of the database is converted to the operation of the persisted object.

Spring is a lightweight control inversion (IoC) and facet-oriented (AOP) container framework. Interface-oriented programming, the dependency between container control programs, rather than traditional implementations, is directly manipulated by the program code. This is the concept of the so-called " inversion of Control ": (dependency) control is transferred from the application code to the external container, and the transfer of control is called inversion. Dependency injection, that is, the dependencies between components are determined by the container at run time, figuratively speaking, that is, the container dynamically injects some kind of dependency into the component, and the main function is decoupling.

The role of Struts, Spring, hibernate in each layer:

(1) Struts is responsible for the Web layer: Actionformbean receives the data submitted from the form on the Web page, then processes it through the action and forward to the corresponding page. The definition of <action-mapping>,actionservlet in Struts-config.xml is loaded.

(2) Spring is responsible for business layer management, i.e. service (or manager).

    • The service provides a statistical calling interface for the action, encapsulating the DAO of the persistence layer;
    • Can write some of their own business methods;
    • A unified JavaBean management method;
    • declarative transaction management;
    • Integrated Hibernate.

(3) Hibernate, responsible for persistence layer, complete the crud operation to the database. Provide or/mapping. It consists of a set of. hbm.xml files and Pojo, which correspond to the tables in the database. Then you define DAO, which is the class that deals with the database, and they use the PO.

3. Framework Business Logic Analysis:

In Struts + Spring + hibernate systems,

The invocation process for an object is:jsp-action-service-dao-hibernate.

The flow of data is: Actionformbean accepts the user's data, the action takes the data out of the Actionformbean, encapsulates it into VO or PO, and then calls the business layer's bean class, completing various business processes and then forward. When the business layer bean receives the PO object, it invokes the DAO interface method for persistence.

Advantages of the SSH framework:

The biggest benefit of hibernate is that the entity classes are generated in reverse based on the table of the database, and there is a relationship inside, and it is also very convenient for the operation of the data;

Spring, which eliminates the process of new objects inside a class, shows the call and the called relationship directly to the configuration file, making any operation easier.

A simple process example illustrates:

When the framework of the program is set up and the various jar packages are imported, the business logic is analyzed-

Assume a basic registration function: The page has two text boxes, a user name (username), and a password (password). QQ Registration page description, here with a nickname and password as a representative for example.

The First is the action layer : It is responsible for the transfer of data between the page and the program, but also the role is to do page jumps. Page by the user fill out the form data, click the Submit button, the page's form data from hibernate automatically encapsulated to the page form corresponding to the Actionfrom (Actionfrom and entity class is not a thing, Actionfrom is the page has what value, the class write what attributes, is used to encapsulate form data, whereas entity classes are generated exactly as a field in a database, entity classes can be used as actionfrom, but actionfrom must not be used as entity classes, so that form data is clicked on the action in the form of a Actionfrom object. The Submit button "executes the method that exists. All you need to do at this time is to put the form data into the database. At this point, the action function is over, and then the data is passed into the biz layer.

bize layer (business Logic layer): is responsible for the processing of data. If there is no data Processing task, this layer only does the simple data transfer function, and then to the DAO layer.

DAO Layer (Database Operation layer): is responsible for the data to the database additions and deletions of the operation.

In this registered framework, if spring is not used, the data passing between each layer requires a new instance of the class that invokes the layer data. In the case of spring, what needs to be done is to write an interface class for each class of the DAO layer and the Biz layer, and to write the method of implementing the class in the interface class, not the new object at the time of the call, directly using the object Point (.). method, and don't forget to add the Set/get method to each object.

Transferred from: http://blog.csdn.net/inter_peng/article/details/41021727

Introduction to SSH framework and Action/service/dao function partitioning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.