For the SSH framework, the core is spring.
1Struts is only responsible for intercepting requests on the browser side (which can be understood to get request requests, that is, to get the request object.) The Request object contains information such as the form information that the user entered on the page , the currently logged on user , and so on, and maps the requests to the execution class. All the rest of the work is given to spring to execute (or all delegates to spring).
2spring is the core of SSH.
2.1spring is responsible for controlling the reversal (that is, by defining member variables in the action class, you can use the methods in the service class, in fact the service class is the interface class and is actually executing the method in the Serviceimp Class).
2.1 Manage hibernate to implement hibernate initialization. (Hibernate is initialized to operate the database.)
3hibernate is responsible for database operations.
It is obvious that spring is the core of the SSH framework.
========================SSH Framework, a common class how to perform database operations. ===================
SSH framework, a common class how to perform database operations. Because Hibernate is available, users do not have to write the most primitive SQL statements, but instead use hibernate to manipulate the database. So the steps are as follows
1 Hibernate is initialized with spring (that is, the database login user name, password, server address and port number are obtained from the spring configuration file).
2 access the database through hibernate.
The example code is as follows:
Import Org.springframework.web.context.WebApplicationContext;
Import Org.springframework.web.context.ContextLoader;
Import Org.hibernate.SessionFactory;
Import Org.springframework.orm.hibernate3.HibernateTemplate;
public class test{
public void TestFunction () {
Starting with the spring initialization of hibernate, it is also visible that spring is responsible for managing hibernate, and Spring is the core of SSH. And struts is only responsible for requests to the execution class mapping, and database operation//Do not have any relationship
Webapplicationcontext WAC = Contextloader.getcurrentwebapplicationcontext ();
Sessionfactory Factory=wac.getbean (Sessionfactory.class);
hibernatetemplate hh = new Hibernatetemplate (factory);
Working with databases through hibernate
String id= "Xxxxxxxxxxxxxx";
User Instance = (user) gethibernatetemplate (). Get ("Com.org.base.User", id);//Where User is the model (understandable as a user table) of the users table, ID is the user's ID. The meaning of the sentence is through The user ID gets the user.
}
}
========================ssh framework, and how the Xxxdao class in the SSH framework does the database operation. ===================
For the Xxxdao class in the SSH framework, it is much easier to access the database.
As follows:
Import java.util.List;
Import Javax.annotation.Resource;
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
Import Org.springframework.orm.hibernate3.HibernateTemplate;
Import org.springframework.stereotype.Component;
Import Com.org.dao.UserDAO;
Import Com.org.model.User;
public class Userdaoimp implements userdao{
Private Hibernatetemplate hibernatetemplate;
protected void Initdao () {
Do nothing
}
Public Hibernatetemplate gethibernatetemplate () {
return hibernatetemplate;
}
public void Sethibernatetemplate (Hibernatetemplate hibernatetemplate) {
This.hibernatetemplate = hibernatetemplate;
}
Public List FindAll () {
Log.debug ("Finding all User instances");
try{
String queryString = "from User";
Return Gethibernatetemplate (). Find (queryString);
}
catch (RuntimeException re) {
Log.error ("Find all Failed", re);
throw re;
}
}
}
Only need to introduce member variables, private hibernatetemplate hibernatetemplate;
The hibernatetemplate needs to be configured in the Spring configuration file (for control inversion), as follows:
Note in the figure above, Hibernatetemplate is sessionfactory as an incoming parameter when "control inversion". and the normal class Access database, the following statement is equivalent:
Webapplicationcontext WAC = Contextloader.getcurrentwebapplicationcontext ();
sessionfactory Factory=wac.getbean (sessionfactory.class);
hibernatetemplate hh = new Hibernatetemplate (factory);