To load the Spring application, you must first declare below Web.xml:
/Web-inf/applicationContext-Hibernate.xml
param-value>
context-param>
Org.springframework.Web.Context.contextloaderServlet
servlet-class>
servlet>
The above declaration is to make the web application to load the spring ApplicationContext-Hibernate.xml configuration file at the beginning.
JNDI DataSource can be used in Spring, which is declared in ApplicationContext-Hibernate.xml:
Class = "org.springframework.jndi.jndiObjectFactoryBean"> Property> bean> Spring can also use the more commonly used Apache's DBCP, the declaration is as follows: Class = "Org.apache.commons.dbcp.basicdataasource"> Property> Property> bean> Spring can also use your own DataSource, in ApplicationContext-Hibernate.xml, as follows: Class = "Org.springframework.jdbc.datasource.driverManagerDataSource"> Property> Property> Property> Property> bean> Here is the use of Hibernate: First, Spring uses a selfSessionFactoryBean to control the loading of Hibernate's sessionFactory (you can abandon the Hibernate.properties or hibernate.cfg.xml of the original Hibernate belt, and the original configuration is declared under the HibernateProperties node), the declaration is as follows: Class = "org.springframework.orm.hibernate.localsessionFactoryBean"> Property> list> Property> Net.sf.hibernate.Dialect.sqlserverdiaLect prop> True prop> prOPS> Property> bean> Spring declares the loading of the transaction, as follows: Property> bean> In order to use Spring's Hibernate support, write your own DAO class to inherit the HibernatedAosupport class, using Spring provided by Spring to write the various operations of the table in the database in this DAO. A DAO example can use some direct methods in HibernateTemplate, for example: Public role findrolebyname (string name) { Return (Role) gethibernateTemplate (). Find ("from role r where r.name =?", Name) .get (0); } You can also use some query statements defined from the Hibernate map file, which is generally used in complicated HQL statements, which can be commissioned using Hibernate8IDE, for example: Public List GetUserswithoutContactinfo () { Return GethibernateTemplate (). FindbyNamedQuery ("UserSwithoutContactInfo); } You can also customize an anonymous HibernateCallback object to control the operations of session in Hibernate, such as: Public List FindordersPlaceByuser (FINAL STRING Places) { Return gethibernateTemplate (). EXECUTEFIND ( New hibernateCallback () { Public Object Doinhibernate (Session Session) THROWS HibernateException, Sqlexception { StringBuffer SB = New StringBuffer (100); Sb.append ("Select Distinct Order"); Sb.append ("from order order"); Sb.append ("join order.lineItems lineItems"); Sb.append ("where order.placedBY =: placedby"); Sb.append ("ORDER BY Order.ID"); Query query = session.createQuery (sb.tostring ()); Query.SetString ("PlacesDBY", PlacesDBY; List list = query.list (); Return List; } // end doinhibernate () }); // end executefind () } Then define a Service class, this class holds an internal field as a corresponding DAO, which is stored in a specific business operation. Finally, in Spring's configuration files are as follows: Class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> Property> Propagation_Required, Readonly, -OrdeRexception prop> Propagation_Required, -OrdeRexception, -OrderminimumumAmountException prop> prOPS> Property> bean> Class = "edu.cqu.rdc.service.spring.ordersrvicespringImpl"> bean> Class = "edu.cqu.rdc.service.dao.hibernate.Orderhibernatedao"> bean> To reference this service in Struts. You can use the following methods: Public void setServlet (ActionServlet ActionServlet) { Super.setServlet (ActionServlet); ServletContext servletContext = ActionServlet.getServletContext (); WebApplicationContext WAC = WebApplicationContextUtils.getRequiredWebApplicationContext (servletContext); This.orderService = (iorderservice) Wac.getBean ("ORDERSERVICE"); }