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

缺点:Service层面把Dao层面的开启事务操作完成了

1.自行创建C3P0Uti,account数据库,导入Jar包


2.Dao层面

 接口:

package com.learning.dao;

import com.learning.domain.Account;

public interface AccountDao {
    /**
     * 转账
     * @param fromname 转出用户
     * @param toname  转入用户
     * @param money  转账金额
     */
    @Deprecated
    public void updateAccount(String fromname,String toname,double money)throws Exception;
    
    /**
     * 根据账户信息修改金额
     * @param accout
     */
    public void updateAccout(Account accout) throws Exception;
    
    /**
     * 根据用户名查找账户信息
     * @param name
     * @return
     * @throws Exception
     */
    public Account findAccountByName(String name)throws Exception;
}

实现类:

package com.learning.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

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

public class AccountDaoImpl implements AccountDao {

    private Connection conn;

    public AccountDaoImpl(Connection conn) {
        this.conn = conn;
    }

    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);
    }

    public void updateAccout(Account account) throws Exception {
        QueryRunner qr = new QueryRunner();
        qr.update(conn,"update account set money=? where name=?",account.getMoney(),account.getName());
    }

    public Account findAccountByName(String name) throws Exception {
        QueryRunner qr = new QueryRunner();
        return qr.query(conn,"select * from account where name=?", new BeanHandler<Account>(Account.class),name);
    }

}

3.Service层面

接口:

package com.learning.service;

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

实现类:

package com.learning.service.impl;

import java.sql.Connection;
import java.sql.SQLException;

import com.learning.dao.AccountDao;
import com.learning.dao.impl.AccountDaoImpl;
import com.learning.domain.Account;
import com.learning.service.AccountService;
import com.learning.util.C3P0Util;

public class AccountServiceImpl implements AccountService {

    public void transfer(String fromname, String toname, double money) {
    //    ad.updateAccount(fromname, toname, money);
        Connection conn = C3P0Util.getConnection();
        AccountDao ad = new AccountDaoImpl(conn);
        
        try {
            conn.setAutoCommit(false);//begin
            //分别得到转出和转入账户对象
            Account fromAccount = ad.findAccountByName(fromname);
            Account toAccount = ad.findAccountByName(toname);
            
            //修改账户各自的金额
            fromAccount.setMoney(fromAccount.getMoney()-money);
            toAccount.setMoney(toAccount.getMoney()+money);
            
            //完成转账操作
            ad.updateAccout(fromAccount);
//            int i = 10/0;
            ad.updateAccout(toAccount);
            
            conn.commit();//提交事务
        } catch (Exception e) {
            try {
                conn.rollback();//回滚事务
            } catch (SQLException e1) {
                e1.printStackTrace();
            } 
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }//关闭
        }
    }

}

 

posted @ 2017-04-04 13:41  第九种格调的人生  阅读(348)  评论(0编辑  收藏  举报