使用MVC模式实现用户转账功能(区分dao层与service层)

现在我们要实现用户转账功能,之前在Mysql事务这片文章中也提到了用JDBC实现转账功能,今天我们加入MVC模式,使用MVC实现这个功能,进而区分dao层与service层的区别

首先编写好Account.java实体类

package com.qianfeng.domain;
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;}
}

接下来分析一下dao层与service层的不同,dao层是对数据库进行操作的一层,里面写的是sql语句,而service层是进行逻辑操作,以这个转账功能为例,dao层炫需要写的功能就是更新某个用户的账户钱数,所以就写一个更新用户信息的功能,而service层是进行逻辑操作的,进行某个用户向某个用户转账的实现,就是决定转账功能的,所以就写一个transfer方法就可以了
接口AccountDao

package com.qianfeng.dao;
import java.sql.SQLException;
import com.qianfeng.domain.Account;
public interface AccountDao {
    //更新用户信息
    public void updateAccount(Account account) throws SQLException;
    //根据用户名查询用户信息的功能
    public Account findAccountByName(String name) throws SQLException;
}

实现类AccountDaoImpl.java

package com.qianfeng.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.qianfeng.dao.AccountDao;
import com.qianfeng.domain.Account;
import com.qianfeng.util.C3P0Util;
public class AccountDaoImpl implements AccountDao {
    private Connection conn;
    public AccountDaoImpl(Connection conn)
    {
        this.conn = conn;
    }
    /**
     * 更新账户余额的功能
     */
    public void updateAccount(Account account) throws SQLException {
        QueryRunner qr = new QueryRunner();
        qr.update(conn,"update account set money=? where name=?", account.getMoney(),account.getName());
    }
    /**
     * 根据用户名查询用户信息的功能
     */
    public Account findAccountByName(String name) throws SQLException {
        QueryRunner qr = new QueryRunner();
        return qr.query(conn,"select * from account where name=?", new BeanHandler<Account>(Account.class),name);
    }
}

service层
接口AccountService

package com.qianfeng.service;
public interface AccountService {
    //转账的业务处理
    public void transfer(String fromname,String toname,int money);
}

实现类AccountServiceImpl.java

package com.qianfeng.service.impl;
import java.sql.Connection;
import java.sql.SQLException;
import com.qianfeng.dao.AccountDao;
import com.qianfeng.dao.impl.AccountDaoImpl;
import com.qianfeng.domain.Account;
import com.qianfeng.service.AccountService;
import com.qianfeng.util.C3P0Util;
public class AccountServiceImpl implements AccountService {

    private AccountDao  accountDao;

    public void transfer(String fromname, String toname, int money)  {
            Connection conn = null;
          try {
            conn = C3P0Util.getConnection();
            accountDao = new AccountDaoImpl(conn);

            conn.setAutoCommit(false);
            //得到转账人的信息
            Account from = accountDao.findAccountByName(fromname);
            //得到收钱人的信息
            Account to = accountDao.findAccountByName(toname);

            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);

            //转账
            accountDao.updateAccount(from);
            int num = 5/0;
            accountDao.updateAccount(to);

            conn.commit();
        } catch (Exception e) {
            try {
                conn.rollback();
                e.printStackTrace();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }
}

简单实现测试

package com.qianfeng.test;
import com.qianfeng.service.AccountService;
import com.qianfeng.service.impl.AccountServiceImpl;
public class TestDemo {
    public static void main(String[] args) {
        AccountService accountService = new AccountServiceImpl();
        accountService.transfer("lisi", "zhaosi", 100);
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值