zoukankan      html  css  js  c++  java
  • spring和mybatis整合进行事务管理

    1、声明式实现事务管理

      XML命名空间定义,定义用于事务支持的tx命名空间和AOP支持的aop命名空间:

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
        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-3.2.xsd 
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.2.xsd 
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
            
        <!-- 事务管理器,对mybatis操作数据库 控制。spring使用jdbc的事务控制类-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 数据源 :dataSource在dao中配置-->
            <property name="dataSource" ref="dataSource"/>
        </bean>
        
        <!-- 通知 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- 传播行为,规范开发接口-->
                <tx:method name="save*" propagation="REQUIRED"/>
                <tx:method name="delete*" propagation="REQUIRED"/>
                <tx:method name="insert*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
                <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
                <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            </tx:attributes>
        </tx:advice>
            
        <!-- 调用通知:aop -->
        <aop:config>
            <aop:advisor advice-ref="txAdvice" pointcut="execution(* edu.mybatis.service.impl.*.*(..))"/>
        </aop:config>
    </beans>

    <bean id="transactionManager" class="...">用来定义事务管理器

    <tx:advice>标签用于事务通知,用于指定事务,transaction-manager属性用来指定事务管理器,并且通过<tx:attributes>方法来指定具体需要拦截的方法

    <tx:method name="save*">等方法是用来定义拦截一save头的方法,而且被拦截的方法应该配置事务属性:propagation="REQUIRED"表示传播行为是必须的,isolation:表示隔离级别,read-only=“true”,表示事务只读

    <aop:config>声明进行Aop的配置,advice-ref属性用来指定通知,pointcut:用来定义切点

  • 相关阅读:
    linux安装python串口工具pyserial遇到不能成功导入的问题
    yum rpm apt-get wget 辨析
    系统时间不一致导致memcached的session不共享
    ERROR: transport error 202: bind failed: Address already in use
    The user specified as a definer ('root'@'%') does not exist
    IE比Chrome强的一个地方
    电脑突然死机,系统日志记录事件ID=6008
    mysql 查询多个id
    tomcat登陆WEB显示无权限问题&& tomcat无限循环启动问题
    MySql中把一个表的数据插入到另一个表中的实现代码
  • 原文地址:https://www.cnblogs.com/googlemeoften/p/4699114.html
Copyright © 2011-2022 走看看