zoukankan      html  css  js  c++  java
  • SSM中事务的配置模板

    Spring-tx.xml

    配置思路:

    1. 声明事务管理器DataSourceTransactionManager,并注入数据源dataSource属性

    2.配置事务增强<tx:advice>,声明事务管理器transaction-manager="",默认值为transaction-manager="transactionManager",设置事务属性<tx:attributes>,声明要为哪些方法配置事务<tx:method name=""/>,以及定义方法的一些属性

    3. 配置切面<aop:config>,首先通过切入点表达式声明切入点位置,然后关联事务通知和切入点

    <?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"
           xsi:schemaLocation="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 id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <!-- 配置事务增强 -->
        <tx:advice id="transaction" transaction-manager="transactionManager">
            <!-- 事务属性 -->
            <tx:attributes>
                <tx:method name="*"/>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="find*" read-only="true"/>
                <tx:method name="list*" read-only="true"/>
    
                <!--增删改 -->
                <tx:method name="insert*" timeout="5000" rollback-for="java.lang.Exception"/>
                <tx:method name="add*" timeout="5000"/>
                <tx:method name="update*" timeout="5000"/>
                <tx:method name="delete*" timeout="5000"/>
            </tx:attributes>
        </tx:advice>
    
        <!-- 配置版的事务切面 -->
        <aop:config>
            <!--  *任意返回值 impl包下  .*任意类 .*任意方法 (..)任意参数  -->
            <aop:pointcut id="pointcut" expression="execution(* com.juyss.service.impl.*.*(..))"/>
            <!--  advice-ref:关联增强事务 pointcut-ref:关联事务的切入点  -->
            <aop:advisor advice-ref="transaction" pointcut-ref="pointcut"/>
        </aop:config>
        
    </beans>
    
  • 相关阅读:
    bzoj 3438: 小M的作物
    bzoj 4445 [SCOI2015] 小凸想跑步
    hdu 4899 Hero meet devil
    hdu 4898 The Revenge of the Princess’ Knight
    【NOIP1999】拦截导弹
    【OpenJudge】2991:2011 题解
    【cqbzoj】1785:残缺棋盘上放车的方案数 --状压dp --输入毁一生
    【cqbzoj】:1330 Prime DP(Ahio2001 质数和分解)
    【Openjudge:Noi】7891:一元三次方程求解 c++
    【USACO FEB 2010 SILVER】吃巧克力(Chocolate Eating)
  • 原文地址:https://www.cnblogs.com/juyss/p/13786370.html
Copyright © 2011-2022 走看看