Spring-tx事务管理
一、创建数据库bankdb
money表
字段 | 数据类型 | 描述 |
---|---|---|
id | int | id |
name | varchar(32) | 用户姓名 |
money | int | 金额 |
二、导入jar包(依赖)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
三、spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 开启注解-->
<context:component-scan base-package="com.wfb"/>
<!-- 配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/bankdb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 配置 jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务管理-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
四、创建dao
MoneyDao.java
package com.wfb.dao;
public interface MoneyDao {
public int outMoney(int outId,int money);
public int inMoney(int inId,int money);
}
MoneyDaoImpl.java
package com.wfb.dao.impl;
import com.wfb.dao.MoneyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class MoneyDaoImpl implements MoneyDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int outMoney(int outId, int money) {
String sql="update money set money=money-? where id=?";
return jdbcTemplate.update(sql, money,outId);
}
@Override
public int inMoney(int inId, int money) {
String sql="update money set money=money+? where id=?";
return jdbcTemplate.update(sql, money,inId);
}
}
五、创建service
MoneyService.java
package com.wfb.service;
public interface MoneyService {
public void tran(int outId,int inId,int money);
}
MoneyServiceImpl.java
@Transactional
注解标记的类将会被事务管理
package com.wfb.service.impl;
import com.wfb.dao.MoneyDao;
import com.wfb.service.MoneyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Transactional
public class MoneyServiceImpl implements MoneyService {
@Autowired
private MoneyDao moneyDao;
@Override
public void tran(int outId, int inId, int money) {
//转出
moneyDao.outMoney(outId, money);
int i=1/0;
//转入
moneyDao.inMoney(inId, money);
}
}
六、测试
import com.wfb.service.MoneyService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MoneyTest {
@Test
public void testTran(){
ApplicationContext context=new ClassPathXmlApplicationContext("springConfig.xml");
MoneyService moneyService = (MoneyService) context.getBean("moneyServiceImpl");
moneyService.tran(1, 2, 200);
}
}