spring配置事务管理

首先配置hibernate的数据源和sessionfactory

  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
        <property name="locations">    
           <list>    
              <!-- 这里支持多种寻址方式:classpath和file -->    
              <value>classpath:config/properties/db.properties</value>    
            </list>    
        </property>    
         </bean>  
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName" value="${db.driverClassName}">  
        </property>  
        <property name="url" value="${db.url}">  
        </property>  
        <property name="username" value="${db.username}"></property>  
        <property name="password" value="${db.password}"></property>  
        </bean>  
          
        <!-- 配置数据库 -->  
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        <property name="dataSource">  
            <ref bean="dataSource" />  
        </property>  
        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.dialect">  
                    org.hibernate.dialect.MySQLDialect  
                </prop>  
            </props>  
        </property>  
        <property name="mappingResources">  
            <list>  
                <value>config/hibernate/getExpress.hbm.xml</value>  
                <value>config/hibernate/UserInfo.hbm.xml</value>  
            </list>  
        </property>  
    </bean> 

 接着就是配置事务管理:

<bean id="shiwuManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
        <property name="globalRollbackOnParticipationFailure" value="false"></property>
    </bean>
    <tx:annotation-driven transaction-manager="shiwuManager" />

 这样只要在方法前加入标签:@Transactional就会对这个方法进行事务管理

如果加在类前面,则是对所有方法进行事务管理

猜你喜欢

转载自dwj147258.iteye.com/blog/2367424