实现购买股票案例:

一、引入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. HttpClient通过Post上传多个文件

    public static String sendFilesPost(String url, String fileNames) { HttpClient httpClient = null; Htt ...

  2. java web学习总结(十六) -------------------数据库连接池

    一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...

  3. GJM : 各大开发游戏引擎

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  4. ZKUI中文编码以及以docker方式运行的问题

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  5. Java Class.cast方法

    1.Java api public T cast(Object obj); Casts an object to the class or interface represented 解释的比较笼统, ...

  6. [Architecture] 系统架构正交分解法

    [Architecture] 系统架构正交分解法 前言 随着企业成长,支持企业业务的软件,也会越来越庞大与复杂.当系统复杂到一定程度,开发人员会发现很多系统架构的设计细节,很难有条理.有组织的用一张大 ...

  7. CSS 框模型

    CSS 框模型 CSS 框模型概述 CSS 内边距 CSS 边框 CSS 外边距 CSS 外边距合并 一,CSS 框模型 (Box Model) 规定元素框处理元素内容.内边距.边框 和 外边距 的方 ...

  8. iOS--UILable自适应大小

    #import "ViewController.h" @interface ViewController () @property(strong,nonatomic) UILabe ...

  9. 通过LoadRunner - Analyze详细分析页面元素请求

    众所周知LoadRunner录制某个链接,包括动态请求与js.css.jpg等静态请求. web_custom_request("动态请求", "URL=http://w ...

  10. VSALM 动手实验 - 持续集成

    Visual Studio 应用生命周期管理(VSALM - Visual Studio Application Lifecycle Managemnet)是微软基于Visual Studio产品线所 ...