aop配置mysql事物_基于AOP的事务管理

使用转账的例子来说明基于AOP的事务管理

1.导入jar包

c06dd0080b8e78eac3877aa9c37e4d8d.png

2.创建数据库

/*Navicat MySQL Data Transfer

Source Server : localhost

Source Server Version : 50725

Source Host : localhost:3306

Source Database : springtx

Target Server Type : MYSQL

Target Server Version : 50725

File Encoding : 65001

Date: 2019-03-28 16:54:21*/

SET FOREIGN_KEY_CHECKS=0;--------------------------------Table structure for account------------------------------

DROP TABLE IF EXISTS`account`;CREATE TABLE`account` (

`id`int(11) NOT NULLAUTO_INCREMENT,

`username`varchar(50) DEFAULT NULL,

`money` int(11) DEFAULT NULL,PRIMARY KEY(`id`)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;--------------------------------Records of account------------------------------

INSERT INTO `account` VALUES ('1', 'xiaoming', '10000');INSERT INTO `account` VALUES ('2', 'xiaohong', '10000');

3.编写dao层的代码

packageoyb.dao;public interfaceIAccountDao {public voidout(String outer, Integer money);public voidin(String inner, Integer money);

}

packageoyb.dao.impl;importorg.springframework.jdbc.core.support.JdbcDaoSupport;importoyb.dao.IAccountDao;public class AccountDaoImpl extends JdbcDaoSupport implementsIAccountDao {

@Overridepublic voidout(String outer, Integer money) {

String sql= "update account set money = money - ? where username = ?";this.getJdbcTemplate().update(sql,money,outer);

}

@Overridepublic voidin(String inner, Integer money) {

String sql= "update account set money = money + ? where username = ?";this.getJdbcTemplate().update(sql,money,inner);

}

}

4.编写业务层的代码

packageoyb.service;public interfaceIAccountService {public voidtransfer(String outer, String in, Integer money);

}

packageoyb.service.impl;importoyb.dao.IAccountDao;importoyb.service.IAccountService;public class AccountServiceImpl implementsIAccountService {//提供set方法,由spring注入

privateIAccountDao accountDao;public voidsetAccountDao(IAccountDao accountDao) {this.accountDao =accountDao;

}

@Overridepublic voidtransfer(String outer, String inner, Integer money) {

accountDao.out(outer,money);

accountDao.in(inner,money);

}

}

添加db.properties文件

driverClass=com.mysql.jdbc.Driver

jdbcUrl=jdbc:mysql:///springtx

user=root

password=123456

配置beans.xml文件

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/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

测试转账功能是否完好

importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importoyb.dao.IAccountDao;importoyb.service.IAccountService;public classtest {

@Testpublic voidtest(){

ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");

IAccountService accountService= (IAccountService) context.getBean("accountService");

accountService.transfer("xiaoming","xiaohong",100);

}

}

测试的结果

3d851bf1515afe5b97eb387b843a929d.png

基本的条件搭建好了之后,下面来讲基于AOP的事务配置,除了AOP的自动代理,还有工厂代理以及手动管理事务,后两者可作为了解,这里不涉及,主要还是掌握基于AOP的事务管理。

在配置事务管理前,我们可以看下如果业务类中的transfer方法出现了异常,数据库的结果会是怎样的。

我在转账的方法中加了一句int i = 10/0 这段会抛出异常的代码

5b630be9a6e1f6671ff8dacd6cc2d0e4.png

可执行完测试的代码之后可以发现,xiaoming的钱虽然减少了,但是并未转入xiaohong的账户中,根据事务管理可知,如果出现了异常,应该回滚。

dc7a3ff5d84d4402cf74afd002f9e246.png

以上便是没有配置事务管理所会出现的结果

我们让xiaoming的账户恢复到之前的余额

修改beans.xml这个配置文件

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/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

此时在transfer方法中还有int 10/0 这段代码,所以仍存在异常,运行完测试类后,我们查看下数据库

efbfa47adb5639f7bc8fd08849112513.png

可知事务确实是回滚了。

我们把int i =10/0这句代码删除,在运行测试方法查看下数据库

5a8a92cd021bb2d82f8859d1a2a256f9.png

以上便是基于AOP的事务配置的例子

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值