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事务概念理解
1.数据并发问题 脏读 A事务读取B事务尚未提交的更新数据,并在此数据的基础上操作.如果B事务回滚,则A事务读取的数据就是错误的.即读取了脏数据或者错误数据. 不可重复组 A事务先后读取了B事务提交[ ...
- 【Java EE 学习 52】【Spring学习第四天】【Spring与JDBC】【JdbcTemplate创建的三种方式】【Spring事务管理】【事务中使用dbutils则回滚失败!!!??】
一.JDBC编程特点 静态代码+动态变量=JDBC编程. 静态代码:比如所有的数据库连接池 都实现了DataSource接口,都实现了Connection接口. 动态变量:用户名.密码.连接的数据库. ...
- Spring事务
1.@Transactional 只能被应用到public方法上, 对于其它非public的方法,如果标记了@Transactional也不会报错,但方法没有事务功能.@Transactional 的 ...
- spring事务管理器设计思想(二)
上文见<spring事务管理器设计思想(一)> 对于第二个问题,涉及到事务的传播级别,定义如下: PROPAGATION_REQUIRED-- 如果当前没有事务,就新建一个事务.这是最常见 ...
- spring事务管理器设计思想(一)
在最近做的一个项目里面,涉及到多数据源的操作,比较特殊的是,这多个数据库的表结构完全相同,由于我们使用的ibatis框架作为持久化层,为了防止每一个数据源都配置一套规则,所以重新实现了数据源,根据线程 ...
- Spring事务管理的三种方式
一 .第一种:全注解声明式事务 Xml代码 复制代码 收藏代码 .<?xml version="1.0" encoding="UTF-8"?> .& ...
- spring 事务传播特性 和隔离级别
事务的几种传播特性1. PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务.如果没有事务则开启2. PROPAGATION_SUPPORTS: 如果存在一个事务,支持当前事务 ...
- Spring事务管理
Spring是SSH中的管理员,负责管理其它框架,协调各个部分的工作.今天一起学习一下Spring的事务管理.Spring的事务管理分为声明式跟编程式.声明式就是在Spring的配置文件中进行相关配置 ...
- Spring事务传播属性
Spring 对事务控制的支持统一在 TransactionDefinition 类中描述,该类有以下几个重要的接口方法: int getPropagationBehavior():事务的传播行为 i ...
- Spring事务属性的介绍
Spring声明式事务让我们从复杂的事务处理中得到解脱.使得我们再也无需要去处理获得连接.关闭连接.事务提交和回滚等这些操作.再也无需要我们在与事务相关的方法中处理大量的try-catch-final ...
随机推荐
- ABP之模块分析
本篇作为我ABP介绍的第三篇文章,这次想讲下模块的,ABP文档已经有模块这方面的介绍,但是它只讲到如何使用模块,我想详细讲解下它模块的设计思路. ABP 框架提供了创建和组装模块的基础,一个模块能够依 ...
- entityframework学习笔记--004-无载荷与有载荷关系
1.无载荷(with NO Payload)的多对多关系建模 在数据库中,存在通过一张链接表来关联两张表的情况.链接表仅包含连接两张表形成多对多关系的外键,你需要把这两张多对多关系的表导入到实体框架模 ...
- javascript中BOM部分基础知识总结
一.什么是BOM BOM(Browser Object Document)即浏览器对象模型. BOM提供了独立于内容 而与浏览器窗口进行交互的对象: 由于BOM主要用于管 ...
- Spring(6)—— AOP
AOP(Aspect-OrientedProgramming)面向切面编程,与OOP完全不同,使用AOP编程系统被分为切面或关注点,而不是OOP中的对象. AOP的引入 在OOP面向对象的使用中,无可 ...
- UIScrollView出现位移问题
啦啦啦啦啦~~~ UINavigationController和UIScrollView一起使用时会导致UIScrollView位置偏移 情况:UINavigationController的视图上的第 ...
- cocos2dx骨骼动画Armature源码分析(一)
源码分析一body { font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 1.6; padding-to ...
- Picasso设置圆角
package liu.roundimagedemo.view; import android.graphics.Bitmap; import android.graphics.BitmapShade ...
- EventBus3.0源码解析
本文主要介绍EventBus3.0的源码 EventBus是一个Android事件发布/订阅框架,通过解耦发布者和订阅者简化 Android 事件传递. EventBus使用简单,并将事件发布和订阅充 ...
- php new self()和new static()
class A { public static function get_self() { return new self(); } public static function get_static ...
- C#结合Jquery LigerUI Tree插件构造树
Jquery LigerUI Tree是Jquery LigerUI()的插件之一,使用它可以快速的构建树形菜单.呵呵 废话不说了,直入正题,下面介绍C#结合ligerui 构造树形菜单的两种方法 1 ...