transaction事务案例--银行转账
1)dao层
package cn.spring.transaction.dao;
public interface MoneyDao {
//加钱的方法
void addMoney(double money);
//减钱的方法
void subMoney(double money);
}
代码实现
(2)daoImpl层
package cn.spring.transaction.dao.impl; import cn.spring.transaction.dao.MoneyDao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport; public class MoneyDaoImpl extends JdbcDaoSupport implements MoneyDao {
JdbcTemplate jdbcTemplate=this.getJdbcTemplate();
@Override
public void addMoney(double money) {
String sql="update accounts set balance=balance+? where accountname='小明'";
this.getJdbcTemplate().update(sql,money);
} @Override
public void subMoney(double money) {
String sql="update accounts set balance=balance-? where accountname='小李'";
this.getJdbcTemplate().update(sql,money);
}
}
代码实现
(3)service层
package cn.spring.transaction.service;
public interface MoneyService {
//转账的方法
void transferMoney(double money);
}
代码实现
(4)serviceImpl层
package cn.spring.transaction.service.impl; import cn.spring.transaction.dao.MoneyDao;
import cn.spring.transaction.service.MoneyService; public class MoneyServiceImpl implements MoneyService {
MoneyDao moneyDao;
@Override
public void transferMoney(double money) { //金额先减后加
moneyDao.subMoney(money);
moneyDao.addMoney(money);
} public MoneyDao getMoneyDao() {
return moneyDao;
} public void setMoneyDao(MoneyDao moneyDao) {
this.moneyDao = moneyDao;
}
}
代码实现
(5)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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
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/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"> <!--1.配置数据源 spring内置的数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> <!--2.引入属性文件-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder> <!--3.构建jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--4.dao-->
<bean id="moneyDaoImpl" class="cn.spring.transaction.dao.impl.MoneyDaoImpl">
<!--为jdbcTemplate配置数据源-->
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <!--5.service-->
<bean id="moneyServiceImpl" class="cn.spring.transaction.service.impl.MoneyServiceImpl">
<property name="moneyDao" ref="moneyDaoImpl"></property>
</bean> <!--扫描注解:包扫描器-->
<context:component-scan base-package="cn.spring"></context:component-scan> <!--开启AOP注解支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
代码实现
(6)测试类
package cn.spring; import cn.spring.transaction.service.MoneyService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TransactionTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("app01.xml");
MoneyService moneyService = context.getBean(MoneyService.class);
//实现转账
moneyService.transferMoney(1000);
}
}
代码实现
(7)效果展示
转账前 转账后

1、使用代理工厂实现事务
<!--1.配置数据源 spring内置的数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> <!--2.引入属性文件-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder> <!--3.构建jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--配置Spring的事务管理器,默认在发生异常的情况下回滚,否则提交-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--第一种事务-->
<!--配置Spring事务的代理工厂-->
<bean id="transactionFactory" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!--事务管理器-->
<property name="transactionManager" ref="transactionManager"></property>
<!--目标对象-->
<property name="target" ref="moneyServiceImpl"></property>
<!--设置方法-->
<property name="transactionAttributes">
<props>
<!--方法对应的隔离级别和传播行为-->
<prop key="transferMoney">ISOLATION_READ_COMMITTED</prop>
</props>
</property>
</bean>
<!--扫描注解:包扫描器-->
<context:component-scan base-package="cn.spring"></context:component-scan> <!--开启AOP注解支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
2、使用AOP实现事务
<!--1.配置数据源 spring内置的数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> <!--2.引入属性文件-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder> <!--3.构建jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--配置Spring的事务管理器,默认在发生异常的情况下回滚,否则提交-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--第二种事务AOP事务-->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="transferMoney" propagation="REQUIRED" isolation="READ_COMMITTED"></tx:method>
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="pointcut" expression="execution(* *..service.impl.*.*(..))"/>
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="pointcut"></aop:advisor>
</aop:config>
<!--扫描注解:包扫描器-->
<context:component-scan base-package="cn.spring"></context:component-scan> <!--开启AOP注解支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
3、使用注解实现事务

<!--第三种事务注解事务-->
<tx:annotation-driven></tx:annotation-driven>
transaction事务案例--银行转账的更多相关文章
- 大数据学习day35----flume01-------1 agent(关于agent的一些问题),2 event,3 有关agent和event的一些问题,4 transaction(事务控制机制),5 flume安装 6.Flume入门案例
具体见文档,以下只是简单笔记(内容不全) 1.agent Flume中最核心的角色是agent,flume采集系统就是由一个个agent连接起来所形成的一个或简单或复杂的数据传输通道.对于每一个Age ...
- [原创]java WEB学习笔记77:Hibernate学习之路---Hibernate 版本 helloword 与 解析,.环境搭建,hibernate.cfg.xml文件及参数说明,持久化类,对象-关系映射文件.hbm.xml,Hibernate API (Configuration 类,SessionFactory 接口,Session 接口,Transaction(事务))
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- SqlCommand对象-Transaction事务的使用
using (SqlConnection connection = new SqlConnection(connStr)) { SqlCommand sqlcmd = new SqlCommand() ...
- Spring事务经典案例-银行转账
1.entity实体类 2.dao层 3.dao实现类 4.service层 5.serviceimpl层 6.大配置.xml <?xml version="1.0" enc ...
- Transaction事务传播行为种类PROPAGATION_REQUIRED
事务传播行为种类 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播: 表1事务传播行为类型 事务传 ...
- MSSQL Transaction[事务] and Procedure[存储过程]
--事务分三种 --1.显示事务 --我们手动begin transaction ...... commit transaction/rollback transaction --上面这种写法叫做“显 ...
- android 数据库中的事务_银行转账示例
主java package com.itheima.transtation; import com.itheima.transtation.db.BankOpenHelper; import andr ...
- Spring transaction事务之roll back回滚
转载自:http://blog.csdn.net/lovejavaydj/article/details/7635848 试验方法: 写一个单元测试,调用一个service层方法(发生对数据库进行写操 ...
- 浩哥解析MyBatis源码(三)——Transaction事务模块
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6634151.html 1.回顾 之前介绍了Environment环境类,这其实是一个单例类 ...
随机推荐
- Dotnetcore安装nuget包时不能使用content中的文件
问题:用NUGET打包了一个asp.netcore的项目,试图安装到另一个asp.netcore项目中,除了自动添加引用外,还希望自动释放一些文件以供修改.这些操作以前在netframe中是正常的,脚 ...
- Windows 2012 R2 安装RD服务
默认只能同时允许2个用户连接,如果希望更多用户同时连接服务器,需要开启并激活远程桌面服务.参考:https://jingyan.baidu.com/article/9f7e7ec0f5a8686f28 ...
- 不同浏览器对cookie大小与个数的限制
一.浏览器允许每个域名所包含的cookie数: Microsoft指出InternetExplorer8增加cookie限制为每个域名50个,但IE7似乎也允许每个域名50个cookie. Firef ...
- 32(1).层次聚类---AGNES
层次聚类hierarchical clustering 试图在不同层次上对数据集进行划分,从而形成树形的聚类结构. 一. AGNES AGglomerative NESting:AGNES是一种常用的 ...
- BOM的初级理解
1.什么是BOM,BOm有什么作用? BOM和DOM.ES是JavaScript的重要三个组成部分: 其中BOM是专门操作浏览器的API,其实他就是一种兼容性问题,这其中问题比较大就是IE浏览器,谁叫 ...
- ETCD:gRPC代理
原文地址:gRPC proxy gRPC代理是在gRPC层(L7)运行的无状态etcd反向代理.代理旨在减少核心etcd群集上的总处理负载.对于水平可伸缩性,它合并了监视和租约API请求. 为了保护集 ...
- Python 从入门到进阶之路(七)
之前的文章我们简单介绍了一下 Python 中异常处理,本篇文章我们来看一下 Python 中 is 和 == 的区别及深拷贝和浅拷贝. 我们先来看一下在 Python 中的双等号 == . == 是 ...
- js调用网络摄像头
不支持IE浏览器(需要使用flash插件), 支持移动端, 未经过完全测试 PC端使用的时候, HTML页面需要预留video标签, canvas标签 移动端使用的时候, HTML页面需要预留file ...
- 用两种以上的 方式实现一个方法或者对象,调用时打印"你好xx",已定义的代码不能做修改,自己编译的不能出现"你好"? (Javasctript)
先上代码 const obj = { say(){ Array.from(arguments).forEach(item=>{ console.log(`${this.str} ${item}` ...
- 利用Azure虚拟机安装Dynamics 365 Customer Engagement之三:安装Windows活动目录域服务
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...