文章出處

實現購買股票案例:

一、引入JAR文件:

 


二、開始搭建分層架構---創建賬戶(Account)和股票(Stock)實體類

Account:

/*
 * 賬戶
 */
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:  

/*
 * 股票
 */
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:

public interface IAccountDao {
    //添加賬戶
    public int addAccount(Account account);
	
   //修改賬戶
    public int updateAccount(int aid,int money,boolean isBuyOrNot);

     //查詢余額
     public int selectMoney(int aid);

}

IStockDao:  

public interface IStockDao {
  //添加股票
  public int addStock(Stock stock);
		
  //修改股票
  public int updateStock(int aid,int num,boolean isBuyOrNot);
}

AccountDaoImpl:實現類。繼承自JdbcDaoSupport并實現IAccountDao接口,在這里需要用到JDBC模板的getJdbcTemplate(),通過該方法實現對SQL語句增刪改查。

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:實現類同理

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。以及金額,股數

 

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,拋出異常

 

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

<!-- 注解 -->
  <tx:annotation-driven transaction-manager="mytx"/>

  

方式三:Aspectj AOP配置事務 。同理 測試類getBean方法id獲取的是原始對象

測試類:

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);
}

文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()