附件:整合过程word文档。s2sh无注解整合.zip与参考资料http://115.com/lb/5lbfh77s#
Spring+Struts2+Hibernate整合
整合过程
1、 配置srping启动监听器,使spring容器在web应用启动的时候启动,在web.xml中(
参考文档:H:\struts-2.2.1.1\docs\WW\spring-plugin.html):
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean.xml</param-value>
</context-param>
2、 由Spring来管理Action
a、注解方式
@Component(value="testAction")
@Scope(value="prototype")
public class TestAction extends ActionSupport {
public String test(){
System.out.println("action........");
return this.SUCCESS;
}
}
b、也可以通过xml方式,把Action当作普通的bean
配置spring文件的头部引用:在spring-framework-2.5.6/docs/reference/html-single 输入XML查找然后复制
<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.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
</beans>
(1)注入service到action中(注意:scope="prototype"表示不为单例模式,即每次执行增加或者修改操作都新创建一个action;property中的name值是class类中的变量)
<bean id="userAction" class="com.action.UserAction" scope="prototype">
<property name="userService">
<ref bean="userService" />
</property>
</bean>
(2)读src下的属性文件的bean配置
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:myserver.properties</value>
</property>
</bean>
3、 由Spring来管理Hibernate
(1)配置数据源,连接数据库:value中的值在myserver.properties文件中定义,注意变量名要一致
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"><value>${jdbc.driverClass}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.user}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>
(2)配置SessionFactory
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
(3)配置事务管理
模板化hibernate持久层
<bean id="myTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="myTxManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="myServiceMethods" expression="execution(* com.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myServiceMethods"/>
</aop:config>
定义Dao父类模板,要为抽象的,把模板化的hibernate持久层注入到dao中
<bean id="baseDao" abstract="true">
<property name="hibernateTemplate">
<ref bean="myTemplate" />
</property>
</bean>
把dao注入到service层
<bean id="userDao" class="com.dao.impl.UserDaoImpl" parent="baseDao" ></bean>
<bean id="userService" parent="myProxy" >
<property name="target">
<bean class="com.service.impl.UserServiceImpl" >
<property name="userDao">
<ref bean="userDao" />
</property>
</bean>
</property>
</bean>
<!-- 配置事务代理机制 -->
<bean id="myProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="myTxManager"/>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
(4)Properties中sqlserver与mysql的连接方式
#jdbc.driverClass="com.microsoft.jdbc.sqlserver.SQLServerDriver"
jdbc.driverClass=com.mysql.jdbc.Driver
#jdbc.url="jdbc:sqlserver://127.0.0.1:3306;DatabaseName=test"
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.user=root
jdbc.password=root
#hibernate.dialect="org.hibernate.dialect.SQLServerDialect"
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
4、 Hibernate的*.hbm.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.model.User" table="SHOPUSER">
<id name="id" column="USER_ID">
<generator class="native"/>
</id>
<property name="userName" type="java.lang.String" column="USER_NAME"/>
</class>
</hibernate-mapping>
5、 Struts2的配置
(1)写struts2.xml文件:
复制:struts-2.2.1.1\apps\struts2-blank\web-inf\classes\struts.xml,粘贴在项目src下,内容如下
<?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>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="userAction" class="com.action.UserAction" method="query">
<result name="list">/user_list.jsp</result>
</action>
</package>
</struts>
(2)导入包:struts-2.2.1.1\apps\struts2-blank\web-inf\lib下的所有包,粘贴在lib下
(3)配置web.xml文件:
找到struts-2.2.1.1\apps\struts2-blank\web-inf\web.xml,复制其中的<filter>……</filter-mapping>,粘贴在项目中的web.xml中,如:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>