spring+hibernate+struts2整合方案(2,整合细则)

运用三大框架,使用MyEclipse帮我们导入jar包会发现有些包冲突了或者有些包是多余的,让我们来手动配置三大框架吧!

1、下载三大框架的必备的jar包,下载地址:http://download.csdn.net/detail/harderxin/4420066

2、新建一个web工程SSHTest,将jar包导入到工程中,注意导入进去后在WEB-INFO目录的lib文件夹中要有相应的jar包,jar包如下


3、打开web.xml,在里面添加如下代码:

              //Spring配置

  1.        <listener> 
  2.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  3.     <!-- default: /WEB-INF/applicationContext.xml --> 
  4. </listener> 
  5.  
  6. <context-param> 
  7.     <param-name>contextConfigLocation</param-name> 
  8.     <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  --> 
  9.     <param-value>classpath:beans.xml</param-value> 
  10. </context-param> 
        <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
		<!-- default: /WEB-INF/applicationContext.xml -->
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  -->
		<param-value>classpath:beans.xml</param-value>
	</context-param>

            //Struts配置

  1. <filter> 
  2.     <filter-name>struts2</filter-name> 
  3.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
  4. </filter> 
  5.  
  6. <filter-mapping> 
  7.     <filter-name>struts2</filter-name> 
  8.     <url-pattern>/*</url-pattern> 
  9. </filter-mapping> 
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

4、打开Struts.xml,配置Struts一些常量,并将stsruts交给Spring容器来管理

 

  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <!DOCTYPE struts PUBLIC 
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> 
  5.      
  6.     <constant name="struts.configuration.xml.reload" value="true"/> 
  7.     <constant name="struts.action.extension" value="action,do,webwork" /> 
  8.      
  9.     <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
  10.         <constant name="struts.devMode" value="true" /> 
  11.     <constant name="struts.multipart.maxSize" value="100971520"></constant> 
  12.     <constant name="struts.i18n.encoding" value="UTF-8"></constant> 
  13.     <constant name="struts.objectFactory.spring.autoWire" value="name"></constant> 
  14.     <!-- 将struts2交给spring管理--> 
  15.     <constant name="struts.objectFactory" value="spring"></constant> 
  16.     <constant name="struts.custom.i18n.resources" value="messages"></constant> 
  17.      
  18.     <include file="strutsConfig/common.xml"></include> 
  19.     <include file="strutsConfig/struts-test.xml"></include> 
  20.     <include file="strutsConfig/struts-admin.xml"></include> 
  21.     <include file="strutsConfig/struts-web.xml"></include> 
  22.     <include file="strutsConfig/struts-admin.xml"></include> 
  23.     <include file="strutsConfig/struts-consumer.xml"></include> 
  24.     <include file="strutsConfig/struts-restaurant.xml"></include> 
  25.     <include file="strutsConfig/struts-web.xml"></include> 
  26. </struts> 
<?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.configuration.xml.reload" value="true"/>
	<constant name="struts.action.extension" value="action,do,webwork" />
	
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
        <constant name="struts.devMode" value="true" />
	<constant name="struts.multipart.maxSize" value="100971520"></constant>
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
	<constant name="struts.objectFactory.spring.autoWire" value="name"></constant>
	<!-- 将struts2交给spring管理-->
	<constant name="struts.objectFactory" value="spring"></constant>
	<constant name="struts.custom.i18n.resources" value="messages"></constant>
	
	<include file="strutsConfig/common.xml"></include>
	<include file="strutsConfig/struts-test.xml"></include>
	<include file="strutsConfig/struts-admin.xml"></include>
	<include file="strutsConfig/struts-web.xml"></include>
	<include file="strutsConfig/struts-admin.xml"></include>
	<include file="strutsConfig/struts-consumer.xml"></include>
	<include file="strutsConfig/struts-restaurant.xml"></include>
	<include file="strutsConfig/struts-web.xml"></include>
</struts>

5、打开Spring,配置连接池和事务

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xmlns:context="http://www.springframework.org/schema/context" 
  5.     xmlns:aop="http://www.springframework.org/schema/aop" 
  6.     xmlns:tx="http://www.springframework.org/schema/tx" 
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans 
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
  9.            http://www.springframework.org/schema/context 
  10.            http://www.springframework.org/schema/context/spring-context-2.5.xsd 
  11.            http://www.springframework.org/schema/aop 
  12.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
  13.            http://www.springframework.org/schema/tx  
  14.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
  15.     <context:annotation-config /> 
  16.     <context:component-scan base-package="com.bjsxt" /> 
  17.  
  18.     <!--  
  19.         <bean id="dataSource" 
  20.         class="org.apache.commons.dbcp.BasicDataSource" 
  21.         destroy-method="close"> 
  22.          
  23.          
  24.         <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
  25.         <property name="url" value="jdbc:mysql://localhost:3306/spring" /> 
  26.         <property name="username" value="root" /> 
  27.         <property name="password" value="bjsxt" /> 
  28.         </bean> 
  29.     --> 
  30.  
  31.     <bean 
  32.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
  33.         <property name="locations"> 
  34.             <value>classpath:jdbc.properties</value> 
  35.         </property> 
  36.     </bean> 
  37.  
  38.     <bean id="dataSource" destroy-method="close" 
  39.         class="org.apache.commons.dbcp.BasicDataSource"> 
  40.         <property name="driverClassName" 
  41.             value="${jdbc.driverClassName}" /> 
  42.         <property name="url" value="${jdbc.url}" /> 
  43.         <property name="username" value="${jdbc.username}" /> 
  44.         <property name="password" value="${jdbc.password}" /> 
  45.     </bean> 
  46.      
  47.      
  48.  
  49.     <bean id="sf" 
  50.         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
  51.         <property name="dataSource" ref="dataSource" /> 
  52.         <!--  
  53.         <property name="annotatedClasses"> 
  54.             <list> 
  55.                 <value>com.bjsxt.model.User</value> 
  56.                 <value>com.bjsxt.model.Log</value> 
  57.             </list> 
  58.         </property> 
  59.          --> 
  60.          <property name="packagesToScan"> 
  61.             <list> 
  62.                 <value>com.bjsxt.registration.model</value> 
  63.                  
  64.             </list> 
  65.         </property> 
  66.         <property name="hibernateProperties"> 
  67.             <props> 
  68.                 <prop key="hibernate.dialect"> 
  69.                     org.hibernate.dialect.MySQLDialect 
  70.                 </prop> 
  71.                 <prop key="hibernate.show_sql">true</prop> 
  72.             </props> 
  73.         </property> 
  74.     </bean> 
  75.      
  76.     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> 
  77.         <property name="sessionFactory" ref="sf"></property> 
  78.     </bean> 
  79.  
  80.     <bean id="txManager" 
  81.         class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
  82.         <property name="sessionFactory" ref="sf" /> 
  83.     </bean> 
  84.  
  85.     <aop:config> 
  86.         <aop:pointcut id="bussinessService" 
  87.             expression="execution(public * com.bjsxt.registration.service.*.*(..))" /> 
  88.         <aop:advisor pointcut-ref="bussinessService" 
  89.             advice-ref="txAdvice" /> 
  90.     </aop:config> 
  91.  
  92.     <tx:advice id="txAdvice" transaction-manager="txManager"> 
  93.         <tx:attributes> 
  94.             <tx:method name="exists" read-only="true" /> 
  95.             <tx:method name="add*" propagation="REQUIRED"/> 
  96.         </tx:attributes> 
  97.     </tx:advice> 
  98.  
  99. </beans> 
<?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:context="http://www.springframework.org/schema/context"
	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/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<context:annotation-config />
	<context:component-scan base-package="com.bjsxt" />

	<!-- 
		<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		
		
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/spring" />
		<property name="username" value="root" />
		<property name="password" value="bjsxt" />
		</bean>
	-->

	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>

	<bean id="dataSource" destroy-method="close"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	
	

	<bean id="sf"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 
		<property name="annotatedClasses">
			<list>
				<value>com.bjsxt.model.User</value>
				<value>com.bjsxt.model.Log</value>
			</list>
		</property>
		 -->
		 <property name="packagesToScan">
			<list>
				<value>com.bjsxt.registration.model</value>
				
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
	</bean>
	
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sf"></property>
	</bean>

	<bean id="txManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sf" />
	</bean>

	<aop:config>
		<aop:pointcut id="bussinessService"
			expression="execution(public * com.bjsxt.registration.service.*.*(..))" />
		<aop:advisor pointcut-ref="bussinessService"
			advice-ref="txAdvice" />
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="exists" read-only="true" />
			<tx:method name="add*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>

</beans>

6、添加相应实体类的XXX.hbm.xml文件映射

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
  4. <!--
  5.     Mapping file autogenerated by MyEclipse Persistence Tools
  6. --> 
  7. <hibernate-mapping> 
  8.     <class name="com.jwjx.beans.DatePlanPri" table="DATEPLAN_PRI" schema="ADMIN"> 
  9.         <id name="rjhmid" type="java.lang.Long"> 
  10.             <column name="RJHMID" /> 
  11.             <generator class="increment" /> 
  12.         </id> 
  13.         <property name="jctype" type="java.lang.String"> 
  14.             <column name="JCTYPE" length="20" /> 
  15.         </property> 
  16.         <property name="jcnum" type="java.lang.String"> 
  17.             <column name="JCNUM" length="20" /> 
  18.         </property> 
  19.         <property name="fixfreque" type="java.lang.String"> 
  20.             <column name="FIXFREQUE" length="20" /> 
  21.         </property> 
  22.         <property name="kcsj" type="java.lang.String"> 
  23.             <column name="KCSJ" length="20" /> 
  24.         </property> 
  25.         <property name="jhqjsj" type="java.lang.String"> 
  26.             <column name="JHQJSJ" length="20" /> 
  27.         </property> 
  28.     </class> 
  29. </hibernate-mapping> 
<?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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.jwjx.beans.DatePlanPri" table="DATEPLAN_PRI" schema="ADMIN">
        <id name="rjhmid" type="java.lang.Long">
            <column name="RJHMID" />
            <generator class="increment" />
        </id>
        <property name="jctype" type="java.lang.String">
            <column name="JCTYPE" length="20" />
        </property>
        <property name="jcnum" type="java.lang.String">
            <column name="JCNUM" length="20" />
        </property>
        <property name="fixfreque" type="java.lang.String">
            <column name="FIXFREQUE" length="20" />
        </property>
        <property name="kcsj" type="java.lang.String">
            <column name="KCSJ" length="20" />
        </property>
        <property name="jhqjsj" type="java.lang.String">
            <column name="JHQJSJ" length="20" />
        </property>
    </class>
</hibernate-mapping>


7、将项目部署到Tomcat服务器,如果启动过程中没有报错,说明ssh三大框架配置OK!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值