Spring 事务
01. 编程式事务控制相关的对象
>> PlatformTransactionManager
* PlatformTransactionManager 是个接口; 是Spring的事务管理器;
* 定义事务的行为,针对不同的持久层技术,提供不同的实现;
> 如 Dao层使用 jdbc 或 mybatis 时, org.springframework.jdbc.datasource.DataSourceTransactionManager
> 如 Dao层使用 hibernate 时, org.springframework.orm.hibernate5.HibernateTransactionManager
* 常用的事务方法
>> TransactionDefinition
* 事务的定义信息对象
* 常用方法
> 事务隔离级别:
> 事务传播行为
>> TransactionStatus
* 提供事务具体的运行状态
* 常用方法如下
02. 声明式事务
>> 指在配置文件中进行配置,实现事务控制;
>> Spring 声明式事务控制,底层就是AOP
>> XML配置声明式事务的实现 > applicationContext.xml 中的配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 10 "> 11 12 <!--数据源--> 13 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> 14 <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 15 <property name="url" value="jdbc:mysql://localhost:3306/test"/> 16 <property name="username" value="root"/> 17 <property name="password" value="123456"/> 18 </bean> 19 <!--jdbc--> 20 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 21 <property name="dataSource" ref="dataSource"/> 22 </bean> 23 <!--dao--> 24 <bean id="accountDao" class="club.wuyu.dao.impl.AccountDaoImpl"> 25 <property name="jdbcTemplate" ref="jdbcTemplate"/> 26 </bean> 27 28 <!--目标对象 内部的方法就是切点--> 29 <bean id="accountService" class="club.wuyu.service.impl.AccountServiceImpl"> 30 <property name="accountDao" ref="accountDao"/> 31 </bean> 32 33 <!--以下就是声明式事务的实现配置--> 34 35 <!--01.配置平台事务管理器 jdbc/mybatis 使用的是 datasource事务管理器--> 36 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 37 <property name="dataSource" ref="dataSource"/> 38 </bean> 39 40 <!--02.通知 事务的增强 advice是专用于事务的aspect--> 41 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 42 <!--设置事务的属性信息的--> 43 <tx:attributes> 44 <!--可独立为某个方法进行声明--> 45 <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> 46 <tx:method name="save" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/> 47 <tx:method name="findAll" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/> 48 <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/> 49 <!--也可统一配置声明,未独立配置的方法将使用这个统一的配置--> 50 <tx:method name="*"/> 51 </tx:attributes> 52 </tx:advice> 53 54 <!--配置事务的aop织入--> 55 <aop:config> 56 <aop:pointcut id="txPointcut" expression="execution(* club.wuyu.service.impl.*.*(..))"/> 57 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> 58 </aop:config> 59 60 </beans>
>> 注解方式的实现
> 上面代码中,红色部分,均可通过注解的方式来配置
> 当Bean对象,使用注解时,需要在xml配置文件中,添加上组件扫描配置
<context:component-scan base-package="club.wuyu"/>
> 当事务增强配置使用注解时,需要在XML配置文件中,添加上事务的注解驱动
<tx:annotation-driven transaction-manager="transactionManager"/>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类