spring--事务控制(银行转账案例)

1.导入相关包
在这里插入图片描述
或许有些包用不着,但为了方便,直接全部导入

2.定义一个转账方法的接口
在这里插入图片描述

3.创建一个类实现转账方法
在这里插入图片描述

这里继承了JdbcDaoSupport类,直接使用getJdbcTemplate()来获取数据库的连接

4.定义Service层接口来获得转出对象,转入对象,转入金额
在这里插入图片描述

5.实现service接口

package cn.lpp.service;
import javax.annotation.Resource;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import cn.lpp.dao.Account;
public class AccountServiceImp implements AccountService{
    private Account  at;
	private TransactionTemplate tt;
	public void setAt(Account at) {
		this.at = at;
	}
	public void setTt(TransactionTemplate tt) {
		this.tt = tt;
	}
	@Override
	public void transfer(Integer from, Integer to, Double money) {
		// TODO Auto-generated method stub
		//开启事务
	    tt.execute(new TransactionCallbackWithoutResult() {
			
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus arg0) {
				//减钱
				at.decreaseMoney(from, money);
				//加钱
				at.addMoney(to, money);
				
			}
		});
	}
}

6.配置xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

<!-- 指定spring去读取配置文件 -->
<context:property-placeholder location="db.properties"/>
	<!-- 1.将连接池放入容器 -->
	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	
	
<!-- 事务 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务模板对象 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>




	
<!-- 2.dao-->
 <bean  name="account" class="cn.lpp.dao.AccountImpl">
<property name="dataSource" ref="dataSource"></property>
 </bean>
 
 
 <!-- 3. service-->
 <bean name="accountService" class="cn.lpp.service.AccountServiceImp" >
 <property name="at" ref="account"></property>
 <property name="tt" ref="transactionTemplate"></property>
 </bean>
  
</beans>

7.配置db.properties

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.user=root
jdbc.password=1234

8.demo测试

package cn.lpp.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.lpp.dao.Account;
import cn.lpp.service.AccountService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class demo {
	@Resource(name="accountService")
	private AccountService as;
@Test
public void fun1(){
as.transfer(1, 2, 100d);
	
}
}

9.demo前
在这里插入图片描述

demo后

在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值