Spring事务管理(基于xml实现)

【例】模拟银行转账

数据表结构

accountNo:表示账号

balance:表示余额

初始余额都为1000



一、创建相关类

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;
public class AccountDao {
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	//银行转账
	public void transfer(){
		add();
		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 java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class Test {
	public static void main(String[] args) {
		//加载配置spring-jdbcTemplate.xml
		ApplicationContext context=new ClassPathXmlApplicationContext("Spring-tx.xml");
		//获取jdbcTemplate实例
		JdbcTemplate jdbcTemplate=context.getBean(JdbcTemplate.class);
		//获取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" 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"> <!-- bean definitions here -->
<!-- 配置数据源 -->
<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="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>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="advice" transaction-manager="transactionManager">
	<tx:attributes>
		<tx:method name="*" propagation="REQUIRED"/>
	</tx:attributes>
</tx:advice>
<!-- 配置AOP -->
<aop:config>
	<aop:pointcut expression="execution(* com.tx.AccountBiz.transfer(..))" id="pointcut"/>
	<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
</beans>

测试正常转账


测试转账失败

注意:在AccountDao类的transfer方法抛出异常

//银行转账
	public void transfer(){
		add();
		int i=10/0;
		reduce();		
	}

运行结果:





猜你喜欢

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