zoukankan      html  css  js  c++  java
  • spring配置aop事务

    spring配置事务两种方式1,注解@Transactional2.xml方式

    下面主要对xml方式配置spring事务进行说明

    原理:使用aop(面向切面编程)对springz中的事务进行管理(同一session下完成一项不可细分的功能/业务需求)

    主要分3部

    (1)添加transactionManager事务管理器(aop中的切面)

    (2)配置事务属性(事务隔离级别,传播级别,是否只读)

    (3)配置事务切入点,并将切入点与属性关联

    <?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:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
            <!-- 事务管理器 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- 配置事务传播行为 规范事务crud方法规范 -->
                <tx:method name="save*" propagation="REQUIRED" />
                <tx:method name="delete*" propagation="REQUIRED" />
                <tx:method name="update*" propagation="REQUIRED" />
                <tx:method name="insert*" propagation="REQUIRED" />
                <tx:method name="select*" propagation="SUPPORTS" />
                <tx:method name="find*" propagation="SUPPORTS"/>
                <tx:method name="get*" propagation="SUPPORTS"/>
                <tx:method name="select*" propagation="SUPPORTS"/>
    <!--             <tx:method name="get*" propagation="REQUIRED"  read-only="true" /> -->
            </tx:attributes>
        </tx:advice>
        <!-- 配置事务切入点 -->
        <aop:config>
            <aop:advisor advice-ref="txAdvice"
                pointcut="execution(* lt.ssm.service.*.*(..))" />
        </aop:config>
    </beans>

    如果tx:method中没有注明的方法在service中出现,name不会为该方法添加事务,不过一般不这么做

     如果出现了上述界面,则事务属性配置出现了问题

    使用原生的mybatis执行数据库操作

        private SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(OrdersCustomTest.class.getClassLoader().getResourceAsStream("mybatis-config.xml"));
        @Test
        public void testFindOrdersUser() throws Exception {
            SqlSession sqlSession = sessionFactory.openSession();
            OrdersCustom mapper = sqlSession.getMapper(OrdersCustom.class);
            List<OrdersCustom> findOrdersUser = mapper.findOrdersUser();
            System.out.println(findOrdersUser);
            sqlSession.commit();
            sqlSession.close();
        }

    一般需要的步骤

    1.通过mybatis.xml配置文件创建sessionFactory

    2.通过sessionFactory创建sqlSession

    3.通过sqlSession获取动态mapper(动态代理)

    4.执行sql

    5.提交事务(sqlSession在执行cud的时候回自动开启事务,select不需要提交)

    6.关闭连接

    //如果sqlsessionFactory.opensession(true)参数是布尔值,如果设置为true,就不需要commit提交事务了

    SqlSession sqlSession = sqlSessionFactory.openSession();//openSession打开会话,事务处理开始

    以上过程中第开启事务,提交,回滚事务,关闭连接都可以使用aop动态代理自动完成,创建sessionfactory和sqlSession可以由spring注入,所以需要做的工作只有在服务层中调用相关dao(mapper)方法,并为方法其添加相应通知.因此所有涉及到cud方法都要为其添加事务,查询方法不用开启事务

    使用spring将动态代理产生的mapper注入到ioc中

    这里没有关闭sqlSession(这里我也不清楚,到底是在动态代理执行close,还是在其他地方关闭)

     

  • 相关阅读:
    cordova 里js和oc通信原理
    awakeFromNib
    CALayer树形结构
    SDWebImage原理,转载
    gcd多线程
    block的存储形态
    关于weak对象什么时候释放
    Swift是否和OC一样有runtime机制
    大数据基础
    python进行离线打包并安装
  • 原文地址:https://www.cnblogs.com/lt123/p/7568068.html
Copyright © 2011-2022 走看看