Spring + hiberNate 完整配置

本文详细介绍了如何在Spring MVC框架中整合使用Hibernate作为持久层解决方案,包括配置web.xml、spring.xml文件,以及实现自动清理内存、解决session关闭问题等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. web.xml

</pre><pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
		<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:/spring/**/spring*.xml,classpath*:spring*.xml
		</param-value>
	</context-param>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/classes/log4j.properties</param-value>
	</context-param>
	<!--spring log4j监听器 start-->
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener
		</listener-class>
	</listener>
	<!-- end -->
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
  <listener-class>
     org.springframework.web.util.IntrospectorCleanupListener
  </listener-class>
</listener>

  <!-- 以下过滤器用于解决session被提前关闭的问题 -->
	  <filter>     
        <filter-name>hibernateFilter</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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      
<!-- 能自动清除已被JVM和WEB容器废弃的空对象,帮助开发人员管理WEB容器中的内存空间 -->
	<filter>
		<filter-name>struts-cleanup</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts-cleanup</filter-name>
		<url-pattern>*.action</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
	</filter-mapping>
	
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
        <filter-mapping>    
	          <filter-name>hibernateFilter</filter-name> 
	           <url-pattern>/*</url-pattern>  
	    </filter-mapping>
	
    <session-config>
		<session-timeout>60</session-timeout>
	</session-config>
  <welcome-file-list>
    <welcome-file>common/index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


2.spring.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: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.ssh"/>
 
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:DBconfig.properties</value>
    </property>
</bean>
<!-- 数据源配置 -->
  <bean id="dataSource" destroy-method="close"  class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${db.driverClassName}"/>
    <property name="url" value="${db.url}"/>
    <property name="username" value="${db.username}"/>
    <property name="password" value="${db.password}"/>
    <property name="maxActive" value="${db.maxActive}" /> 
<property name="initialSize" value="${db.initialSize}" /> 
<property name="maxIdle" value="${db.maxIdle}" /> 
<property name="maxWait" value="${db.maxWait}" /> 
<property name="defaultAutoCommit" value="true" /> 
<property name="removeAbandoned" value="true" /> 
<property name="removeAbandonedTimeout" value="${db.removeAbandonedTimeout}" /> 
<property name="validationQuery" value="${db.validationQuery}" />
     <property name="testOnBorrow" value="${db.testOnBorrow}" /> 
</bean>


<!--     sessionFactory配置-->
  <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  支持实体注解-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!--  不支持实体注解-->
       <property name="dataSource" ref="dataSource"/>
       <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop> 
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
        <!--
        <property name="configLocation" >
            <value>classpath:hibernate.cfg.xml</value>    
        </property>
        -->     
       <!-- 也可以用通配符指定,'*'指定一个文件(路径)名,'**'指定多个文件(路径)名    -->
        <property name="mappingLocations">
           <value>classpath:com/ssh/model/*.hbm.xml</value>
      </property>
     
        <!--  实体类,未加注解 指定classpath下具体映射文件名
        <property name="mappingResources">
          <list>
             <value>com/ssh/model/user.hbm.xml</value>
          </list>
        </property>
        -->
    
         <!--  实体类,加注解(1)
         <property name="annotatedClasses">
             <list>
               <value>com.ssh.model.User</value>
             </list>
        </property>
        -->
         <!--  实体类,加注解(2),扫描spring要注入的bean
         <property name="packagesToScan">
             <list>
               <value>com.ssh.model</value>
             </list>
        </property>
         -->
    </bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
          <!--事务管理器配置-->
 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory"  ref="sessionFactory"/>
 </bean>
<!-- 定义事务管理器(声明式的事务) -->  
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>


    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
        </tx:attributes>
    </tx:advice>
    
    <aop:config>
        <aop:pointcut id="interceptorPointCuts"
            expression="execution(* com.bluesky.spring.dao.*.*(..))" />
        <aop:advisor advice-ref="txAdvice"
            pointcut-ref="interceptorPointCuts" />        
    </aop:config>      
 
    <!-- 注解方式配置事务 -->
   <tx:annotation-driven transaction-manager="txManager"/>
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值