Spring中基于xml的聲明式事務(wù)示例詳解
15.1、環(huán)境搭建
創(chuàng)建名為spring_transaction_xml的新module,過(guò)程參考13.1節(jié)
15.1.1、配置打包方式和依賴

注意:比起基于注解的聲明式事務(wù),基于xml的聲明式事務(wù)還需要額外引入spring-AOP的依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.rain</groupId>
<artifactId>spring_transaction_xml</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!-- Spring IOC相關(guān) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.1</version>
</dependency>
<!-- Spring 持久化層相關(guān) -->
<!-- Spring 在執(zhí)行持久化層操作、與持久化層技術(shù)進(jìn)行整合過(guò)程中,需要使用orm、jdbc、tx三個(gè)jar包 -->
<!-- 導(dǎo)入 orm 包就可以通過(guò) Maven 的依賴傳遞性把其他兩個(gè)也導(dǎo)入 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.1</version>
</dependency>
<!-- Spring 測(cè)試相關(guān) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.1</version>
</dependency>
<!-- spring-AOP的依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.1</version>
</dependency>
<!-- junit測(cè)試 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- MySQL驅(qū)動(dòng) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<!-- 數(shù)據(jù)源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
</dependencies>
</project>15.1.2、創(chuàng)建jdbc.properties文件

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8 jdbc.username=root jdbc.password=root
15.1.3、創(chuàng)建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:context="http://www.springframework.org/schema/context"
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">
<!-- 導(dǎo)入外部屬性文件 -->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<!-- 配置數(shù)據(jù)源 -->
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
<!--通過(guò)${key}的方式訪問(wèn)外部屬性文件的value-->
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置 JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 裝配數(shù)據(jù)源 -->
<property name="dataSource" ref="datasource"></property>
</bean>
</beans>15.1.4、創(chuàng)建持久層接口BookDao及其實(shí)現(xiàn)類(lèi)

package org.rain.spring.dao;
/**
* @author liaojy
* @date 2023/9/3 - 17:34
*/
public interface BookDao {
/**
* 查詢圖書(shū)的價(jià)格
* @param bookId
* @return
*/
Integer getPriceByBookId(Integer bookId);
/**
* 更新圖書(shū)的庫(kù)存
* @param bookId
*/
void updateStockOfBook(Integer bookId);
/**
* 更新用戶的余額
* @param userId
* @param price
*/
void updateBalanceOfUser(Integer userId,Integer price);
}
package org.rain.spring.dao.impl;
import org.rain.spring.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
/**
* @author liaojy
* @date 2023/9/3 - 17:36
*/
@Repository
public class BookDaoImpl implements BookDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public Integer getPriceByBookId(Integer bookId) {
String sql = "select price from t_book where book_id = ?";
Integer price = jdbcTemplate.queryForObject(sql, Integer.class,bookId);
return price;
}
public void updateStockOfBook(Integer bookId) {
String sql = "update t_book set stock = stock -1 where book_id = ?";
jdbcTemplate.update(sql, bookId);
}
public void updateBalanceOfUser(Integer userId, Integer price) {
String sql = "update t_user set balance = balance - ? where user_id = ?";
jdbcTemplate.update(sql,price,userId);
}
}15.1.5、創(chuàng)建業(yè)務(wù)層接口BookService及其實(shí)現(xiàn)類(lèi)

package org.rain.spring.service;
/**
* @author liaojy
* @date 2023/9/3 - 17:52
*/
public interface BookService {
void buyBook(Integer bookId,Integer userId);
}
package org.rain.spring.service.impl;
import org.rain.spring.dao.BookDao;
import org.rain.spring.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author liaojy
* @date 2023/9/3 - 17:53
*/
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
public void buyBook(Integer bookId, Integer userId) {
//查詢圖書(shū)的價(jià)格
Integer price = bookDao.getPriceByBookId(bookId);
//更新圖書(shū)的庫(kù)存
bookDao.updateStockOfBook(bookId);
//更新用戶的余額
bookDao.updateBalanceOfUser(userId,price);
}
}15.1.6、創(chuàng)建控制層BookController

注意:因?yàn)榭刂茖記](méi)用到接口,所以方法的訪問(wèn)修飾符要手動(dòng)設(shè)置
package org.rain.spring.controller;
import org.rain.spring.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
* @author liaojy
* @date 2023/9/3 - 17:54
*/
@Controller
public class BookController {
@Autowired
private BookService bookService;
public void buyBook(Integer bookId, Integer userId){
bookService.buyBook(bookId,userId);
}
}15.1.7、配置對(duì)注解組件的掃描

<!--掃描注解組件-->
<context:component-scan base-package="org.rain.spring"></context:component-scan>15.2、基于xml事務(wù)的實(shí)現(xiàn)
15.2.1、配置事務(wù)管理器

<!--配置事務(wù)管理器-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 裝配要進(jìn)行事務(wù)管理的數(shù)據(jù)源 -->
<property name="dataSource" ref="datasource"></property>
</bean>15.2.2、配置事務(wù)通知

注意:tx:advice標(biāo)簽導(dǎo)入的名稱(chēng)空間需要 tx 結(jié)尾的那個(gè)
<!--
tx:advice標(biāo)簽:配置事務(wù)通知
id屬性:給事務(wù)通知標(biāo)簽設(shè)置唯一標(biāo)識(shí),便于引用
transaction-manager屬性:通過(guò)id引用對(duì)應(yīng)的事務(wù)管理器
-->
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager"></tx:advice>15.2.3、配置事務(wù)通知作用到連接點(diǎn)

<aop:config>
<!--
通過(guò)切入點(diǎn)表達(dá)式,將事務(wù)通知作用到連接點(diǎn)
-->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* org.rain.spring.service.impl.*.*(..))"></aop:advisor>
</aop:config>15.2.4、配置事務(wù)通知的屬性

注意:不使用tx:method標(biāo)簽指定的方法是不會(huì)使用事務(wù)的,為了讓切入點(diǎn)表達(dá)式的所有方法都使用到事務(wù),可以使用*通配符:<tx:method name="*"/>
<tx:attributes>
<!--
tx:method標(biāo)簽:配置使用事務(wù)的方法
name屬性:指定用事務(wù)的方法名,可以使用星號(hào)代表多個(gè)字符
-->
<tx:method name="buyBook"/>
</tx:attributes>15.3、測(cè)試事務(wù)的效果
15.3.1、創(chuàng)建測(cè)試類(lèi)
模擬場(chǎng)景:
- 用戶購(gòu)買(mǎi)圖書(shū),先查詢圖書(shū)的價(jià)格,再更新圖書(shū)的庫(kù)存和用戶的余額
- 假設(shè)id為1的用戶(余額為50),購(gòu)買(mǎi)id為1的圖書(shū)(價(jià)格為80)
- 購(gòu)買(mǎi)圖書(shū)之后,用戶的余額應(yīng)為-30;但由于數(shù)據(jù)庫(kù)中余額字段設(shè)置了無(wú)符號(hào),因此無(wú)法將-30插入到余額字段;此時(shí)執(zhí)行更新用戶余額的sql語(yǔ)句會(huì)拋出異常

package org.rain.spring.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.rain.spring.controller.BookController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author liaojy
* @date 2023/9/4 - 0:20
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-tx-xml.xml")
public class TxByXml {
@Autowired
private BookController bookController;
@Test
public void testBuyBook(){
bookController.buyBook(1,1);
}
}15.3.2、執(zhí)行前的數(shù)據(jù)

此時(shí)id為1的圖書(shū)庫(kù)存為100

此時(shí)id為1的用戶余額為50
15.3.3、執(zhí)行時(shí)的異常

15.3.3、執(zhí)行后的數(shù)據(jù)

由于使用了Spring的聲明式事務(wù),更新(圖書(shū))庫(kù)存和更新(用戶)余額,要么都成功,要么都失?。?/p>
本例屬于都失敗,所以(圖書(shū))庫(kù)存和(用戶)余額都沒(méi)有變化

15.4、配置事務(wù)的屬性

事務(wù)屬性的詳細(xì)作用,請(qǐng)參考14.5節(jié)
<!-- rollback-for屬性:設(shè)置回滾的異常 --> <!-- no-rollback-for屬性:設(shè)置不回滾的異常 --> <!-- isolation屬性:設(shè)置事務(wù)的隔離級(jí)別 --> <!-- timeout屬性:設(shè)置事務(wù)的超時(shí)屬性 --> <!-- propagation屬性:設(shè)置事務(wù)的傳播行為 --> <tx:method name="save*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRES_NEW"/> <tx:method name="update*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRES_NEW"/> <tx:method name="delete*" read-only="false" rollback-for="java.lang.Exception" propagation="REQUIRES_NEW"/>
到此這篇關(guān)于Spring之基于xml的聲明式事務(wù)的文章就介紹到這了,更多相關(guān)spring 聲明式事務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MybatisPlus使用Wrapper實(shí)現(xiàn)條件查詢功能
這篇文章主要介紹了MybatisPlus使用Wrapper實(shí)現(xiàn)查詢功能,使用它可以實(shí)現(xiàn)很多復(fù)雜的查詢,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
Java 和 Kotlin Lambda 表達(dá)式示例詳解
Lambda 表達(dá)式是一種簡(jiǎn)潔的函數(shù)表達(dá)方式,可以把函數(shù)作為一個(gè)方法的參數(shù),或者將代碼塊轉(zhuǎn)換為數(shù)據(jù)傳遞,這篇文章主要介紹了Java 和 Kotlin Lambda 表達(dá)式示例詳解,需要的朋友可以參考下2024-06-06
java實(shí)現(xiàn)PPT轉(zhuǎn)化為PDF
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)PPT轉(zhuǎn)化為PDF的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
關(guān)于MyBatis中映射對(duì)象關(guān)系的舉例
這篇文章主要介紹了關(guān)于MyBatis中映射對(duì)象關(guān)系的舉例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Spring Security Oauth2.0 實(shí)現(xiàn)短信驗(yàn)證碼登錄示例
本篇文章主要介紹了Spring Security Oauth2.0 實(shí)現(xiàn)短信驗(yàn)證碼登錄示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
spring?cloud?eureka?服務(wù)啟動(dòng)失敗的原因分析及解決方法
這篇文章主要介紹了spring?cloud?eureka?服務(wù)啟動(dòng)失敗的原因解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
Java多線程之同步工具類(lèi)CountDownLatch
這篇文章主要介紹了Java多線程之同步工具類(lèi)CountDownLatch,CountDownLatch是一個(gè)同步工具類(lèi),它允許一個(gè)或多個(gè)線程一直等待,直到其他線程執(zhí)行完后再執(zhí)行。例如,應(yīng)用程序的主線程希望在負(fù)責(zé)啟動(dòng)框架服務(wù)的線程已經(jīng)啟動(dòng)所有框架服務(wù)之后執(zhí)行,下面一起來(lái)學(xué)習(xí)學(xué)習(xí)內(nèi)容吧2021-10-10

