Spring整合Hibernate的事务管理

来源:互联网 发布:mac qq五笔拼音输入法 编辑:程序博客网 时间:2024/05/16 05:12

笔者遇到蛋疼的 毕业设计。为了不浪费时间做无聊的设计,决定趁这个机会练习一下SSH。但是在配置spring事物管理时遇到了不少麻烦。

映射 *.hbm.xml文件的时候  此类文件放在src下对应的包中,不知是否应该加上classpath。曾经有段时间没加上classpath 并不会报错 ,但是今天改动了spring配置文件,则除了问题,找了老半天,在前面加上classpath就OK了,这个问题出现的太奇葩。

切记session.openssion()必须手动进行关闭。

这是学SSH后时隔三个月再进行一次实际操作,虽然中间基本没再接触过,但是觉得简单了许多。    还得好好学学Spring。


附上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/context
                http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd">

    
    
    <!-- 自动扫描装配bean -->
    <context:component-scan base-package="com.mjia.*"></context:component-scan>
    <!-- 导入外部的properties文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="mappingLocations">
           <list>
              <value>classpath:com/mjia/orm/userInfo.hbm.xml</value>    
           </list>
        </property>
        <!-- 指定hibernate的配置文件位置 -->
        <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
        <!-- 配置c3p0数据库连接池 -->
        <property name="dataSource">
            <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <property name="jdbcUrl" value="${jdbcUrl}"></property>
                <property name="driverClass" value="${driverClass}"></property>
                <property name="user" value="${user}"></property>
                <property name="password" value="${password}"></property>
                <!-- 其他配置 -->
                <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
                <property name="initialPoolSize" value="3"></property>
                <!--连接池中保留的最小连接数。Default: 3 -->
                <property name="minPoolSize" value="3"></property>
                <!--连接池中保留的最大连接数。Default: 15 -->
                <property name="maxPoolSize" value="5"></property>
                <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
                <property name="acquireIncrement" value="3"></property>
                <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
                <property name="maxStatements" value="8"></property>
                <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
                <property name="maxStatementsPerConnection" value="5"></property>
                <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
                <property name="maxIdleTime" value="1800"></property>
           
            </bean>
        </property>
         <property name="hibernateProperties">  
                <props>  
                   <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <!-- <prop key= "hibernate.current_session_context_class">thread</prop> -->
                   <prop key="hibernate.connection.autocommit">true</prop>
                   <prop key="hibernate.show_sql">true</prop>  
                   <prop key="hibernate.format_sql">true</prop>
                   <prop key="hibernate.hbm2ddl.auto">update</prop>  
                </props>  
         </property>  
    </bean>

    <bean name="userDao" class="com.mjia.daoimpl.normalUserImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
            
        <bean name="loginAction" class="com.mjia.action.userLoginAction">
        <property name="userdao" ref="userDao">
        </property>
        <property name="form" ref="userLoginForm">
        </property>
    </bean>
        <bean name="registerAction" class="com.mjia.action.userRegisterAction">
        <property name="userdao" ref="userDao">
        </property>
    </bean>
    
    <bean name="userInfo" class="com.mjia.orm.userInfo"></bean>
    <bean name="adminInfo" class="com.mjia.orm.adminInfo"></bean>
    <bean name="userLoginForm" class="com.mjia.form.userLoginForm"></bean>
        <bean name="roleManager" class="com.mjia.action.roleManagerAction">
    </bean>
    <bean name="systemManagement" class="com.mjia.action.systemManagementAction">
    </bean>
    
            <!-- 定义事务管理器(声明式的事务) -->    
    <bean id="transactionManager"  
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>  
 
    <tx:advice id="txAdvice" transaction-manager="transactionManager">  
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" />  
        </tx:attributes>  
    </tx:advice>  
      
    <aop:config>  
        <aop:pointcut id="interceptorPointCuts"  
            expression="execution(* com.mjia.daoimpl.*.*(..))" />  
        <aop:advisor advice-ref="txAdvice"  
            pointcut-ref="interceptorPointCuts" />          
    </aop:config>
</beans>


0 0
原创粉丝点击