`

struts+spring+hibernate整合

阅读更多

本文基于Struts1.3.10+Spring2.5.6+Hibernate3.3.2

一、下载相关jar

Struts:struts.apache.org

Spring:www.springsource.org/download

Hibernate:www.hibernate.org/downloads

 

二、搭建环境

1、struts需要的jar

(1)必需的jar

struts-core-1.3.10.jar,struts-extras-1.3.10.jar,struts-taglib-1.3.10.jar,antlr-2.7.2.jar,commons-beanutils-1.8.0.jar,commons-chain-1.2.jar,commons-digester-1.8.jar,commons-logging-1.0.4.jar

(2)文件上传需要的jar

commons-fileupload-1.1.1.jar,commons-io-1.1.jar

(3)验证时需要的jar

commons-validator-1.3.1.jar

2、Spring需要的jar

spring.jar,spring-webmvc-struts.jar,slf4j-api-1.5.0.jar,slf4j-log4j12-1.5.0.jar,log4j-1.2.15.jar,commons-dbcp.jar,commons-pool.jar

3、Hibernate需要的jar

hibernate-distribution-3.3.2.GA-dist.zip中的以下jar

hibernate3.jar,lib\required文件夹下的所有jar(其中antlr-2.7.6.jar与struts中的antlr-2.7.2.jar冲突,建议选择高版本)

 

三、在web.xml中对Spring容器和Struts进行实例化

    <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath*:\config\**\*applicationContext.xml,classpath*:\config\**\*applicationContext-*.xml,classpath*:**\applicationContext.xml,classpath*:**\*-applicationContext-*.xml</param-value>
 </context-param>
 <!-- 对Spring容器进行实例化 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
   
 <servlet>
  <servlet-name>struts</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/config/struts-config.xml</param-value>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>struts</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>

 

四、spring配置数据源,配置模板如下:

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       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
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
                          
 <!-- 采用单一元素属性占位符的方式配置数据源 -->                    
 <context:property-placeholder location="classpath:config/jdbc.properties"/>

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
     <property name="driverClassName" value="${jdbc.driver}"/>
     <property name="url" value="${jdbc.url}"/>
     <property name="username" value="${jdbc.username}"/>
     <property name="password" value="${jdbc.password}"/>
    </bean>
   
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <property name="dataSource" ref="dataSource"/>
     
     <!-- 指定映射文件存放的文件夹 -->
     <property name="mappingLocations">   
       <value>classpath*:/**/*.hbm.xml</value>
     </property>
  
     <!-- 指定映射文件
     <property name="mappingResources">
         <list>
            <value>config/hbm/Employee.hbm.xml</value>
            <value>config/hbm/Department.hbm.xml</value>
            <value>config/hbm/EmployeeDepartment.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>
    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
   </props>
    </property>
    
    <!-- 映射hibernateProperties属性的简写方式
    <property name="hibernateProperties">
         <value>
          hibernate.dialect=${hibernate.dialect}
          hibernate.show_sql=${hibernate.show_sql}
          hibernate.format_sql=${hibernate.format_sql}
         </value>
     </property>
     -->
 </bean>
 
   <!-- JDBC事务代理  -->
   <bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"/>
   </bean>
 <bean id="jdbcTransactionProxy"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
  abstract="true">
  <property name="transactionManager" ref="jdbcTransactionManager"/>
  <property name="transactionAttributes">
   <props>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>
 
 <!-- HIBERNATE事务代理  -->
 <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
       <property name="sessionFactory" ref="sessionFactory"/>
   </bean>
 <bean id="hibernateTransactionProxy"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
  abstract="true">
  <property name="transactionManager" ref="hibernateTransactionManager" />
  <property name="proxyTargetClass" value="true" />
  <property name="transactionAttributes">
   <props>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>
 
 <!-- 为处理BLOB进行JDBC抽取-->
 <bean id="nativeJdbcExtractor"
    class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor" lazy-init="true" />
 <bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
  <property name="nativeJdbcExtractor">
   <ref local="nativeJdbcExtractor" />
  </property>
 </bean>
 
 <bean id="baseHibernateDAO" class="dal.dao.BaseHibernateDAO">
     <description>Hibernate实现</description>
     <property name="sessionFactory" ref="sessionFactory"/>
 </bean>
 
 <bean id="baseJdbcDAO" class="dal.dao.BaseJdbcDAO">
     <description>JDBC实现</description>
     <property name="dataSource" ref="dataSource"/>
     <!--property name="pageDialect" ref="pageDialect"/-->
 </bean>
 
</beans>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics