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 {
@Test
public void addTest() throws StockException{
    ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
     
    IStockService service = (IStockService)ctx.getBean("stockService");
     
    service.buyStock(1, 800, 1, 2);
}
原文地址:https://www.cnblogs.com/jingpeipei/p/6038317.html