Spring + Struts

xiaoxiao2021-03-06  57

To load the Spring application, you must first declare below Web.xml:

contextconfiglocation

/Web-inf/applicationContext-Hibernate.xml

SpringContextServlet

Org.springframework.Web.Context.contextloaderServlet

1

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">

java: comp / env / jdbc / mydc

Spring can also use the more commonly used Apache's DBCP, the declaration is as follows:

Class = "Org.apache.commons.dbcp.basicdataasource">

Net.SourceForge.jtds.jdbc.driver

JDBC: JTDS: SQLSERVER: // localhost: 1433 / forum

sa

123456

Spring can also use your own DataSource, in ApplicationContext-Hibernate.xml, as follows:

Class = "Org.springframework.jdbc.datasource.driverManagerDataSource">

net.sourceforge.jtds.jdbc.driver

JDBC: JTDS: SQLSERVER: // localhost: 1433 / forum

sa

123456

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">

com / meagle / bo / order.hbm.xml

com / meagle / bo / orderlineItem.hbm.xml

Net.sf.hibernate.Dialect.sqlserverdiaLect

True

True

Spring declares the loading of the transaction, as follows:

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">

Propagation_Required, Readonly, -OrdeRexception

Propagation_Required, -OrdeRexception, -OrderminimumumAmountException

Class = "edu.cqu.rdc.service.spring.ordersrvicespringImpl">

Class = "edu.cqu.rdc.service.dao.hibernate.Orderhibernatedao">

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");

}

转载请注明原文地址:https://www.9cbs.com/read-118741.html

New Post(0)