一、概述
?????????聲明式事務(declarative transaction management)是Spring提供的對程序事務管理的一種方式,Spring的聲明式事務顧名思義就是采用聲明的方式來處理事務。這里所說的聲明,是指在配置文件中聲明,用在Spring配置文件中聲明式的處理事務來代替業務邏輯中使用代碼處理事務,這樣做的好處是:事務管理不侵入開發的組件,具體來說就是業務邏輯對象不會意識到自己正在事務管理之中,事實上也應該如此,因為事務管理是屬于系統層面的服務,而不是業務邏輯的一部分,如果想要改變事務管理策略的話,也只需要在配置文件中修改配置即可,在不需要事務管理的時候,修改一下配置信息,即可移除事務管理服務,無需改變代碼重新編譯,這樣維護起來也很方便,Spring中是使用aop來完成聲明式事務管理的,因而聲明式事務是以方法為單位的,也即聲明式事務是作用在業務方法上的。
二、聲明式事務(xml方式)環境搭建
2.1、項目概覽
2.2、配置思路
第一步:配置事務管理器;
第二步:配置事務要處理的方法;注意事項:一旦配置了事務方法名稱規則后,service中的方法一定要按照這里配置的名稱,否則事務配置不生效;
第三步:配置aop;
2.3、pom.xml
同 系列四 pom.xml?
2.4、applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 組件掃描 --><context:component-scan base-package="org.star"/><!-- 數據源 --><context:property-placeholder location="db.properties"/><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${db.driver}"/><property name="url" value="${db.url}"/><property name="username" value="${db.username}"/><property name="password" value="${db.password}"/></bean><!--配置sqlSessionFactory:讀取配置文件,獲取數據庫相關的信息--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="typeAliasesPackage" value="org.star.entity.model"></property><property name="mapperLocations" value="classpath:mapper/*.xml"></property><property name="configuration"><bean class="org.apache.ibatis.session.Configuration"><property name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl"></property></bean></property><!-- 分頁插件 --><property name="plugins"><array><bean class="com.github.pagehelper.PageInterceptor"><property name="properties"><!--reasonable=true解釋:如果用戶輸入的不合理的pageSize參數,pageHelper會自動進行調整--><value>helperDialect=mysqlreasonable=true</value></property></bean></array></property></bean><!--配置mapper接口的位置,并指定sqlSessionFactorysqlSession = sqlSessionFactory.openSession();mapper = sqlSession.getMapper();--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="org.star.mapper"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean><!-- 配置事務管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--聲明式事務配置:spring中的事務是基于aop實現,但使用的不是通知而是攔截器--><tx:advice id="txAdvice" transaction-manager="transactionManager"><!-- 定義事務的對象信息 --><tx:attributes><!-- transfer*:表示以transfer開頭的所有方法 --><tx:method name="transfer*" propagation="REQUIRED" isolation="DEFAULT"/></tx:attributes></tx:advice><!-- aop配置 --><aop:config><aop:pointcut id="txPointCut" expression="execution(* org.star.service.*.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"></aop:advisor></aop:config></beans>
2.5、db.properties
同 系列四 db.properties?
2.6、AccountDO.java
同 系列四 AccountDO.java?
2.7、AccountMapper.java
同 系列四 AccountMapper.java
2.8、AccountMapper.xml
同 系列四 AccountMapper.xml
2.9、AccountService.java
同 系列四 AccountService.java
2.10、AccountServiceImpl.java
/*** @Author : 一葉浮萍歸大海* @Date: 2023/11/24 8:25* @Description:*/
@Service
public class AccountServiceImpl implements AccountService {@Resourceprivate AccountMapper accountMapper;/*** 轉賬** @return*/@Overridepublic void transferMoney() {try {// Jack 轉出100元accountMapper.accountExpenditure("Jack", 100);// 模擬異常int i = 10 /0;// Rose 入賬100元accountMapper.accountEntry("Rose", 100);} catch (Exception e) {throw new RuntimeException(e);}}
}
2.11、SpringJunitTest.java
同 系列四 SpringJunitTest.java
?