ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring-mybatis 事务

2022-04-26 16:01:28  阅读:131  来源: 互联网

标签:事务 Spring 如果 以非 当前 mybatis 执行


事务的ACID原则

原子性、一致性、隔离性,持久性

Spring-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/context http://www.springframework.org/schema/context/spring-context.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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;useSSL=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>

    <!-- 创建SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--绑定mybatis配置文件-->
        <!--是configLocation,不是configuration -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>

    <!--SqlSessionTemplate就是我們使用的sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--只能用构造器注入sqlSessionFactory,应为他没有set方法-->
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--结合aop实现事务的切入-->
    <!--配置事务的通知-->
<tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
    <!--给方法配置事务-->
    <!--配置事务的传播特性,propagation有七种 默认:REQUIRED,没有事务就新建事务-->
    <tx:attributes>
        <tx:method name="query" read-only="true"/>
        <tx:method name="add" propagation="REQUIRED"/>
        <tx:method name="delete" propagation="REQUIRED"/>
        <tx:method name="update" propagation="REQUIRED"/>
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>

    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.liu.mapper.*.*(..))"/>
        <aop:advisor advice-ref="transactionInterceptor" pointcut-ref="txPointcut"/>
    </aop:config>

</beans>

propagation的7种策略

REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 required
SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。 supports
MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。 mandatory
REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。 requires_new
NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 not_supported
NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。 never
NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。

标签:事务,Spring,如果,以非,当前,mybatis,执行
来源: https://www.cnblogs.com/123xian/p/16195122.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有