Spring 事务详解
实现购买股票案例:
一、引入JAR文件:

二、开始搭建分层架构---创建账户(Account)和股票(Stock)实体类
Account:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
/* * 账户 */public class Account { private int aid;//账户编号 private String aname;//账户名称 private double balance;//账户金额 public int getAid() { return aid; } public void setAid(int aid) { this.aid = aid; } public String getAname() { return aname; } public void setAname(String aname) { this.aname = aname; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } |
Stock:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/* * 股票 */public class Stock {private int sid;//股票编号private String sname;//名称private int count;//股数public int getSid() { return sid;}public void setSid(int sid) { this.sid = sid;}public String getSname() { return sname;}public void setSname(String sname) { this.sname = sname;}public int getCount() { return count;}public void setCount(int count) { this.count = count;}} |
三、创建Dao层,定义账户以及股票的接口,自定义新增和修改的方法,实现类实现该接口,重写方法
IAccountDao:
|
1
2
3
4
5
6
|
public interface IAccountDao { //添加账户 public int addAccount(Account account); //修改账户 public int updateAccount(int aid,int money,boolean isBuyOrNot);<br> |
//查询余额
public int selectMoney(int aid);
|
1
|
} |
IStockDao:
|
1
2
3
4
5
6
7
|
public interface IStockDao { //添加股票 public int addStock(Stock stock); //修改股票 public int updateStock(int aid,int num,boolean isBuyOrNot);} |
AccountDaoImpl:实现类。继承自JdbcDaoSupport并实现IAccountDao接口,在这里需要用到JDBC模板的getJdbcTemplate(),通过该方法实现对SQL语句增删改查。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao{ //添加 public int addAccount(Account account) { String sql="insert into account(aid,aname,balance) values(?,?,?)"; int count=this.getJdbcTemplate().update(sql, account.getAid(),account.getAname(),account.getBalance()); return count; } //修改 public int updateAccount(int aid, int money, boolean isBuyOrNot) { String sql=null; if(isBuyOrNot){ sql="update account set balance=balance-? where aid=?"; } else{ sql="update account set balance=balance+? where aid=?"; } int count=this.getJdbcTemplate().update(sql, money,aid); return count; } |
StockDaoImpl:实现类同理
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class StockDaoImpl extends JdbcDaoSupport implements IStockDao{ //添加股票 public int addStock(Stock stock) { String sql="insert into stock(sid,sname,count) values(?,?,?)"; int count=this.getJdbcTemplate().update(sql, stock.getSid(),stock.getSname(),stock.getCount()); return count; } //修改 public int updateStock(int aid, int num, boolean isBuyOrNot) { String sql=null; if(isBuyOrNot){ sql="update stock set count=count+? where sid=?"; } else{ sql="update stock set count=count-? where sid=?"; } int count=this.getJdbcTemplate().update(sql, num,aid); return count; } |
四、业务逻辑层:service
定义接口IStockService,并实现添加账户,股票,以及购买股票的方法.购买股票需要传入账户的id,股票的id。以及金额,股数
|
1
2
3
4
5
6
7
8
9
|
public interface IStockService { //添加账户 public int addAccount(Account account); //添加股票 public int addStock(Stock stock); //购买股票 public void buyStock(int aid,int money,int sid,int num) throws StockException;} |
实现类:StockServiceImpl。重写方法。并植入Dao。以及自定义StockException异常,用于判定用户的余额小于0,抛出异常

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public class StockServiceImpl implements IStockService{ //植入dao private IAccountDao accountDao; private IStockDao stockDao; //添加账户 public int addAccount(Account account) { return accountDao.addAccount(account); } //添加股票 public int addStock(Stock stock) { return stockDao.addStock(stock); } //购买一股票 public void buyStock(int aid, int money, int sid, int num) throws StockException { boolean isBuy=true; accountDao.updateAccount(aid, money, isBuy); if(accountDao.selectMoney(aid)<=0){ throw new StockException("捕获异常!!!"); } stockDao.updateStock(aid, num, isBuy); } |
五、Spring配置文件。[重点]
方式一:通过事务代理工厂bean进行配置[XML方式]
①引入一系列的约束头文件以及标签

②配置C3P0数据源以及DAO、Service

③配置事务管理器以及事务代理工厂Bean。测试类getBean获取的是代理工厂id

方式二:注解。测试类getBean获取的id是原始对象service
|
1
2
|
<!-- 注解 --> <tx:annotation-driven transaction-manager="mytx"/> |

方式三:Aspectj AOP配置事务 。同理 测试类getBean方法id获取的是原始对象

测试类:
|
1
2
3
4
5
6
7
8
9
|
public class Test01 {@Testpublic void addTest() throws StockException{ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); IStockService service = (IStockService)ctx.getBean("stockService"); service.buyStock(1, 800, 1, 2);} |
Spring 事务详解的更多相关文章
- spring事务详解(五)总结提高
系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.概念 ...
- spring事务详解(四)测试验证
系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...
- spring事务详解(二)简单样例
系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...
- spring事务详解(三)源码详解
系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...
- spring事务详解(一)初探事务
系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 引子 很多 ...
- Spring、Spring事务详解;使用XML配置事务
@Transactional可以设置以下参数: @Transactional(readOnly=false) // 指定事务是否只读的 true/false @Transactional(rollba ...
- spring事务详解(转载+高亮)
spring提供的事务管理可以分为两类:编程式的和声明式的.编程式的,比较灵活,但是代码量大,存在重复的代码比较多:声明式的比编程式的更灵活.编程式主要使用transactionTemplate.省略 ...
- spring事务详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt122 Spring事务机制主要包括声明式事务和编程式事务,此处侧重讲解声明式 ...
- JAVA框架之Spring【Spring事务详解】
spring提供的事务管理可以分为两类:编程式的和声明式的.编程式的,比较灵活,但是代码量大,存在重复的代码比较多:声明式的比编程式的更灵活.编程式主要使用transactionTemplate.省略 ...
- spring事务详解(二)实例
在Spring中,事务有两种实现方式: 编程式事务管理: 编程式事务管理使用底层源码可实现更细粒度的事务控制.spring推荐使用TransactionTemplate,典型的模板模式. 申明式事务管 ...
随机推荐
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(39)-在线人数统计探讨
系列目录 基于web的网站在线统计一直处于不是很精准的状态!基本上没有一种方法可以确实的统计在线用户! Discuz!NT 在线用户功能算是做得比较好的!参考资料 他的原理大致是根据用户的操作间隔来确 ...
- Android随笔之——Android时间、日期相关类和方法
今天要讲的是Android里关于时间.日期相关类和方法.在Android中,跟时间.日期有关的类主要有Time.Calendar.Date三个类.而与日期格式化输出有关的DateFormat和Simp ...
- java笔记--笔试中极容易出错的表达式的陷阱
我相信每一个学过java的人儿们都被java表达式虐过,各种"肯定是它,我不可能错!",然后各种"尼玛,真假,怎么可能?",虽然在实际开发中很少会真的让你去使用 ...
- How do servlets work-Instantiation, sessions, shared variables and multithreading[reproduced]
When the servletcontainer (like Apache Tomcat) starts up, it will deploy and load all webapplication ...
- 解决Asp.net Mvc中使用异步的时候HttpContext.Current为null的方法
在项目中使用异步(async await)的时候发现一个现象,HttpContext.Current为null,导致一系列的问题. 上网查了一些资料后找到了一个对象: System.Threading ...
- 分享:使用 TypeScript 编写的 JavaScript 游戏代码
<上篇博客>我写出了我一直期望的 JavaScript 大型程序的开发模式,以及 TS(TypeScript) 的一些优势.博客完成之后,我又花了一天时间试用 TS,用它来重构之前编写的一 ...
- C#测试题
阅读下面的程序,程序运行后hovertree值为( ) int x = 3, y = 4, z = 5;String s = "xyz";string hovertree = s ...
- python之最强王者(6)——列表(List)
1.Python 列表(List) 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置 ...
- Warning: mysql_connect(): No such file or directory 解决方案总结(操作系统: Mac)
说明: 本文主要内容参考: Mac下PHP连接MySQL报错"No such file or directory"的解决办法, 并进行个人补充 1. 运行环境: Mac OS X 10.11.4 (M ...
- redis开启远程访问
redis默认只允许本地访问,要使redis可以远程访问可以修改redis.conf 打开redis.conf文件在NETWORK部分有说明 ######################### ...