15、Spring之基于xml的声明式事务
阅读本文前,建议先阅读Spring之基于注解的声明式事务15.1、环境搭建
创建名为spring_transaction_xml的新module,过程参考13.1节
15.1.1、配置打包方式和依赖
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230901081051610-591777281.png
注意:比起基于注解的声明式事务,基于xml的声明式事务还需要额外引入spring-AOP的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
<context:component-scan base-package="org.rain.spring"></context:component-scan> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<context:component-scan base-package="org.rain.spring"></context:component-scan> 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>
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan><dependency>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <groupId>org.springframework</groupId>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <artifactId>spring-context</artifactId>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <version>5.3.1</version>
<context:component-scan base-package="org.rain.spring"></context:component-scan></dependency>
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan><dependency>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <groupId>org.springframework</groupId>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <artifactId>spring-orm</artifactId>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <version>5.3.1</version>
<context:component-scan base-package="org.rain.spring"></context:component-scan></dependency>
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan><dependency>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <groupId>org.springframework</groupId>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <artifactId>spring-test</artifactId>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <version>5.3.1</version>
<context:component-scan base-package="org.rain.spring"></context:component-scan></dependency>
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan><dependency>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <groupId>org.springframework</groupId>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <artifactId>spring-aspects</artifactId>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <version>5.3.1</version>
<context:component-scan base-package="org.rain.spring"></context:component-scan></dependency>
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan><dependency>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <groupId>junit</groupId>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <artifactId>junit</artifactId>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <version>4.12</version>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <scope>test</scope>
<context:component-scan base-package="org.rain.spring"></context:component-scan></dependency>
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan><dependency>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <groupId>mysql</groupId>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <artifactId>mysql-connector-java</artifactId>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <version>5.1.49</version>
<context:component-scan base-package="org.rain.spring"></context:component-scan></dependency>
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan><dependency>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <groupId>com.alibaba</groupId>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <artifactId>druid</artifactId>
<context:component-scan base-package="org.rain.spring"></context:component-scan> <version>1.0.31</version>
<context:component-scan base-package="org.rain.spring"></context:component-scan></dependency>
</dependencies>
</project>15.1.2、创建jdbc.properties文件
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230903171627392-1562992818.png
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root15.1.3、创建Spring的配置文件
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230903172910397-318368728.png
<?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">
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
<bean id="datasource" >
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan><property name="driverClassName" value="${jdbc.driver}"></property>
<context:component-scan base-package="org.rain.spring"></context:component-scan><property name="url" value="${jdbc.url}"></property>
<context:component-scan base-package="org.rain.spring"></context:component-scan><property name="username" value="${jdbc.username}"></property>
<context:component-scan base-package="org.rain.spring"></context:component-scan><property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="jdbcTemplate" >
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan><property name="dataSource" ref="datasource"></property>
</bean>
</beans>15.1.4、创建持久层接口BookDao及其实现类
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230903173539003-26352100.png
package org.rain.spring.dao;
/**
* @author liaojy
* @date 2023/9/3 - 17:34
*/
public interface BookDao {
/**
* 查询图书的价格
* @param bookId
* @return
*/
Integer getPriceByBookId(Integer bookId);
/**
* 更新图书的库存
* @param bookId
*/
void updateStockOfBook(Integer bookId);
/**
* 更新用户的余额
* @param userId
* @param price
*/
void updateBalanceOfUser(Integer userId,Integer price);
}https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230903175121243-793299678.png
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) {
<context:component-scan base-package="org.rain.spring"></context:component-scan>String sql = "select price from t_book where book_id = ?";
<context:component-scan base-package="org.rain.spring"></context:component-scan>Integer price = jdbcTemplate.queryForObject(sql, Integer.class,bookId);
<context:component-scan base-package="org.rain.spring"></context:component-scan>return price;
}
public void updateStockOfBook(Integer bookId) {
<context:component-scan base-package="org.rain.spring"></context:component-scan>String sql = "update t_book set stock = stock -1 where book_id = ?";
<context:component-scan base-package="org.rain.spring"></context:component-scan>jdbcTemplate.update(sql, bookId);
}
public void updateBalanceOfUser(Integer userId, Integer price) {
<context:component-scan base-package="org.rain.spring"></context:component-scan>String sql = "update t_user set balance = balance - ? where user_id = ?";
<context:component-scan base-package="org.rain.spring"></context:component-scan>jdbcTemplate.update(sql,price,userId);
}
}15.1.5、创建业务层接口BookService及其实现类
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230903175251256-1608396360.png
package org.rain.spring.service;
/**
* @author liaojy
* @date 2023/9/3 - 17:52
*/
public interface BookService {
void buyBook(Integer bookId,Integer userId);
}https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230903175406227-1975179397.png
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) {
<context:component-scan base-package="org.rain.spring"></context:component-scan>//查询图书的价格
<context:component-scan base-package="org.rain.spring"></context:component-scan>Integer price = bookDao.getPriceByBookId(bookId);
<context:component-scan base-package="org.rain.spring"></context:component-scan>//更新图书的库存
<context:component-scan base-package="org.rain.spring"></context:component-scan>bookDao.updateStockOfBook(bookId);
<context:component-scan base-package="org.rain.spring"></context:component-scan>//更新用户的余额
<context:component-scan base-package="org.rain.spring"></context:component-scan>bookDao.updateBalanceOfUser(userId,price);
}
}15.1.6、创建控制层BookController
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230903175621917-28255730.png
注意:因为控制层没用到接口,所以方法的访问修饰符要手动设置
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){
<context:component-scan base-package="org.rain.spring"></context:component-scan>bookService.buyBook(bookId,userId);
}
}15.1.7、配置对注解组件的扫描
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230903175835316-1121838344.png
<context:component-scan base-package="org.rain.spring"></context:component-scan>15.2、基于xml事务的实现
15.2.1、配置事务管理器
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230903181053326-1492298040.png
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan>15.2.2、配置事务通知
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230903223814533-729043351.png
注意:tx:advice标签导入的名称空间需要 tx 结尾的那个
<context:component-scan base-package="org.rain.spring"></context:component-scan>15.2.3、配置事务通知作用到连接点
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230903224810104-32506783.png
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan>15.2.4、配置事务通知的属性
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230903225656134-984980302.png
注意:不使用tx:method标签指定的方法是不会使用事务的,为了让切入点表达式的所有方法都使用到事务,可以使用*通配符:
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan>
<context:component-scan base-package="org.rain.spring"></context:component-scan>15.3、测试事务的效果
15.3.1、创建测试类
模拟场景:
[*]用户购买图书,先查询图书的价格,再更新图书的库存和用户的余额
[*]假设id为1的用户(余额为50),购买id为1的图书(价格为80)
[*]购买图书之后,用户的余额应为-30;但由于数据库中余额字段设置了无符号,因此无法将-30插入到余额字段;
此时执行更新用户余额的sql语句会抛出异常
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230904002225920-640796628.png
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(){
<context:component-scan base-package="org.rain.spring"></context:component-scan>bookController.buyBook(1,1); }}15.3.2、执行前的数据
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230904002749022-585498797.png
此时id为1的图书库存为100
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230904002809261-2027852567.png
此时id为1的用户余额为50
15.3.3、执行时的异常
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230904003124688-1086405931.png
15.3.3、执行后的数据
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230904003213877-1681838744.png
由于使用了Spring的声明式事务,更新(图书)库存和更新(用户)余额,要么都成功,要么都失败;
本例属于都失败,所以(图书)库存和(用户)余额都没有变化
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230904003231557-1855100103.png
15.4、配置事务的属性
https://img2023.cnblogs.com/blog/2052479/202309/2052479-20230904010628305-1318337605.png
事务属性的详细作用,请参考14.5节
<context:component-scan base-package="org.rain.spring"></context:component-scan>
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]