Spring声明式事务管理(基于注解方式实现)
----------------------siwuxie095
Spring 声明式事务管理(基于注解方式实现)
以转账为例
1、导入相关
jar 包(共 10 个包)
(1)导入核心 jar 包和日志相关的 jar 包

(2)导入
JdbcTemplate 的 jar 包

(3)导入
MySQL 的 JDBC 驱动包

mysql-connector-java
下载链接:
https://dev.mysql.com/downloads/connector/j/
(4)导入 AOP 的 jar 包

2、在
MySQL 中手动创建数据库和表
数据库名:tx_db,表名:account,字段:id、name、money

手动添加数据,用作测试

3、具体步骤
(1)配置事务管理器
|
<!-- 配置事务管理器 --> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 在 DataSourceTransactionManager 源代码中有 属性 dataSource 和其 set 方法,所以可以注入 --> <property </bean> |
(2)配置事务注解
|
<!-- 配置事务注解,即 <tx:annotation-driven |
(3)在使用事务的类上添加注解:@Transactional
「使用事务的类一般在业务层」
4、具体实现
编写一个
Dao 类
AccountDao.java:
|
package com.siwuxie095.dao; import org.springframework.jdbc.core.JdbcTemplate; public class AccountDao { private JdbcTemplate jdbcTemplate;
public this.jdbcTemplate = jdbcTemplate; }
/** * 转出 */ public String sql="update account set money=money-? where name=?"; jdbcTemplate.update(sql, money, from); }
/** * 转入 */ public String sql="update account set money=money+? where name=?"; jdbcTemplate.update(sql, money, to); }
} |
(2)编写一个
Service 类
AccountService.java:
|
package com.siwuxie095.service; import org.springframework.transaction.annotation.Transactional; import com.siwuxie095.dao.AccountDao; // 在使用事务的类上添加注解:@Transactional @Transactional public class AccountService { private AccountDao accountDao;
public this.accountDao = accountDao; }
/** * 转账 */ public
accountDao.lessMoney(from, money);
// 即便中间出现了什么异常,也会进行回滚 // 如:int num=10/0;
accountDao.moreMoney(to, money);
}
} |
(3)在配置文件中进行配置
applicationContext.xml:
|
<?xml <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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/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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置内置连接池 --> <bean <property <!-- jdbc:mysql:///tx_db 是 jdbc:mysql://localhost:3306/tx_db 的简写 --> <property <property <property </bean>
<!-- 配置事务管理器 --> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 在 DataSourceTransactionManager 源代码中有 属性 dataSource 和其 set 方法,所以可以注入 --> <property </bean>
<!-- 配置事务注解,即 <tx:annotation-driven
<!-- 配置对象并注入属性 --> <bean <property </bean>
<bean <property </bean>
<bean <!-- 在 JdbcTemplate 源代码中有属性 dataSource 和其 set 方法,所以可以注入 --> <property </bean> </beans> |
(4)编写一个测试类
TestDemo.java:
|
package com.siwuxie095.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.siwuxie095.service.AccountService; public class TestDmo { /** * 手动加上 @Test 以进行单元测试(将自动导入 JUnit 4 的 jar 包) * * 选中方法名,右键->Run As->JUint Test */ @Test public
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService=(AccountService) context.getBean("accountService");
accountService.transfer("小白", "小黑", 1000); }
} |
【made by siwuxie095】
Spring声明式事务管理(基于注解方式实现)的更多相关文章
- Spring声明式事务管理基于@Transactional注解
概述:我们已知道Spring声明式事务管理有两种常用的方式,一种是基于tx/aop命名空间的xml配置文件,另一种则是基于@Transactional 注解. 第一种方式我已在上文为大 ...
- Spring声明式事务管理基于tx/aop命名空间
目的:通过Spring AOP 实现Spring声明式事务管理; Spring支持编程式事务管理和声明式事务管理两种方式. 而声明式事务管理也有两种常用的方式,一种是基于tx/aop命名空间的xml配 ...
- Spring事务管理之声明式事务管理-基于注解的方式
© 版权声明:本文为博主原创文章,转载请注明出处 案例 - 利用Spring的声明式事务(TransactionProxyFactoryBean)管理模拟转账过程 数据库准备 -- 创建表 CREAT ...
- Spring声明式事务管理(基于XML方式实现)
--------------------siwuxie095 Spring 声明式事务管理(基于 XML 方式实现) 以转账为例 ...
- Spring 声明式事务管理方式
声明式事务管理,基于AOP对目标代理,添加环绕通知,比编码方案优势,不具有侵入式,不需要修改原来的代码. 1.基于XML配置的声明式事务管理方案(案例) 接口Service public i ...
- XML方式实现Spring声明式事务管理
1.首先编写一个实体类 public class Dept { private int deptId; private String deptName; public int getDeptId() ...
- Spring声明式事务管理与配置介绍
转至:http://java.9sssd.com/javafw/art/1215 [摘要]本文介绍Spring声明式事务管理与配置,包括Spring声明式事务配置的五种方式.事务的传播属性(Propa ...
- Spring声明式事务如何选择代理方式?
Spring声明式事务如何选择代理方式 解决方法: 1.基于注解方法: <tx:annotation-driven transaction-manager="txManager&q ...
- spring 声明式事务管理
简单理解事务: 比如你去ATM机取5000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉5000元钱:然后ATM出5000元钱.这两个步骤必须是要么都执行要么都不执行.如果银行卡扣除了5000块但 ...
随机推荐
- ESXI服务器的四个网口负载均衡
什么是NIC Team(负载均衡) NIC Team其实就是将多个物理网卡同时分配到相同的端口/端口组,目的是为了实现带宽聚合,负载均衡以及故障转移 配置NIC Team 一.选择一台ESXi主机,打 ...
- 3种方法轻松处理php开发中emoji表情的问题
背景 做微信开发的时候就会发现,存储微信昵称必不可少. 可这万恶的微信支持emoji表情做昵称,这就有点蛋疼了 一般Mysql表设计时,都是用UTF8字符集的.把带有emoji的昵称字段往里面inse ...
- __del__,item系列 ,hash方法,__eq__,
# 构造方法 申请一个空间# 析构方法 释放一个空间 # 某个对象借用了操作系统的资源,还要通过析构方法归还回去:文件资源 网络资源 # 垃圾回收机制 class A: def __del__(sel ...
- python学习之----urllib与urllib2的区分
urllib 还是urllib2 ? 如果你用过Python 2.x 里的urllib2 库,可能会发现urllib2 与urllib 有些不同. 在Python 3.x 里,urllib2 改名为u ...
- PHP笔记(配置UPUPW环境)
一,首先修改HOSTS将127.0.0.1 gzt.com加入,前面不要# 二,GZT.COM的数据库文件在--------------配置在: - 三,配置 主要修改这几个 $BASIC=arra ...
- JQ-用户注册用到的图形验证码,短信验证码点击事件,切换active类
// 点击切换图形验证码 页面加载完后执行,类似window.onload $(function () { var imgCaptcha = $(".img-captcha"); ...
- MVC,MVP 和 MVVM 模式如何选择?
转摘:http://www.linuxidc.com/Linux/2015-10/124622.htm 前言 做客户端开发.前端开发对MVC.MVP.MVVM这些名词不了解也应该大致听过,都是为了解决 ...
- OpenACC 优化矩阵乘法
▶ 按书上的步骤使用不同的导语优化矩阵乘法 ● 所有的代码 #include <iostream> #include <cstdlib> #include <chrono ...
- LSTM(Long Short-Term Memory)长短期记忆网络
1. 摘要 对于RNN解决了之前信息保存的问题,例如,对于阅读一篇文章,RNN网络可以借助前面提到的信息对当前的词进行判断和理解,这是传统的网络是不能做到的.但是,对于RNN网络存在长期依赖问题,比如 ...
- xe7 Unresolved external CSPIN.OBJ
工程里打开使用了CSPIN控件的界面窗体,再编译就好了 [ilink32 Error] Error: Unresolved external 'Vcl::Controls::TControl::Set ...