购买股票-spring-事物管理的实现

事物管理实现的是提交与回滚,我这里用aspectJ实现

jar包:spring-aop-5.0.1.RELEASE.jar  com.springsource.org.aopalliance-1.0.0.jar  com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar  

aop的约束 以及tx事物的约束

全部jar包:

 com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
com.springsource.org.apache.commons.pool-1.5.3.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
mysql-connector-java-5.1.7-bin.jar
spring-aop-5.0.1.RELEASE.jar
spring-aspects-5.0.1.RELEASE.jar
spring-beans-5.0.1.RELEASE.jar
spring-context-5.0.1.RELEASE.jar
spring-core-5.0.1.RELEASE.jar
spring-expression-5.0.1.RELEASE.jar
spring-jcl-5.0.1.RELEASE.jar
spring-jdbc-4.3.6.RELEASE.jar
spring-test-5.0.1.RELEASE.jar
spring-tx-4.3.6.RELEASE.jar

定义account账户类

package com.abc.beans;


public class Account {


private Integer aid;
private String aname;
private double balance;

public Account() {
super();
// TODO Auto-generated constructor stub
}


public Account(String aname, double balance) {
super();
this.aname = aname;
this.balance = balance;

}


public Integer getAid() {
return aid;
}


public void setAid(Integer 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股票类

package com.abc.beans;


public class Stock {
   
private Integer sid;
private String sname;
private int count; //股票数量
public Stock() {
super();
// TODO Auto-generated constructor stub
}
public Stock(String sname, int count) {
super();
this.sname = sname;
this.count = count;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer 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;
}

}

定义事物类 开户方法以及购买股票service

package com.abc.service;


public interface IBuyStockService {


void openAccount(String aname ,double money);
void openStock(String sname,int count);  /*开户*/


void buyStock(String aname, double money, String sname,int amount);//买票

实现service

package com.abc.service;


import com.abc.dao.IAccountDao;
import com.abc.dao.IStockDao;
import com.abc.exceptions.BuyStockException;


public class BuyStockServiceImpl implements IBuyStockService {


private IAccountDao adao;
private IStockDao sdao;              这里编译代码直接接dao,然后引入setdao方法()为spring容器添加属性,编译之后会报错,ctrl1 加入相应dao类,  同理对应的方法直写,


public void setAdao(IAccountDao adao) {
this.adao = adao;
}
                                                                                       

public void setSdao(IStockDao sdao) {
this.sdao = sdao;
}


@Override
public void openAccount(String aname, double money) {
adao.insertAccount(aname,money);


}


@Override
public void openStock(String sname, int count) {
sdao.insertStock(sname, count);


}


public void buyStock(String aname, double money, String sname,int amount) {
boolean isBuy=true;
adao.updateAccount(aname,money,isBuy);//减少账号余额;
if(1==1){
                                                                                     这里引入事物,使其绑在一起,增票,少钱。
try {
throw new BuyStockException("股票异常");
} catch (BuyStockException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}                                                                                        

}
sdao.updateStock(sname,amount,isBuy);//增加票数
         
}



}

package com.abc.exceptions;


public class BuyStockException extends Exception {


public BuyStockException() {
super();
// TODO Auto-generated constructor stub
}


public BuyStockException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
   
}
}

定义俩个接口类
package com.abc.dao;


public interface IAccountDao {

void insertAccount(String aname, double money);


void updateAccount(String aname, double money, boolean isBuy);


}

package com.abc.dao;


public interface IStockDao {


void insertStock(String sname, int count);


void updateStock(String sname, int amount, boolean isBuy);


}

实现俩接口

package com.abc.dao;


import org.springframework.jdbc.core.support.JdbcDaoSupport;


public class AccountDaoimpl extends JdbcDaoSupport  implements IAccountDao {


public void insertAccount(String aname, double money) {
String sql="insert into account(aname,balance)  value(?,?) ";
        this.getJdbcTemplate().update(sql, aname, money);


}


public void updateAccount(String aname, double money, boolean isBuy) {
String sql="update account set balance=balance+? where aname=?";

if(isBuy){
sql="update account set balance=balance-? where aname=?";
}
                                                                                      需要注意的写法sql语句,出现的问题:张三,开户5000,买票扣除1000,怎么用
 sql语句实现?         解决办法如红色标记。 



this.getJdbcTemplate().update(sql,money,aname);
}



}

package com.abc.dao;


import org.springframework.jdbc.core.support.JdbcDaoSupport;


public class StockDaoImpl extends JdbcDaoSupport implements IStockDao {


@Override
public void insertStock(String sname, int count) {
String sql="insert into stock(sname,account)  value(?,?) ";
         this.getJdbcTemplate().update(sql, sname, count);
}


@Override
public void updateStock(String sname, int amount, boolean isBuy) {
String sql="update stock set account=account-? where sname=?";
if(isBuy){
sql="update stock set account=account+? where sname=?";
}
 this.getJdbcTemplate().update(sql,  amount,sname);
}


}

package com.abc.test;


import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.abc.service.IBuyStockService;


public class Mytest {


private IBuyStockService service;


@Before
public void before() {
String config = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
service = (IBuyStockService) ac.getBean("buyStockService");
}


@Test
public void test01() {
service.openAccount("pp", 10000);
service.openStock("ww", 0);


}
@Test
public void test02() {
service.buyStock("pp", 2000, "ww", 500);


}


}


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop.xsd">











 
























   
   









 

           



  注册顺序  :平台事务管理器-事务通知-aop配置


























































你可能感兴趣的:(spring)