[程序开发]Struts2 Spring Hibernate 的简单整合
![[程序开发]Struts2 Spring Hibernate 的简单整合](http://www.qqread.com/ArtImage/20080610/vb89_1.jpg)
WEB-INF目录下,或许不理它,到项目中去移动也可以。单击Finish, 对Spring的添加到此结束。
接着我们再添加Hibernate框架,(可以就导入一个包 Hibernate 3.0 Core)如图:
![[程序开发]Struts2 Spring Hibernate 的简单整合](http://www.qqread.com/ArtImage/20080610/vb89_2.jpg)
接着选择将Hibernate的配置文件交给Spring来进行管理,如图:
![[程序开发]Struts2 Spring Hibernate 的简单整合](http://www.qqread.com/ArtImage/20080610/vb89_3.jpg)
再为Hibernate创建一个sessionFactory,如图:
![[程序开发]Struts2 Spring Hibernate 的简单整合](http://www.qqread.com/ArtImage/20080610/vb89_4.jpg)
点击查看大图
接着再选择数据源,
![[程序开发]Struts2 Spring Hibernate 的简单整合](http://www.qqread.com/ArtImage/20080610/vb89_5.jpg)
点击查看大图
接着是提示你是否建立sessionFactory,因为已经将sessionFactory交给Spring管理了,所以在这里不用创建了
![[程序开发]Struts2 Spring Hibernate 的简单整合](http://www.qqread.com/ArtImage/20080610/vb89_6.jpg)
点击查看大图
单击Finish,并将Spring中与Hibernate中一样的Jar包全部替换,这样就完成了对Hibernate框架的加载了。
接着再加载struts2框架的jar
接着就是配置ApplicationContext.xml 文件。代码如图:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@192.168.0.109:1521:orcl">
</property>
<property name="username" value="platform"></property>
<property name="password" value="platform"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<!-- 以下是添加的,不是自动生成的 -->
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.show_sql">true</prop>
<!--上面是方便我们对程序的调试,和操作 -->
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="remover*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution (* org.agilecc.sd.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="allManagerMethod" />
</aop:config>
</beans>
再编写struts.xml 文件,具体代码如下(因现在只是配置三大框架的环境,所以很简单,空空如也):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
</struts>
接着就是最重要的web.xml配置文件了,在Struts2整合Spring的例子已经说明了,这里也不详说了,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!--
<context-param> <param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
-->
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>lazyLoadingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
这时,还不能进行编写详细的代码,应该要先测试下这个环境出错了没。部署项目到 tomcat 里,运行没有
出现错误,如出现错误,请详细检查上面每一步的操作是否一致。
到此为止,开发环境三大框架整合的准备工作就完成了。