Service开发流程(转账业务)

Service开发流程(转账业务)

一、业务需求和逻辑

(1)业务需求:

  • 一个账户向另一个账户转账

(2)业务逻辑:

  1. 首先在数据库中查询转账账号是否存在
  2. 然后在数据库中查询转账账号密码是否存在
  3. 再验证转账账号的余额是否足够转账
  4. 再验证收款账号是否存在
  5. 执行转账操作,转账账号余额减少,收款账号余额增加(减少的金额与增加的金额相等)

二、业务实现步骤

提前准备:创建lib目录,将mysql架包文件配好

创建包列表:

在这里插入图片描述

1、创建表account
CREATE TABLE IF NOT EXISTS `account`(
`cardNo` VARCHAR(20) PRIMARY KEY COMMENT '卡号',
`password` VARCHAR(20) NOT NULL COMMENT '密码',
`name` VARCHAR(20) NOT NULL COMMENT '名称',
`balance` DOUBLE NOT NULL COMMENT '账户余额'
);
2、向account表中插入数据
INSERT INTO account VALUES('6001','123456','zhangsan',10000);
INSERT INTO account VALUES('6002','123456','lisi',5000);
3、封装实体类Account
package com.bdqn.entity;


/*entity实体类Account类
 */

public class Account {
    /** 账号 */
    private String cardNo;
    /** 密码 */
    private String password;
    /** 用户名 */
    private String name;
    /** 账户余额 */
    private double balance;

    // 无参构造方法
    public Account() {
        super();
    }

    // 有参构造方法
    public Account(String cardNo, String password, String name, double balance) {
        super();
        this.cardNo = cardNo;
        this.password = password;
        this.name = name;
        this.balance = balance;
    }

    // getXxx()/setXxx()方法
    public String getCardNo() {
        return cardNo;
    }

    public void setCardNo(String cardNo) {
        this.cardNo = cardNo;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    // 重写Object类中的toString()方法
    @Override
    public String toString() {
        return "cardNo=" + cardNo + ", password=" + password + ", name=" + name
                + ", balance=" + balance;
    }


}

4、 编写AccountDao接口

​ 这里呢,我们采用通过接口去定义抽象方法,目的是为了防止在实际的开发过程中呢,大家因为方法名不同导致合作效率低。

package com.bdqn.AccountDaoImpl;

import com.bdqn.entity.Account;

import java.util.List;

public interface AccountDao {
    //增
    int insert(Account account);
    //删
    int delete(String cardNo);
    //改
    int update(Account account);
    //查单个
    Account select(String cardNo);
    //查所有
    List<Account> selectAll();
}

5.编写AccountDaoImpl类(Dao层代码)
package com.bdqn.AccountDaoImpl;

import com.bdqn.entity.Account;
import com.bdqn.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AccountDaoImpl implements AccountDao {
    @Override
    public int insert(Account account) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        //准备SQL语句
        String sql = "INSERT INTO `account` (cardNo,password,`name`,balance) VALUES (?,?,?,?);";
        connection = DBUtils.getConnection();
        try {
            preparedStatement = connection.prepareStatement(sql);
            //绑定参数赋值
            preparedStatement.setString(1,account.getCardNo());
            preparedStatement.setString(2,account.getPassword());
            preparedStatement.setString(3,account.getName());
            preparedStatement.setDouble(4,account.getBalance());

            //执行SQL语句
            int result = preparedStatement.executeUpdate();

            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtils.closeAll(connection,preparedStatement,null);
        }

    }

    @Override
    public int delete(String cardNo) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        //准备SQL语句
        String sql = "DELETE *FROM WHERE cardNo=?;";
        connection = DBUtils.getConnection();
        try {
            preparedStatement = connection.prepareStatement(sql);
            //绑定参数赋值
            preparedStatement.setString(1,cardNo);
            //执行sql语句
            int result =preparedStatement.executeUpdate();

            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtils.closeAll(connection,preparedStatement,null);
        }
    }

    @Override
    public int update(Account account) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        //准备SQL语句
        String sql = "UPDATE `account` SET password=?,`name`=?,balance=? WHERE cardNo=?;";
        connection = DBUtils.getConnection();
        try {
            preparedStatement = connection.prepareStatement(sql);
            //绑定参数赋值
            preparedStatement.setString(1,account.getPassword());
            preparedStatement.setString(2,account.getName());
            preparedStatement.setDouble(3,account.getBalance());
            preparedStatement.setString(4,account.getCardNo());

            //执行sql语句
            int result = preparedStatement.executeUpdate();
            return result;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtils.closeAll(connection,preparedStatement,null);
        }

    }

    @Override
    public Account select(String cardNo) {
        Account account = null;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //准备sql语句
        String sql = "SELECT *FROM `account` WHERE cardNo=?;";
        connection = DBUtils.getConnection();
        try {
            preparedStatement = connection.prepareStatement(sql);
            //绑定参数赋值
            preparedStatement.setString(1,cardNo);

            //执行SQL语句
            resultSet = preparedStatement.executeQuery();

            if(resultSet.next()){
                String cardNo1= resultSet.getString("cardNo");
                String password = resultSet.getString("password");
                String name = resultSet.getString("name");
                Double balance = resultSet.getDouble("balance");
                account = new Account(cardNo1,password,name,balance);
            }
            return account;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtils.closeAll(connection,preparedStatement,null);
        }

    }

    @Override
    public List<Account> selectAll() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet =null;
        Account account = null;
        List<Account> list = new ArrayList<Account>();
        //准备SQL语句
        String sql = "SELECT *FROM `account` WHERE cardNo=?;";
        connection = DBUtils.getConnection();
        try {
            preparedStatement = connection.prepareStatement(sql);
          //执行SQL语句
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()){
                String cardNo = resultSet.getString("cardNo");
                String password= resultSet.getString("password");
                String name = resultSet.getString("name");
                Double balance = resultSet.getDouble("balance");
                account = new Account(cardNo,password,name,balance);
                list.add(account);
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtils.closeAll(connection,preparedStatement,null);
        }

    }
}

6.编写AccountService接口代码:
package com.bdqn.service;

public interface AccountService {
    void transfer(String fromNo, String password, String toNo, double money);
}

7.编写AccountServiceImpl类(业务层代码)
package com.bdqn.service;

import com.bdqn.AccountDaoImpl.AccountDaoImpl;
import com.bdqn.entity.Account;

public class AccountServiceImpl implements AccountService {
    /**
     * 转账业务
     *
     * @param fromNo   转账人账号
     * @param password 转账人账号密码
     * @param toNo     收款人账号
     * @param money    转账金额
     */
    @Override
    public void transfer(String fromNo, String password, String toNo, double money) {
        AccountDaoImpl accountDaoImpl = new AccountDaoImpl();

        try {
            // 1.验证fromNo账号是否存在
            Account fromAccount = accountDaoImpl.select(fromNo);
            if (fromAccount == null) {

                throw new RuntimeException("卡号不存在!");
            }

            //2.验证password密码是否正确
            if (!fromAccount.getPassword().equals(password)) {

                throw new RuntimeException("密码错误!");
            }
            //3.验证余额是否充足
            if (fromAccount.getBalance() < money) {

                throw new RuntimeException("抱歉,您的余额不足!");
            }
            //4.验证收款人卡号是否存在
            Account toAccount = accountDaoImpl.select(toNo);
            if (toAccount == null) {
                throw new RuntimeException("对方卡号不存在!");
            }
            //5.减少fromNo账号的余额
            fromAccount.setBalance(fromAccount.getBalance() - money);
            accountDaoImpl.update(fromAccount);

            // 6.增加toNo账号的余额
            toAccount.setBalance(toAccount.getBalance() + money);
            accountDaoImpl.update(toAccount);
            System.out.println("转账成功");
        } catch (Exception e) {
            System.out.println("转账失败");
            e.printStackTrace();
        }
    }
}



8.编写AccountTest,测试转账业务
package com.bdqn.Test;

import com.bdqn.service.AccountServiceImpl;

public class AccountTest {
    public static void main(String[] args) {
        AccountServiceImpl accountServiceImpl = new AccountServiceImpl();
        accountServiceImpl.transfer("6001","123456","6002",2000);
    }
}

9.工具类(DBUtils)

​ 目的:为了简便代码量,DBUtils工具类中定义了2个方法:

​ 作用:1.getConnection()方法是为了获取SQL连接

​ 2.closeAll()方法是为了释放缓存资源

package com.bdqn.utils;

import java.sql.*;

public class DBUtils {
    static {

        //注册驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    //获取连接
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbcdatabase","root","123456");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }

    //释放资源
    public static void closeAll(Connection connection, Statement statement, ResultSet resultSet){
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值