一、环境
1、SQL:8.0.23
2、spring+mybatis
3、表
CREATE TABLE `account` (
`money` DOUBLE NULL DEFAULT NULL,
`name` VARCHAR(50) NULL DEFAULT NULL
)
COLLATE='utf8mb4_0900_ai_ci'
ENGINE=InnoDB
;
二、依赖
<?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.example</groupId>
<artifactId>spring-transaction</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
</project>
三、代码
1、domain包
import java.io.Serializable;
/**
* @Classname Account
* @Description TODO
* @Date 2021/8/17 22:57
* @Created by 张斌
*/
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
2、dao层
import org.apache.ibatis.annotations.Param;
/**
* @Classname AccountDao
* @Description TODO
* @Date 2021/8/17 23:00
* @Created by 张斌
*/
public interface AccountDao {
/**
* 入账操作
* @param name 入账用户名
* @param money 入账金额
*/
void inMoney(@Param("name") String name, @Param("money") Double money);
/**
* 出账操作
* @param name 出账用户名
* @param money 出账金额
*/
void outMoney(@Param("name") String name, @Param("money") Double money);
}
3、service层
/**
* @Classname AccountService
* @Description TODO
* @Date 2021/8/17 23:01
* @Created by 张斌
*/
public interface AccountService {
/**
* 转账操作
* @param outName 出账用户名
* @param inName 入账用户名
* @param money 转账金额
*/
public void transfer(String outName, String inName, Double money);
}
service实现类
import com.riant.dao.AccountDao;
import com.riant.service.AccountService;
/**
* @Classname AccountServiceImpl
* @Description TODO
* @Date 2021/8/17 23:01
* @Created by 张斌
*/
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void transfer(String outName, String inName, Double money) {
/**
* 业务操作
*/
accountDao.inMoney(outName,money);
//int i = 1/0;
accountDao.outMoney(inName,money);
}
}
4、切面
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import javax.sql.DataSource;
/**
* @Classname TxAdvice
* @Description TODO
* @Date 2021/8/17 23:03
* @Created by 张斌
*/
public class TxAdvice {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public Object transtructionManager(ProceedingJoinPoint pjp) throws Throwable {
// 开启事务
PlatformTransactionManager ptm = new DataSourceTransactionManager(dataSource);
// 事务定义
TransactionDefinition td = new DefaultTransactionDefinition();
// 事务状态
TransactionStatus ts = ptm.getTransaction(td);
// 原始方法调用
Object ret = pjp.proceed(pjp.getArgs());
// 提交事务
ptm.commit(ts);
return ret;
}
}
5、映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.riant.dao.AccountDao">
<update id="inMoney">
update account set money = money + #{money} where name = #{name}
</update>
<update id="outMoney">
update account set money = money - #{money} where name = #{name}
</update>
</mapper>
6、数据库配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.0.12:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
jdbc.username=riant
jdbc.password=root
7、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"
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/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="classpath:*.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="accountService" class="com.riant.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
<!--<property name="dataSource" ref="dataSource"/>-->
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.riant.domain.Account"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.riant.dao"/>
</bean>
<!--###########################################-->
<bean id="txAdvice" class="com.riant.aop.TxAdvice">
<property name="dataSource" ref="dataSource"/>
</bean>
<aop:config>
<aop:pointcut id="pt" expression="execution(* *..transfer(..))"/>
<aop:aspect ref="txAdvice">
<aop:around method="transtructionManager" pointcut-ref="pt" />
</aop:aspect>
</aop:config>
</beans>