Spring事务管理(基于@Transactional注解)

【例子】银行转账
一、创建相关类

AccountBiz类

package com.tx;
public class AccountBiz {
	private AccountDao accountDao;
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	//银行转账
	public void transfer(){
		accountDao.transfer();
	}
}

AccountDao类

package com.tx;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
//使用@Transactional注解实现事务管理
@Transactional
public class AccountDao {
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	//银行转账
	public void transfer(){
		add();
		int i=10/0;
		reduce();		
	}
	//增加余额
	public void add(){
		String sql="update account set balance=balance+? where accountNo=?";
		jdbcTemplate.update(sql, 500,655925);
	}
	//减少余额
	public void reduce(){
		String sql="update account set balance=balance-? where accountNo=?";
		jdbcTemplate.update(sql, 500,358788);
	}
}

Test类

package com.tx;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
	public static void main(String[] args) {
		//加载配置Spring-tx.xml
		ApplicationContext context=new ClassPathXmlApplicationContext("Spring-tx.xml");
		//获取accountBiz实例
		AccountBiz accountBiz=(AccountBiz) context.getBean("accountBiz");
		//银行转账
		accountBiz.transfer();
	}
}
二、配置Spring-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:aop="http://www.springframework.org/schema/aop"       
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql:///spring_demo"/>
  <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"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置实体类 -->
<bean id="accountBiz" class="com.tx.AccountBiz">
	<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="com.tx.AccountDao">
	<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>

运行结果:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

云淡风轻58

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

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

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

打赏作者

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

抵扣说明:

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

余额充值