SpringMVC+MyBatis 事务中 基于注解的声明式事务

 

 

spring事务管理包含两种情况,编程式事务、声明式事务。而声明式事务又包括基于注解@Transactional和tx+aop的方式。初学中,这里记录一下自己学习过程中用到的“基于注解的声明式事务”这种方式

spring beans.xml配置文件中关于事务部分的配置如下:
复制代码
<!-- 5.事务管理 -->
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        
        <!-- 事务通知 -->
        <tx:advice id="txAdivce" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="insert*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="delete*" propagation="REQUIRED"/>
                <tx:method name="save*" propagation="REQUIRED"/>
                <tx:method name="add*" propagation="REQUIRED"/>
                
                <tx:method name="find*" read-only="false"/>
                <tx:method name="get*" read-only="false"/>
                <tx:method name="view*" read-only="false"/>
            </tx:attributes>
        </tx:advice>
        
        <aop:config>
            <aop:pointcut expression="execution(* com.pp.service.*.*(..))" id="txPointcut"/>
            <aop:advisor advice-ref="txAdivce" pointcut-ref="txPointcut"/>
        </aop:config>
        
        <!-- 开启事务注解驱动 -->
        <tx:annotation-driven transaction-manager="txManager" /> 
复制代码

 

首先要开启事务注解驱动

 <!-- 开启事务注解驱动 -->
        <tx:annotation-driven transaction-manager="txManager" /> 

在aop切面配置节中声明 在哪个包中使用事务,我的例子中声明的是 com.pp.service 包中

 <aop:config>
            <aop:pointcut expression="execution(* com.pp.service.*.*(..))" id="txPointcut"/>
            <aop:advisor advice-ref="txAdivce" pointcut-ref="txPointcut"/>
        </aop:config>

声明在哪种方法中使用事务

复制代码
 <!-- 事务通知 -->
        <tx:advice id="txAdivce" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="insert*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="delete*" propagation="REQUIRED"/>
                <tx:method name="save*" propagation="REQUIRED"/>
                <tx:method name="add*" propagation="REQUIRED"/>
                
                <tx:method name="find*" read-only="false"/>
                <tx:method name="get*" read-only="false"/>
                <tx:method name="view*" read-only="false"/>
            </tx:attributes>
        </tx:advice>
复制代码

如果在<tx:method /> 节中没有声明在程序中使用的方法的话,需要在该方法使用@Transactional注解,如下:

复制代码
@Transactional//事务注解,需要在spring 配置文件中开启事务注解驱动
    public void add(SysRole role, String[] permissionIds) {
        this.insert(role);
        int a = permissionIds.length;
        List<SysRoleAuthorize> lst = role.getLstAuthorize();
        if (lst != null) {
            for (SysRoleAuthorize entity : lst) {
                entity.setfId(UUID.randomUUID().toString());
                entity.setfObjecttype(1);
                entity.setfObjectid(role.getfId());

                daoAuthorize.insert(entity);
            }
        }
    }
复制代码

如果不在方法上面使用@Transactional注解,那么需要在tx:advice节中增加如下黄色背景的方法声明

复制代码
 <!-- 事务通知 -->
        <tx:advice id="txAdivce" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="insert*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="delete*" propagation="REQUIRED"/>
                <tx:method name="save*" propagation="REQUIRED"/>
                <tx:method name="add*" propagation="REQUIRED"/>
                
                <tx:method name="find*" read-only="false"/>
                <tx:method name="get*" read-only="false"/>
                <tx:method name="view*" read-only="false"/>
            </tx:attributes>
        </tx:advice>
        
复制代码

这里的name=“add*”对应的就是java代码中的add方法

posted @ 2021-05-28 16:40  Forever丶随风  阅读(184)  评论(0编辑  收藏  举报