一、引入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 | /* * 账户 */publicclassAccount {    privateintaid;//账户编号    privateString aname;//账户名称    privatedoublebalance;//账户金额            publicintgetAid() {        returnaid;    }    publicvoidsetAid(intaid) {        this.aid = aid;    }    publicString getAname() {        returnaname;    }    publicvoidsetAname(String aname) {        this.aname = aname;    }    publicdoublegetBalance() {        returnbalance;    }    publicvoidsetBalance(doublebalance) {        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 | /* * 股票 */publicclassStock {privateintsid;//股票编号privateString sname;//名称privateintcount;//股数publicintgetSid() {    returnsid;}publicvoidsetSid(intsid) {    this.sid = sid;}publicString getSname() {    returnsname;}publicvoidsetSname(String sname) {    this.sname = sname;}publicintgetCount() {    returncount;}publicvoidsetCount(intcount) {    this.count = count;}} | 
三、创建Dao层,定义账户以及股票的接口,自定义新增和修改的方法,实现类实现该接口,重写方法
IAccountDao:
| 1 2 3 4 5 6 | publicinterfaceIAccountDao {    //添加账户    publicintaddAccount(Account account);       //修改账户    publicintupdateAccount(intaid,intmoney,booleanisBuyOrNot);<br> | 
     //查询余额
     public int selectMoney(int aid);
| 1 | } | 
IStockDao:
| 1 2 3 4 5 6 7 | publicinterfaceIStockDao {  //添加股票  publicintaddStock(Stock stock);          //修改股票  publicintupdateStock(intaid,intnum,booleanisBuyOrNot);} | 
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 | publicclassAccountDaoImpl extendsJdbcDaoSupport implementsIAccountDao{    //添加    publicintaddAccount(Account account) {        String sql="insert into account(aid,aname,balance) values(?,?,?)";        intcount=this.getJdbcTemplate().update(sql, account.getAid(),account.getAname(),account.getBalance());        returncount;    }    //修改    publicintupdateAccount(intaid, intmoney, booleanisBuyOrNot) {        String sql=null;        if(isBuyOrNot){            sql="update account set balance=balance-? where aid=?";        }        else{            sql="update account set balance=balance+? where aid=?";        }        intcount=this.getJdbcTemplate().update(sql, money,aid);        returncount;    } | 
StockDaoImpl:实现类同理
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | publicclassStockDaoImpl extendsJdbcDaoSupport implementsIStockDao{    //添加股票    publicintaddStock(Stock stock) {        String sql="insert into stock(sid,sname,count) values(?,?,?)";        intcount=this.getJdbcTemplate().update(sql, stock.getSid(),stock.getSname(),stock.getCount());        returncount;    }    //修改    publicintupdateStock(intaid, intnum, booleanisBuyOrNot) {        String sql=null;        if(isBuyOrNot){            sql="update stock set count=count+? where sid=?";        }        else{            sql="update stock set count=count-? where sid=?";        }        intcount=this.getJdbcTemplate().update(sql, num,aid);        returncount;        } | 
四、业务逻辑层:service
定义接口IStockService,并实现添加账户,股票,以及购买股票的方法.购买股票需要传入账户的id,股票的id。以及金额,股数
| 1 2 3 4 5 6 7 8 9 | publicinterfaceIStockService {       //添加账户    publicintaddAccount(Account account);    //添加股票    publicintaddStock(Stock stock);        //购买股票    publicvoidbuyStock(intaid,intmoney,intsid,intnum) throwsStockException;} | 
实现类: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 | publicclassStockServiceImpl implementsIStockService{       //植入dao    privateIAccountDao accountDao;    privateIStockDao stockDao;    //添加账户    publicintaddAccount(Account account) {                returnaccountDao.addAccount(account);    }       //添加股票    publicintaddStock(Stock stock) {        returnstockDao.addStock(stock);    }    //购买一股票    publicvoidbuyStock(intaid, intmoney, intsid, intnum) throwsStockException {        booleanisBuy=true;        accountDao.updateAccount(aid, money, isBuy);        if(accountDao.selectMoney(aid)<=0){            thrownewStockException("捕获异常!!!");        }                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 | publicclassTest01 {@TestpublicvoidaddTest() throwsStockException{    ApplicationContext ctx=newClassPathXmlApplicationContext("applicationContext.xml");        IStockService service = (IStockService)ctx.getBean("stockService");        service.buyStock(1, 800, 1, 2);} | 
原文:http://www.cnblogs.com/ainiaiwo/p/6045914.html