Spring声明式事务
Spring声明式事务
概述
本文主要讲述如何配置声明式事务
本篇博客讲述通过在配置文件中以aop的方式将事务配置到函数中去实现了在对代码没有修改的情况下将事务功能添加了上去,在delete方法失败的时候添加的数据也会回滚
背景
基本功能:对User类进行增删改查
pom.xml
<?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">
<parent>
<artifactId>SpringStudy1</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>SpringStudy-10-transaction</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<typeAlias type="com.kuang.pojo.User" alias="User"></typeAlias>
</typeAliases>
</configuration>
application-source.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost:3306/test?useSSL=true&useUnicode=true&characterEncoding=UTF8"></property>
<property name="username" value="root"></property>
<property name="password" value="xxx"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="mybatis-config.xml"></property>
<property name="mapperLocations" value="com/kuang/dao/UserMapper.xml"></property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean>
</beans>
application-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="application-source.xml"></import>
<bean id="userMapper" class="com.kuang.dao.UserMapperImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
User.java
package com.kuang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 功能描述
*
* @since 2022-07-28
*/
@Data @AllArgsConstructor @NoArgsConstructor public class User {
private int id;
private String name;
private String pwd;
}
UserMapper.java
package com.kuang.dao;
import com.kuang.pojo.User;
import java.util.List;
/**
* 功能描述
*
* @since 2022-07-28
*/
public interface UserMapper {
List<User> getUserList();
int addUser(User user);
int delete(int id);
}
UserMapperImpl.java
package com.kuang.dao;
import com.kuang.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
/**
* 功能描述
*
* @since 2022-07-31
*/
public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper {
@Override public List<User> getUserList() {
int i = addUser(new User(6, "yiyi", "123"));
int delete = delete(5);
return getSqlSession().getMapper(UserMapper.class).getUserList();
}
@Override public int addUser(User user) {
return getSqlSession().getMapper(UserMapper.class).addUser(user);
}
@Override public int delete(int id) {
return getSqlSession().getMapper(UserMapper.class).delete(id);
}
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.UserMapper">
<select id="getUserList" resultType="user">
select *
from `user`
</select>
<insert id="addUser" parameterType="user">
insert into user (id, name, pwd)
values (#{id}, #{name}, #{pwd})
</insert>
<delete id="delete" parameterType="_int">
deletes
from user
where id = #{id}
</delete>
</mapper>
MyTest.java
import com.kuang.dao.UserMapper;
import com.kuang.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* 功能描述
*
* @since 2022-07-31
*/
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
UserMapper mapper = context.getBean("userMapper", UserMapper.class);
List<User> users = mapper.getUserList();
for (User user : users) {
System.out.println(user);
}
}
}
添加声明式事务
上面的例子中UserMapper.xml中delete方法的sql我们故意写错,写成deletes,来测试数据库中添加数据的功能是否会回滚,运行上面的测试,可以发现数据库中的数据被添加成功,delete方法执行失败,添加声明式事务之后会发现数据会被回滚
application-source.xml
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="delete" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="query" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txPoint" expression="execution(* com.kuang.dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"></aop:advisor>
</aop:config>
总结
我们可以看到,transactionManager这个bean是在配置声明式事务,txAdvice这个通知是结合aop实现事务的织入,除query方法所有的方法都执行spring默认的事务管理机制REQUIRED,即如果没有默认事务,则创建一个,query方法对数据库的操作是只能读取的。最后对com.kuang.dao下面所有文件的所有方法都切入这个事务通知