第一步创建实体类: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); }
}

数据库的建立:

DAO跨事物调用---转账的更多相关文章

  1. Dao跨事务调用实现转账功能

    1.首先在数据库当中创建数据库,并且创建它的 实现类 package com.beiwo.epet.entity; public class Account { private int id; pri ...

  2. 配置CORS解决跨域调用—反思思考问题的方式

    导读:最近都在用一套完整的Java EE的体系做系统,之前都是用spring框架,现在弄这个Java EE,觉得新鲜又刺激.但,由于之前没有过多的研究和使用,在应用的过程中,也出现了不少的问题.累积了 ...

  3. Atitit java c# php c++ js跨语言调用matlab实现边缘检测等功能attilax总结

    Atitit java c# php c++ js跨语言调用matlab实现边缘检测等功能attilax总结 1.1. 边缘检测的基本方法Canny最常用了1 1.2. 编写matlab边缘检测代码, ...

  4. AJAX跨域调用ASP.NET MVC或者WebAPI服务的解决方案

    问题描述 当跨域(cross domain)调用ASP.NET MVC或者ASP.NET Web API编写的服务时,会发生无法访问的情况. 重现方式 使用模板创建一个最简单的ASP.NET Web ...

  5. ajax——CORS跨域调用REST API 的常见问题以及前后端的设置

    RESTful架构是目前比较流行的一种互联网软件架构,在此架构之下的浏览器前端和手机端能共用后端接口. 但是涉及到js跨域调用接口总是很头疼,下边就跟着chrome的报错信息一起来解决一下. 假设:前 ...

  6. 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码】

    项目中常常用到搜索,特别是导航类的网站.自己做关键字搜索不太现实,直接调用百度的是最好的选择.使用jquery.ajax的jsonp方法可以异域调用到百度的js并拿到返回值,当然$.getScript ...

  7. js Ajax跨域调用JSON并赋值全局变量

    //跨域调用JSON <script type="text/javascript"> function _callback(obj) { alert(obj); } j ...

  8. ThinkPHP跨控制器调用方法

    跨控制器调用方法 1. 先造对象,再调用里面的方法 $sc=new \Home\Controller\IndexController();  用绝对路径找echo $sc->ShuChu(); ...

  9. 跨域调用webapi

    web端跨域调用webapi   在做Web开发中,常常会遇到跨域的问题,到目前为止,已经有非常多的跨域解决方案. 通过自己的研究以及在网上看了一些大神的博客,写了一个Demo 首先新建一个webap ...

随机推荐

  1. JavaScript知识点

    Day 01 1.Javascript 概述 1.什么是Javascript (JS) Javascript 是一种运行于 JS解释器/引擎 中的解释型脚本语言 JS解释器/引擎 :JS运行环境 1. ...

  2. warning 4510 with const member in struct

    I write a code section as this struct My{const int a;}; OK, then set the warning level then I will g ...

  3. Codeforces Round #341 Div.2 D. Rat Kwesh and Cheese

    嗯本来想着直接算出来不就行了吗 然后我想到了200^200^200....... 好吧其实也不难取两次log就行了 然后我第一次写出来log就写残了........... log里面的拆分要仔细啊.. ...

  4. GROUP与HAVING的使用

    SELECT pub_name, state,SUM(state) AS 总数 FROM publishers GROUP BY pub_name, state HAVING SUM(state)&g ...

  5. python 调用浏览器方法

    每天都要登陆某网站,刷积分.为了节省时间,用了下python中的webbrowser模块.新建.py 文件 #!/usr/bin/python import webbrowser webbrowser ...

  6. 在chrome 总调试cordova出现Detached from the target. Remote debugging has been terminated with reason: Connection lost. Please re-attach to the new target

    在chrome 总调试cordova出现如下错误: "Detached from the target. Remote debugging has been terminated with ...

  7. python运算符重载

    python运算符重载就是在解释器使用对象内置操作前,拦截该操作,使用自己写的重载方法. 重载方法:__init__为构造函数,__sub__为减法表达式 class Number: def __in ...

  8. 收集一些有用的docker镜像

    https://hub.docker.com/explore/  是star排名靠前的image =================================================== ...

  9. [转载]Eclipse调试Java的10个技巧

    原文:http://www.oschina.net/question/82993_69439 我也特别喜欢的是Drop to frame. 在看这篇文章前,我推荐你看一下Eclipse 快捷键手册,我 ...

  10. 1、SQL可搜索可排序可分页存储过程, 2、范围内的随机时间 适用于sql 2008以上

    -- ============================================= -- Author: 蜘蛛王 -- Create date: 2015-10-29 -- Descri ...