zoukankan      html  css  js  c++  java
  • DAO跨事物调用---转账

    第一步创建实体类:Entity

     1 package com.beiwo.epet.entity;
     2 
     3 public class Account {
     4     private int id;
     5 
     6     private String name;
     7 
     8     private int money;
     9 
    10     public int getId() {
    11         return id;
    12     }
    13 
    14     public void setId(int id) {
    15         this.id = id;
    16     }
    17 
    18     public String getName() {
    19         return name;
    20     }
    21 
    22     public void setName(String name) {
    23         this.name = name;
    24     }
    25 
    26     public int getMoney() {
    27         return money;
    28     }
    29 
    30     public void setMoney(int money) {
    31         this.money = money;
    32     }
    33 
    34 }

    第二步:基类接口和实现基类接口:Account AccountDaoImpl

     1 package com.beiwo.epet.dao;
     2 
     3 import com.beiwo.epet.entity.Account;
     4 
     5 public interface  AccountDao {
     6 
     7     
     8     public void updateAccount(String fromName,String toName,int money)throws Exception;
     9     
    10     
    11     public void updateAccount(Account account)throws Exception;
    12     
    13 
    14     public Account findAccountByName(String name)throws Exception;
    15 }
    16 
    17 
    18 
    19 
    20 package com.beiwo.epet.dao.impl;
    21 
    22 import org.apache.commons.dbutils.QueryRunner;
    23 import org.apache.commons.dbutils.handlers.BeanHandler;
    24 
    25 import com.beiwo.epet.dao.AccountDao;
    26 import com.beiwo.epet.entity.Account;
    27 import com.beiwo.epet.util.C3P0Util;
    28 import com.beiwo.epet.util.TransactionManager;
    29 
    30 public class AccountDaoImpl implements AccountDao{
    31 
    32     @Override
    33     public void updateAccount(String fromName, String toName, int money) throws Exception{
    34         QueryRunner qr=new QueryRunner(C3P0Util.getDataSource());
    35         
    36         qr.update("UPDATE account SET money=money-? WHERE name=?",money,fromName);
    37         qr.update("UPDATE account SET money=money+? WHERE name=?",money,toName);
    38     }
    39 
    40     @Override
    41     public void updateAccount(Account account) throws Exception{
    42         QueryRunner qr=new QueryRunner();
    43         qr.update(TransactionManager.getConnection(),"UPDATE account SET money=? WHERE name=?", account.getMoney(),account.getName());
    44     }
    45 
    46     @Override
    47     public Account findAccountByName(String name) throws Exception{
    48         QueryRunner qr=new QueryRunner();
    49         return qr.query(TransactionManager.getConnection(),"SELECT * FROM account WHERE name=?",new BeanHandler<Account>(Account.class),name);
    50 
    51     }
    52 
    53     
    54     
    55 }

    第三步:业务逻辑接口和业务逻辑的实现:AccountService  AccountServiceImply

     1 package com.beiwo.epet.service;
     2 
     3 public interface AccountService {
     4 
     5     public void transfer(String formName,String toName,int money);
     6     
     7 }
     8 
     9 
    10 
    11 
    12 
    13 
    14 package com.beiwo.epet.service.impl;
    15 
    16 import com.beiwo.epet.dao.AccountDao;
    17 import com.beiwo.epet.dao.impl.AccountDaoImpl;
    18 import com.beiwo.epet.entity.Account;
    19 import com.beiwo.epet.service.AccountService;
    20 import com.beiwo.epet.util.TransactionManager;
    21 
    22 public class AccountServiceImpl implements AccountService{
    23 
    24     @Override
    25     public void transfer(String formName, String toName, int money) {
    26         AccountDao accountDao=new AccountDaoImpl(); 
    27         
    28         try {
    29             ///开始一个事务,start transaction;
    30             //获取转入和转出的账户对象
    31             TransactionManager.startTransaction();
    32             
    33             Account fromAccount=accountDao.findAccountByName(formName);
    34             Account toAccount=accountDao.findAccountByName(toName);
    35             
    36             //修改账户的各自金额
    37             fromAccount.setMoney(fromAccount.getMoney()-money);
    38             toAccount.setMoney(toAccount.getMoney()+money);
    39             
    40             //完成转账的操作
    41             accountDao.updateAccount(fromAccount);
    42             
    43             int i=2/0;
    44             
    45             accountDao.updateAccount(toAccount);
    46               
    47             TransactionManager.commitTransaction();
    48             
    49         } catch (Exception e) {
    50             try {
    51                 TransactionManager.rollbackTransaction();;//事务的回滚
    52             } catch (Exception e2) {
    53                 e2.printStackTrace();
    54             }
    55             
    56         }finally{
    57             try {
    58                 TransactionManager.close();
    59             } catch (Exception e2) {
    60                 e2.printStackTrace();
    61             }
    62         }
    63         
    64         
    65     }
    66 
    67     
    68 }

    第四步:测试类 TesTransfer

     1 package com.beiwo.epet.test;
     2 
     3 import org.junit.Test;
     4 
     5 import com.beiwo.epet.service.AccountService;
     6 import com.beiwo.epet.service.impl.AccountServiceImpl;
     7 
     8 public class TestTransfer {
     9 
    10     
    11     @Test
    12     public void test(){
    13         AccountService accountService=new AccountServiceImpl();
    14         
    15         accountService.transfer("aaa", "bbb", 100);
    16         
    17     }
    18 }

    数据库的建立:

  • 相关阅读:
    记录-tomcat启动项目配置
    记录 -- js浏览器窗口关闭调用ajax
    远程桌面时出现身份验证错误,要求的函数不受支持
    记录-马斯洛需求层次理论模型
    记录-powerDesigner 导入sql文件注释问题
    记录-Java md5加密
    记录--js 剪贴板操作 (转载)
    如何判定一台计算机的唯一性
    GO_OOP简单摘要
    后台执行命令
  • 原文地址:https://www.cnblogs.com/lanyinhao/p/6196201.html
Copyright © 2011-2022 走看看