实现购买股票案例:

一、引入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 {
@Test
public void addTest() throws StockException{
    ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
     
    IStockService service = (IStockService)ctx.getBean("stockService");
     
    service.buyStock(180012);
}
 
 

Spring 事务的更多相关文章

  1. spring事务概念理解

    1.数据并发问题 脏读 A事务读取B事务尚未提交的更新数据,并在此数据的基础上操作.如果B事务回滚,则A事务读取的数据就是错误的.即读取了脏数据或者错误数据. 不可重复组 A事务先后读取了B事务提交[ ...

  2. 【Java EE 学习 52】【Spring学习第四天】【Spring与JDBC】【JdbcTemplate创建的三种方式】【Spring事务管理】【事务中使用dbutils则回滚失败!!!??】

    一.JDBC编程特点 静态代码+动态变量=JDBC编程. 静态代码:比如所有的数据库连接池 都实现了DataSource接口,都实现了Connection接口. 动态变量:用户名.密码.连接的数据库. ...

  3. Spring事务

    1.@Transactional 只能被应用到public方法上, 对于其它非public的方法,如果标记了@Transactional也不会报错,但方法没有事务功能.@Transactional 的 ...

  4. spring事务管理器设计思想(二)

    上文见<spring事务管理器设计思想(一)> 对于第二个问题,涉及到事务的传播级别,定义如下: PROPAGATION_REQUIRED-- 如果当前没有事务,就新建一个事务.这是最常见 ...

  5. spring事务管理器设计思想(一)

    在最近做的一个项目里面,涉及到多数据源的操作,比较特殊的是,这多个数据库的表结构完全相同,由于我们使用的ibatis框架作为持久化层,为了防止每一个数据源都配置一套规则,所以重新实现了数据源,根据线程 ...

  6. Spring事务管理的三种方式

    一 .第一种:全注解声明式事务 Xml代码 复制代码 收藏代码 .<?xml version="1.0" encoding="UTF-8"?> .& ...

  7. spring 事务传播特性 和隔离级别

    事务的几种传播特性1. PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务.如果没有事务则开启2. PROPAGATION_SUPPORTS: 如果存在一个事务,支持当前事务 ...

  8. Spring事务管理

    Spring是SSH中的管理员,负责管理其它框架,协调各个部分的工作.今天一起学习一下Spring的事务管理.Spring的事务管理分为声明式跟编程式.声明式就是在Spring的配置文件中进行相关配置 ...

  9. Spring事务传播属性

    Spring 对事务控制的支持统一在 TransactionDefinition 类中描述,该类有以下几个重要的接口方法: int getPropagationBehavior():事务的传播行为 i ...

  10. Spring事务属性的介绍

    Spring声明式事务让我们从复杂的事务处理中得到解脱.使得我们再也无需要去处理获得连接.关闭连接.事务提交和回滚等这些操作.再也无需要我们在与事务相关的方法中处理大量的try-catch-final ...

随机推荐

  1. 连接输出 如果存在在php中多次echo输出js的时候

  2. 向ES6靠齐的Class.js

    写在前面 在2008年的时候,John Resig写了一 Class.js,使用的方式如下: var Person = Class.extend({ init: function(isDancing) ...

  3. AMD and CMD are dead之KMDjs内核之依赖分析

    有人说js中有三座大三:this.原型链和scope tree,搞懂了他们就算是js成人礼.当然还有其他不同看法的js成人礼,如熟悉js的:OOP.AP.FP.DOP.AOP.当然还听说一种最牛B的j ...

  4. #9.5课堂JS总结#循环语句、函数

    一.循环语句 1.for循环 下面是 for 循环的语法: for (语句 1; 语句 2; 语句 3) { 被执行的代码块 } 语句 1 在循环(代码块)开始前执行 语句 2 定义运行循环(代码块) ...

  5. 字体属性设置(一):谷歌浏览器12px以下字体的显示;方法和原理

    前言: chrome 谷歌浏览器默认的字体大小为16px:可以通过设置font-size来设置字体大小但是当设置到12px以下的时候字体大小不再改变:对于想设置其他大小的字体就很头疼,本文参考网上的方 ...

  6. 心无旁骛,向死而生:WGDC2016给创企上的一堂课

    "这是最好的时代,也是最坏的时代:这是希望的春天,也是失望的冬天." ------狄更斯 WGDC2016落幕已经一月有余,我仍然记得会议结束后,穿过高大宽敞的国家会议中心大厅,走 ...

  7. iOS 学习 - 22 异步解析 JSON,使用 FMDB 存储,TableView 显示

    前提是已经知道了有哪些 key 值 Model 类: .h @interface ListModel : NSObject @property (nonatomic, copy)NSString *t ...

  8. ORACLE临时表总结

    临时表概念 临时表就是用来暂时保存临时数据(亦或叫中间数据)的一个数据库对象,它和普通表有些类似,然而又有很大区别.它只能存储在临时表空间,而非用户的表空间.ORACLE临时表是会话或事务级别的,只对 ...

  9. Tomcat:配置SSL

    SSL简述 SSL就是安全套接字层,是一种允许web浏览器和 web服务器通过安全连接通信的技术.这是一个双向的过程,这意味着 服务器和浏览器在发送数据之前加密所有交流的数据. SSL有一个重要的特点 ...

  10. ligerDialog的使用

    1.通过ViewBag来传值. @if (ViewBag.ReturnMessage != null) 2.脚本代码: 对话框设计与赋值问题. <script type="text/j ...