当前位置: 首页 > news >正文

Spring声明式事务

1.概念 

  • 事务就是用户定义的一系列执行SQL语句的操作, 这些操作要么完全地执行,要么完全地都不执行, 它是一个不可分割的工作执行单元
  • 一个使用Mybatis-Spring的主要原因是它允许Mybatis参与到Spring的事务管理中,而不是给Mybatis创建一个新的专用事务管理器 

2.步骤

2.1.spring配置文件
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"><!--Spring整合Mybatis,省略了Mybatis的核心配置文件,转而在Spring的配置文件中配置Mybatis--><!--datasource--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/user?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/><property name="username" value="root"/><property name="password" value="123456"/></bean><!--sqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" />
<!--        <property name="configLocation" value="classpath:mybatis-config.xml"/>--><property name="mapperLocations" value="classpath:com/sun/mapper/*.xml"/></bean><!--SqlSessionTemplate:就是我们使用的sqlSession,这个是spring提供的--><bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"><!--我们只能使用构造器注入,因为没有set方法--><constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/></bean><bean id="UserMapperImpl" class="com.sun.mapper.UserMapperImpl"><property name="sqlSessionTemplate" ref="sqlSessionTemplate"/></bean>
</beans>
 2.2.配置声明式事务
<!--配置声明式事务-->
<bean id="transationManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/>
</bean>
2.3.用AOP的方式实现事务
    <!--结合AOP实现事务的织入--><!--第一步:配置事务的通知--><tx:advice id="txAdvice" transaction-manager="transationManage"><tx:attributes><tx:method name="*" propagation="REQUIRED"/></tx:attributes></tx:advice>
2.4.配置事务切入
    <aop:config><aop:pointcut id="PointCut" expression="execution(* com.sun.mapper.*.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="PointCut"/></aop:config>

http://www.lryc.cn/news/292326.html

相关文章:

  • PyTorch深度学习实战(34)——Pix2Pix详解与实现
  • 第96讲:MySQL高可用集群MHA的核心概念以及集群搭建
  • 外星人入侵(python)
  • Unity中开发程序打包发布
  • 2024.2.1日总结
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组
  • STM32目录结构
  • 算法专题:记忆搜索
  • 【数据分享】1929-2023年全球站点的逐日最低气温数据(Shp\Excel\免费获取)
  • 2024美赛数学建模D题思路+模型+代码+论文(持续更新)
  • dubbo+sentinel最简集成实例
  • 9.2爬楼梯(LC70-E)
  • Asp.net移除Server, X-Powered-By, 和X-AspNet-Version头
  • reactnative 调用原生ui组件
  • 面试手写第五期
  • 【CSS】css选择器和css获取第n个元素(:nth-of-type(n)、:nth-child(n)、first-child和last-child)
  • 解析Excel文件内容,按每列首行元素名打印出某个字符串的统计占比(超详细)
  • qt中遇到[Makfile.Debug:119:debug/app.res.o] Error 1的原因以及解决方法
  • pytorch调用gpu训练的流程以及示例
  • 学习Android的第一天
  • 回归预测 | Matlab实现CPO-LSTM【24年新算法】冠豪猪优化长短期记忆神经网络多变量回归预测
  • Typora导出html文件图片自动转换成base64
  • 『C++成长记』string使用指南
  • 硬件连通性测试:构建数字世界的无形基石
  • mysql的安装与卸载
  • 假期作业 2.2
  • 运维SRE-02 正则表达式、grep
  • 【SpringCloud】使用OpenFeign进行微服务化改造
  • DRV8313和L298N都是电机驱动,一个是驱动三相FOC无刷直流电机的,一个是驱动有刷电机,使stm32控制无刷电机简单入门知识
  • React16源码: React中event事件系统初始化源码实现