SSH (STRUTS + spring + hibernate)

Source: Internet
Author: User
SSH (STRUTS + spring + hibernate)

Their respective roles:

To put it simply:

Struts-controlled

Hibernate

Spring decoupled
Details:

Struts plays a controlling role in the SSH framework. Its core is controller, that is, actionservlet. The core of actionservlet is Struts-confi G. XML, which mainly controls the processing of logical relationships.

Hibernate is a data persistence layer. It is a new ing tool for objects and relationships. It provides ing from Java classes to data tables, as well as data query and recovery mechanisms, this greatly reduces the complexity of data access. Converts direct operations on the database to operations on persistent objects.

Spring is a lightweight container framework for IOC and AOP. It is interface-oriented programming and is based on the (dependency) Relationship between container control programs, in non-traditional implementations, programs are directly controlled by code. This is the concept of "control reversal": (dependency) control is transferred from application code to external containers, and control is transferred. Dependency injection, that is, the dependency between components is determined by the container at runtime. In the image, the container dynamically injects a dependency into the component.
The main function of decoupling

Role of struts, spring, and hibernate at various layers

1) struts is responsible for the web layer.

Actionformbean receives the data submitted by the form on the webpage, processes the data through the action, and forwards the data to the corresponding webpage.

Define <action-mapping> In the struts-config.xml and the actionservlet loads.

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

1. The Service provides a statistical call interface for action to encapsulate the DAO of the persistent layer.

2. You can write your own business methods.

3. unified management of JavaBean

4. Declarative Transaction Management

5. Integrate hiberante

3) hiberante, responsible for the persistence layer and performing database crud operations

Hibernate provides or/mapping for the persistence layer.

It has a set of. HBM. xml files and pojo, which correspond to tables in the database. Then define Dao. These are classes used to deal with databases, and they use PO.

In the Struts + spring + hibernate system,

The object calling process is: JSP-> action-> service-> Dao-> hibernate.

The data flow direction is that actionformbean accepts user data. Action extracts the data from actionfrombean and encapsulates the data into VO or po,

Then, call the Bean class at the business layer to complete various business processing and then forward. After receiving this po object, the business layer bean will call the DaO interface method to perform persistence operations.

Simple process

The SSH framework has advantages,
Personal Understanding:
The biggest benefit of Hibernian is that it generates entity classes in reverse direction based on the database table, and has a link in it. It also facilitates data operations.
Spring I understand is the programmer's spring, saving the process of new objects in the class and displaying the call and called relationships directly to the configuration file, it is easy to do anything.

Let me tell you that,
After the program framework is set up and various jar packages are imported, the project starts to work.
Take the registration function for example, the page only has two text boxes, one user name (uname) and one password (PWD)
The first is the action layer:
It is responsible for data transmission between pages and programs, and also serves as page Jump. The user fills in the form data on the page and clicks the submit button. The form data on the page is automatically encapsulated by Hibernian to the actionfrom corresponding to the form on the page (actionfrom is not a thing with the entity class, actionfrom is the value of a page, and the attributes written in my class are used to encapsulate form data. Entity classes are generated based on database fields. Entity classes can be used as actionfrom, but actionfrom cannot be used as entity classes ), in this way, the form data is stored in the method executed by clicking the submit button of the action in the form of an actionfrom object. In this case, you need to store the form data into the database, however, if the database does not only have the uname and PWD fields on the page, but also has other fields such as the user status, you need to assign a value to this attribute in the program, otherwise, a non-empty error will be reported. In this example, there are only two text boxes on the page, and the database only has these two fields, so you do not have to consider that much. In this step, the function of action has come to an end, and data is transferred to the biz layer.
Biz layer (business logic layer ):
It is responsible for data processing. For example, if the website function is card sales, you need to discount the card at this layer and perform other mathematical operations, in this example, this layer only performs data transmission, and then goes to the DaO layer.
Dao layer (database operation layer ):
It is responsible for adding, deleting, modifying, and querying data to the database, so it will not be explained much.

In the above example, if spring is not used, you need to create an instance that calls the data class at the layer for data transmission between each layer. If spring is used, what you need to do is to write an interface class for each class in the DAO and Biz layers, and write methods of implementation classes in the interface class, so that no new object will be created during the call, directly use the object vertex (.) you can do this by adding the set/get method to each object.

Biz layer example

Private savemyscheduledaoimpl smsdi; // defines the write interface (savemyscheduledaoimpl) in the class. Do not write the implementation class and do not create a new

Set/get method ·········

Public list savemyschedule (schedule S)
{
Return smsdi. savemyschedule (s); // because it is an interface instance, it is required that all methods of the implementation class be written in this interface so that (.)
}

Only one biz layer is written here.

The above process is to get the value from the page and store it in the database. It is a one-way action layer-> bzi layer-> Dao layer data transmission, the data transmission at Dao layer> biz layer> action layer is the same as what I just described.

Summary at this step:
Write the interface at the DaO layer and write all methods of the implementation class in the interface.
The biz layer writes the interface and writes all methods of the implementation class in the interface. To call the DaO layer method, only define the interface of the class to which the DaO layer calls the method, then add the get/set method, and use the method without the new instance point.
The action layer is the same as the biz layer.

The following is how powerful spring works ------- dependency Injection
After writing all the modified dead things, we have to inject dependencies in the configuration file.

Find the spring configuration file and first note the DaO layer. Note that the object class must be added during dependency injection !! Not an interface !! This is a good habit.
The dependency injection on the DaO layer is bound to the value of sessionfactory that you asked you to fill in when adding spring to the project. This bean should exist in the configuration file.
Then there is the biz layer dependency injection, which is also the entity class. According to my example, smsdi needs to be bound, in this case, enter the name of the class corresponding to the DaO layer you noted.
Then, the dependency injection at the action layer is roughly the same as that at the biz layer.

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.