基于XML 方式的声明式事务

applicationContext.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-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    
  <!-- 配置数据库连接池(数据源) -->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
   		
	<!-- 数据库驱动 -->
	<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
	<!-- 连接数据库的url -->
	<property name="jdbcUrl" value="jdbc:mysql:///transaction?useUnicode=true&amp;characterEncoding=utf-8"></property>	
	<!-- 连接数据库的用户名 -->
	<property name="user" value="root"></property>
	<!-- 连接数据库的密码 -->
	<property name="password" value="123456"></property>
   </bean>
    	
   <!-- 事务管理对象,依赖于数据源 -->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   		<property name="dataSource" ref="dataSource"></property>
   </bean>
	
	<!-- 编写通知:对事务进行增强(通知),需要编写对切入点和具体执行事务细节 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- name 表示任意方法名称 -->
			<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 编写aop,让spring自动对目标生成代理,需要使用Aspcetj的表达式-->
	<aop:config>
		<!-- 切入点 -->
		<aop:pointcut expression="execution(* com.itheima.transaction.*.*(..))" id="pc"/>
		<!-- 切面:将切入点与通知整合 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
	</aop:config>
	
	<!-- 配置JDBC模板 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<!-- 默认使用数据库源 -->
		<property name="dataSource" ref="dataSource"></property>	
	</bean>
	<!-- 定义id为accountDaoImpl的Bean -->
	<bean id="accountDaoImpl" class="com.itheima.transaction.AccountDaoImpl">
		<!-- 将jdbcTemplate注入到AccountDaoImpl实例中 -->
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>

  </beans>

AccountDaoImpl

package com.itheima.transaction;

import org.springframework.jdbc.core.JdbcTemplate;

public class AccountDaoImpl implements AccountDao{
	
	private JdbcTemplate jdbcTemplate;
	
	
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	
	@Override
	public void transfer(int fromId, int toId, double money) {
		
		String sql1 = "UPDATE account SET money = money - ? where id = ?";
		this.jdbcTemplate.update(sql1,money,fromId);
		
		//int i = 10 / 0;
		
		String sql2 = "UPDATE account SET money = money + ? where id = ?";
		this.jdbcTemplate.update(sql2,money,toId);
		
	}
	
}

测试方法

package com.itheima.transaction;

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

public class AccountTest {
	
	public static void main(String[] args) {
		
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		AccountService accountService = (AccountService)applicationContext.getBean("accountDaoImpl");
		
		//调用实例中的转账方法
		accountService.tranfer(1, 2, 500);
		System.out.println("转账成功.................");
		
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值