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>

运行结果:



猜你喜欢

转载自blog.csdn.net/dwenxue/article/details/80886706