/**
* 账户对象
*
*/
public class Account {
private int accountid;
private String name;
private String balance;
public int getAccountid() {
return accountid;
}
public void setAccountid(int accountid) {
this.accountid = accountid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBalance() {
return balance;
}
public void setBalance(String balance) {
this.balance = balance;
}
}
/**
* 股票对象
*
*/
public class Stock {
private int stockid;
private String name;
private Integer count;
public Stock() {
super();
}
public Stock(int stockid, String name, Integer count) {
super();
this.stockid = stockid;
this.name = name;
this.count = count;
}
public int getStockid() {
return stockid;
}
public void setStockid(int stockid) {
this.stockid = stockid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
}
DAO层
public interface AccountDao {
void addAccount(String name,double money);
void updateAccount(String name,double money,boolean isbuy);
}
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void addAccount(String name, double money) {
String sql = "insert account(name,balance) values(?,?);";
this.getJdbcTemplate().update(sql,name,money);
}
@Override
public void updateAccount(String name, double money, boolean isbuy) {
String sql = "update account set balance=balance+? where name=?";
if(isbuy)
sql = "update account set balance=balance-? where name=?";
this.getJdbcTemplate().update(sql, money,name);
}
}
public interface StockDao {
void addStock(String sname,int count);
void updateStock(String sname,int count,boolean isbuy);
}
public class StockDaoImpl extends JdbcDaoSupport implements StockDao {
@Override
public void addStock(String sname, int count) {
String sql = "insert into stock(name,count) values(?,?)";
this.getJdbcTemplate().update(sql,sname,count);
}
@Override
public void updateStock(String sname, int count, boolean isbuy) {
String sql = "update stock set count = count-? where name = ?";
if(isbuy)
sql = "update stock set count = count+? where name = ?";
this.getJdbcTemplate().update(sql, count,sname);
}
}
Service
public interface BuyStockService {
public void addAccount(String accountname, double money);
public void addStock(String stockname, int amount);
public void buyStock(String accountname, double money, String stockname, int amount) throws BuyStockException;
}
public class BuyStockServiceImpl implements BuyStockService{
private AccountDao accountDao;
private StockDao stockDao;
@Override
public void addAccount(String accountname, double money) {
accountDao.addAccount(accountname,money);
}
@Override
public void addStock(String stockname, int amount) {
stockDao.addStock(stockname,amount);
}
@Override
public void buyStock(String accountname, double money, String stockname, int amount) throws BuyStockException {
boolean isBuy = true;
accountDao.updateAccount(accountname, money, isBuy);
if(isBuy==true){
throw new BuyStockException("购买股票发生异常");
}
stockDao.updateStock(stockname, amount, isBuy);
}
public AccountDao getAccountDao() {
return accountDao;
}
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public StockDao getStockDao() {
return stockDao;
}
public void setStockDao(StockDao stockDao) {
this.stockDao = stockDao;
}
}
自定义异常类
public class BuyStockException extends Exception {
public BuyStockException() {
super();
}
public BuyStockException(String message) {
super(message);
}
}
ISOLATION_DEFAULT,PROPAGATION_REQUIRED
ISOLATION_DEFAULT,PROPAGATION_REQUIRED,-BuyStockException
测试入口
public static void main(String[] args) {
String resouce = "transaction/test2/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(resouce);
BuyStockService buyStockService = (BuyStockService) applicationContext.getBean("serviceProxy");
// buyStockService.openAccount("小郑", 5000);
// buyStockService.openStock("知晓科技", 0);
try {
buyStockService.buyStock("小郑", 1000, "知晓科技", 100);
} catch (BuyStockException e) {
e.printStackTrace();
}
}
发生异常账户金额不能减,股票不能增加
其他类不做改变,只改变购买股票接口实现类和配置文件
public class BuyStockServiceImpl implements BuyStockService{
private AccountDao accountDao;
private StockDao stockDao;
@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED)
@Override
public void addAccount(String accountname, double money) {
accountDao.addAccount(accountname,money);
}
@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED)
@Override
public void addStock(String stockname, int amount) {
stockDao.addStock(stockname,amount);
}
public BuyStockServiceImpl() {
// TODO Auto-generated constructor stub
}
@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,rollbackFor=BuyStockException.class)
@Override
public void buyStock(String accountname, double money, String stockname, int amount) throws BuyStockException {
boolean isBuy = true;
accountDao.updateAccount(accountname, money, isBuy);
if(isBuy==true){
throw new BuyStockException("购买股票发生异常");
}
stockDao.updateStock(stockname, amount, isBuy);
}
public AccountDao getAccountDao() {
return accountDao;
}
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public StockDao getStockDao() {
return stockDao;
}
public void setStockDao(StockDao stockDao) {
this.stockDao = stockDao;
}
}
(3)基于Aspectj AOP配置事务
public class BuyStockServiceImpl implements BuyStockService{
private AccountDao accountDao;
private StockDao stockDao;
@Override
public void addAccount(String accountname, double money) {
accountDao.addAccount(accountname,money);
}
@Override
public void addStock(String stockname, int amount) {
stockDao.addStock(stockname,amount);
}
public BuyStockServiceImpl() {
// TODO Auto-generated constructor stub
}
@Override
public void buyStock(String accountname, double money, String stockname, int amount) throws BuyStockException {
boolean isBuy = true;
accountDao.updateAccount(accountname, money, isBuy);
if(isBuy==true){
throw new BuyStockException("购买股票发生异常");
}
stockDao.updateStock(stockname, amount, isBuy);
}
public AccountDao getAccountDao() {
return accountDao;
}
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public StockDao getStockDao() {
return stockDao;
}
public void setStockDao(StockDao stockDao) {
this.stockDao = stockDao;
}
}
好了今天的博客写到这里,明天继续加油!