Spring 事务管理之注解配置声明式事务

废话不多说,直接开始配置

前提:准备数据库数据

 

第一步:创建工程、导入相应的包

新建一个 spring 的工程,导入相应的 spring 的 jar 包。其中 druid 的包,是本人测试使用,可以忽略。

 

相应的工程目录结构:

 

 第二步:编码

AccountDao.java

package com.wq.spring.tx;

public interface AccountDao {
	public void outMoney(String out, Double money) ;
	public void inMoney(String in, Double money);
}

AccountDaoImpl.java

package com.wq.spring.tx;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

	public void outMoney(String out, Double money) {
		System.out.println( out + " 减少:"+ money );
		
		String sql = "update customer set money = money - ? where name = ?";
		int i = this.getJdbcTemplate().update(sql, money, out);
		
		System.out.println(i);
	}

	public void inMoney(String in, Double money) {
		System.out.println( in + " 增加:"+ money );
		
		String sql = "update customer set money = money + ? where name = ?";
		int i = this.getJdbcTemplate().update(sql, money, in);
		System.out.println(i);
	}
}

 AccountService.java

package com.wq.spring.tx;
public interface AccountService {
	public void transfer(String out, String in, Double money);
}

 AccountServiceImpl.java

需要在这里增加 @Transactional 注解 加入事务

package com.wq.spring.tx;

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class AccountServiceImpl implements AccountService{

	private AccountDao accountDao;

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	@Override
	public void transfer(String out, String in, Double money) {
		accountDao.outMoney(out, money);
		// 释放这个地方,测试事务是否成功
//		int i = 1/0;
		accountDao.inMoney(in, money);
	}
}

 TransactionTest.java 测试类

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

public class TransactionTest {
	
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-jdbc-tx.xml");
   	 
		AccountService accountService = (AccountService) context.getBean("accountService");
       
        accountService.transfer("Tim", "Wayfreem", 200d);
	}
}

第三步:配置文件

jdbc.properties

jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/study_db?serverTimezone\=GMT%2B8
jdbc.username=root
jdbc.password=admin

spring-jdbc-tx.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"
     xmlns:task="http://www.springframework.org/schema/task"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.1.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
         http://www.springframework.org/schema/task
		 http://www.springframework.org/schema/task/spring-task-3.1.xsd">
		 
	<!-- 引入外部的属性文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>	

	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driverClass}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	
	<!-- 配置业务层类 -->
	<bean id="accountService" class="com.wq.spring.tx.AccountServiceImpl">
		<property name="accountDao" ref="accountDao" />
	</bean>
	
	<!-- 配置DAO类(简化,会自动配置JdbcTemplate) -->
	<bean id="accountDao" class="com.wq.spring.tx.AccountDaoImpl">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- ======================= 4.使用注解配置声明式事务 ======================= -->

	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- 开启注解事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
</beans>

 第四步:测试正常事务

启动 TransactionTest

 

 查询数据库

 

 第五步:测试异常事务

修改 AccountServiceImpl 类,人为报错。。。

@Override
public void transfer(String out, String in, Double money) {
	accountDao.outMoney(out, money);
	// 释放这个地方,测试事务是否成功
	int i = 1/0;
	accountDao.inMoney(in, money);
}

重新启动测试类

 

 数据库查看

 

 项目工程地址:

链接:https://pan.baidu.com/s/1y8RHwHw2rPc8NUIysC2m-A

提取码:e7fr

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wayfreem

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值