转账示例(一):Dao层面实现(本例采用QueryRunner来执行sql语句,数据源为C3P0)...

缺点:Dao层面把Service层面的操作完成了,不利于后期的代码修改和重构

1.自行创建C3P0Util

account数据库

 

2.jar包

3.Dao层面

接口:

package com.learning.dao;

import com.learning.domain.Account;

public interface AccountDao {
    /**
     * 转账
     * @param fromname 转出用户
     * @param toname  转入用户
     * @param money  转账金额
     */
    public void updateAccount(String fromname,String toname,double money)throws Exception;
}

 实现类:

package com.learning.dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

import com.learning.dao.AccountDao;
import com.learning.util.C3P0Util;

public class AccountDaoImpl implements AccountDao {

    public void updateAccount(String fromname, String toname, double money) throws Exception {
        //创建一个QueryRunner对象
        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);
    }

}

 

转载于:https://www.cnblogs.com/youwillsee/p/6664836.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值