第一步创建实体类:Entity
package com.beiwo.epet.entity; public class Account {
private int id; private String name; private int money; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getMoney() {
return money;
} public void setMoney(int money) {
this.money = money;
} }
第二步:基类接口和实现基类接口:Account AccountDaoImpl
package com.beiwo.epet.dao; import com.beiwo.epet.entity.Account; public interface AccountDao { public void updateAccount(String fromName,String toName,int money)throws Exception; public void updateAccount(Account account)throws Exception; public Account findAccountByName(String name)throws Exception;
} package com.beiwo.epet.dao.impl; import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler; import com.beiwo.epet.dao.AccountDao;
import com.beiwo.epet.entity.Account;
import com.beiwo.epet.util.C3P0Util;
import com.beiwo.epet.util.TransactionManager; public class AccountDaoImpl implements AccountDao{ @Override
public void updateAccount(String fromName, String toName, int money) throws Exception{
QueryRunner qr=new QueryRunner(C3P0Util.getDataSource()); qr.update("UPDATE account SET money=money-? WHERE name=?",money,fromName);
qr.update("UPDATE account SET money=money+? WHERE name=?",money,toName);
} @Override
public void updateAccount(Account account) throws Exception{
QueryRunner qr=new QueryRunner();
qr.update(TransactionManager.getConnection(),"UPDATE account SET money=? WHERE name=?", account.getMoney(),account.getName());
} @Override
public Account findAccountByName(String name) throws Exception{
QueryRunner qr=new QueryRunner();
return qr.query(TransactionManager.getConnection(),"SELECT * FROM account WHERE name=?",new BeanHandler<Account>(Account.class),name); } }
第三步:业务逻辑接口和业务逻辑的实现:AccountService AccountServiceImply
package com.beiwo.epet.service; public interface AccountService { public void transfer(String formName,String toName,int money); } package com.beiwo.epet.service.impl; import com.beiwo.epet.dao.AccountDao;
import com.beiwo.epet.dao.impl.AccountDaoImpl;
import com.beiwo.epet.entity.Account;
import com.beiwo.epet.service.AccountService;
import com.beiwo.epet.util.TransactionManager; public class AccountServiceImpl implements AccountService{ @Override
public void transfer(String formName, String toName, int money) {
AccountDao accountDao=new AccountDaoImpl(); try {
///开始一个事务,start transaction;
//获取转入和转出的账户对象
TransactionManager.startTransaction(); Account fromAccount=accountDao.findAccountByName(formName);
Account toAccount=accountDao.findAccountByName(toName); //修改账户的各自金额
fromAccount.setMoney(fromAccount.getMoney()-money);
toAccount.setMoney(toAccount.getMoney()+money); //完成转账的操作
accountDao.updateAccount(fromAccount); int i=2/0; accountDao.updateAccount(toAccount); TransactionManager.commitTransaction(); } catch (Exception e) {
try {
TransactionManager.rollbackTransaction();;//事务的回滚
} catch (Exception e2) {
e2.printStackTrace();
} }finally{
try {
TransactionManager.close();
} catch (Exception e2) {
e2.printStackTrace();
}
} } }
第四步:测试类 TesTransfer
package com.beiwo.epet.test; import org.junit.Test; import com.beiwo.epet.service.AccountService;
import com.beiwo.epet.service.impl.AccountServiceImpl; public class TestTransfer { @Test
public void test(){
AccountService accountService=new AccountServiceImpl(); accountService.transfer("aaa", "bbb", 100); }
}
数据库的建立: