Spring 事务管理

demo:账户转账

1、引入依赖

<dependency>
	<groupId>org.aspectj</groupId>
	<artifactId>aspectjrt</artifactId>
	<version>1.9.8.RC3</version>
</dependency>

<dependency>
	<groupId>org.aspectj</groupId>
	<artifactId>aspectjweaver</artifactId>
	<version>1.9.8.RC3</version>
</dependency>

2、配置spring.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/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">

<!-- 文件扫描  -->	
	<context:property-placeholder location="classpath:db.properties"/>
	<context:component-scan base-package="service"/>

<!-- spring事务管理 -->
	
	<!-- 1.配置事务管理 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 2.切面类 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*"/>
		</tx:attributes>
	</tx:advice>
	<!-- 3.aop代理 -->
	<aop:config>
		<aop:pointcut id="pc"
			expression="execution(* *.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pc" />
	</aop:config>
	
<!-- spring事务管理 -->
</beans>

3、数据层

AccountMapper.java

package mapper;

import org.apache.ibatis.annotations.Param;

public interface AccountMapper {
	//传入多个普通类型参数时需要注解(你也可以使用POJO)
	public int  transAccount(@Param("userid") int userid, @Param("amount")float amount);
}

AccountMapper.xml

<?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="mapper.AccountMapper">
	<!-- 返回实际转账金额,注意传入多个普通类型参数的写法 -->
	<!-- 更新操作都不设置返回值类型,默认是修改的记录数 -->
	<update id="transAccount" >
		update account 
		set balance=balance + #{amount} 
		where userid=#{userid}
	</update>
</mapper>

4、服务层

AccountService.java

package service;

import org.apache.ibatis.annotations.Param;

public interface AccountService {
	public void transfer(int inId,int outId,float amount);
}

AccountServiceImpl.java

package service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.AccountDao;

@Service
public class AccountServiceImpl implements AccountService {
	@Autowired
	private AccountDao acc;

	public void transfer(int inId, int outId, float amount) {
		// TODO Auto-generated method stub
		acc.transfer(inId, amount);
		int a = 1 / 0;
		acc.transfer(outId, -amount);
	}

}

5、测试类

package lx56;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.AccountService;

public class MyTest {

	@Test
	public void test0() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
		AccountService as = (AccountService) ctx.getBean("accountServiceImpl");
		as.transfer(1, 2, 10);
	}
}

6、测试结果

出现异常,由于添加了事务管理器,事务发生回滚,数据表中数据并没有发生改变。
在这里插入图片描述

基于注解形式

<!-- 代替切面类和aop -->
<tx:annotation-driven transaction-manager="transactionManager"/>

在需要使用事务管理的方法前加上@Transactional
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值