SQL脚本
/*
Navicat Premium Data Transfer
Source Server : 本地
Source Server Type : MySQL
Source Server Version : 50723
Source Host : localhost:3306
Source Schema : account
Target Server Type : MySQL
Target Server Version : 50723
File Encoding : 65001
Date: 06/03/2019 20:43:26
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for ac_info
-- ----------------------------
DROP TABLE IF EXISTS `ac_info`;
CREATE TABLE `ac_info` (
`accountId` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`balance` double NULL DEFAULT NULL,
PRIMARY KEY (`accountId`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of ac_info
-- ----------------------------
INSERT INTO `ac_info` VALUES (1, 'Tom', 1100);
INSERT INTO `ac_info` VALUES (2, 'Sam', 1400);
INSERT INTO `ac_info` VALUES (3, 'Deng', 1500);
INSERT INTO `ac_info` VALUES (4, 'Zang', 8000000);
INSERT INTO `ac_info` VALUES (5, 'Gong', 12000);
SET FOREIGN_KEY_CHECKS = 1;
使用的是MYSQL数据库
构建项目结构
AccountDao
package T1;
/**
* Created with IntelliJ IDEA.
* Description:
* Date:2019.03.06
* Author:Seale
*/
public interface AccountDao {
/**
* 转账
* @param outName 汇款人
* @param inName 收款人
* @param money 金额
*/
void transfer(String outName,String inName , double money);
}
AccountDaoImpl
package T1;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
/**
* Created with IntelliJ IDEA.
* Description:
* Date:2019.03.06
* Author:Seale
*/
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
@Resource(name = "jdbcTemplate")
private JdbcTemplate jdbcTemplate ;
//转账
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,readOnly = false)
@Override
public void transfer(String outName, String inName, double money) {
String sql = "update ac_info set balance = balance + ? where name = ?";
//收款人增长金额
this.jdbcTemplate.update(sql,money,inName);
//模拟异常
int i = 1/0;
String sql1 = "update ac_info set balance = balance - ? where name = ?";
//汇款人扣除金额
this.jdbcTemplate.update(sql1,money,outName);
}
}
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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解处理器,启动扫描-->
<context:component-scan base-package="T1"/>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--数据库驱动-->
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<!--连接数据库的url-->
<property name="url" value="jdbc:mysql://localhost/account?serverTimezone=GMT&useUnicode=true&characterEncoding=UTF-8"/>
<!--连接数据库的用户名/密码-->
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--配置jdbc模版-->
<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>
编写测试类
package T1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created with IntelliJ IDEA.
* Description:
* Date:2019.03.06
* Author:Seale
*/
public class AccountTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
//获取实例
AccountDao accountDao = (AccountDao) context.getBean("accountDao");
try {
accountDao.transfer("Tom","Sam",100.00);
System.out.println("转账成功");
}catch (Exception e){
System.out.println("转账失败");
}
}
}
One comment
邮件测试