使用Dao层、测试层、业务层以及工具类完成账户的收转账

1.测试类

 AccountServiceImpl accountService=new AccountServiceImpl();
        //实例化业务层对象,调取业务层方法
        int res=accountService.transfor("10086","123","10010",100.5);
        System.out.println("转账"+res);

2.业务层

 public int transfor(String toAccount, String password, String fromAccount, double money) {
        AccountDaoImpl accountDao=new AccountDaoImpl();
        Account toAcc=accountDao.selectCart(toAccount);//1.首先判断发款方与收款方账户是否存在
        Account fromAcc=accountDao.selectCart(fromAccount);
        if (toAcc.getCart()==null){//i.查询转账方账户是否存在
            throw new RuntimeException("转账方账户不存在");
        }
        if (!toAcc.getPassword().equals(password)){//ii.查询转账方账户密码是否正确
            throw new RuntimeException("转账方密码不正确");
        }
        if (toAcc.getMoney()<money){//iii.查看转账方账户余额是否充足
            throw new RuntimeException("账户余额不足");
        }
        if (fromAcc.getCart()==null){//iv.查看收款方账户是否存在
            throw new RuntimeException("收款方账户不存在");
        }
        Connection conn=null;
        int res=0;
        try {
            DruidBUtils.begin();
            toAcc.setMoney(toAcc.getMoney()-money);//v.完成转账方发钱与收款收账方的收款功能,并返回转账是否有效给测试层
            res=accountDao.update(toAcc);//2.更新账余额,返回给前端
            int i=1/0;
            fromAcc.setMoney(fromAcc.getMoney()+money);
            res=accountDao.update(fromAcc);
            DruidBUtils.commit();
        } catch (Exception e) {
            System.out.println("事务的回滚");
            try {
                DruidBUtils.rollback();
            } catch (Exception ex) { ex.printStackTrace(); }
        }
        return res;
    }

3.Dao层即数据访问层

public Account selectCart(String toAccount) {
        String sql="select * from account where cart=?";
        List<Account> list=DaoUtils.commonsQuery(sql,Account.class,toAccount);
        if (list.size()>0){
            return list.get(0);
        }
        return null;
    }

    public int update(Account toAcc) {
        String sql="update account set money=? where cart=?";
        return DaoUtils.commonsUpdate(sql,toAcc.getMoney(),toAcc.getCart());
    }

4.工具类

 public static int commonsUpdate(String sql,Object...ob){//传入sql语句以及可变长参数代表多个属性
        //增删改DAO的代码类似因此封装在同一个方法中
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            conn= DruidBUtils.getConnection();
            ps=conn.prepareStatement(sql);
            //这里采用for循环将每一个属性挨个赋值
            for (int i=0;i<ob.length;i++){
                //这里一会测试以下i能否为1
                ps.setObject(i+1,ob[i]);
            }
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DruidBUtils.closeAll(ps);
        }
        return 0;
    }
    public static <T> List<T> commonsQuery(String sql,Class<T> cla,Object...ob){
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        List<T> list=new ArrayList<>();
        try {
            conn= DruidBUtils.getConnection();
            ps=conn.prepareStatement(sql);
            for (int i=0;i<ob.length;i++){
                //这里一会测试以下i能否为1
                ps.setObject(i+1,ob[i]);
            }
            rs=ps.executeQuery();
            //
            while (rs.next()){
                Field[] fields=cla.getDeclaredFields();
                T t=cla.newInstance();
                for (Field f:fields){
                  Object value= rs.getObject(f.getName());
                    f.setAccessible(true);
                  f.set(t,value);
                }
                list.add(t);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DruidBUtils.closeAll(rs,ps);
        }
        return null;
    }

5.Druid工具类

 private static final ThreadLocal<Connection> TH=new ThreadLocal<>();
    private static DataSource dataSource;
    private static Properties p=new Properties();//实例化map集合对象
    static {//静态代码块只加载一次
        try {
            p.load(new FileInputStream("user.properties"));
            Class.forName(p.getProperty("driver"));//1.加载驱动
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
        //2.获取连接对象,并返回
        Connection conn=TH.get();
        if (conn==null){
            conn= DriverManager.getConnection(p.getProperty("url"),p.getProperty("user"),p.getProperty("password"));
            TH.set(conn);
        }
       return conn;
    }
    public static void begin() throws SQLException {//开启事务
        Connection conn=getConnection();//获取连接对象
        conn.setAutoCommit(false);//开启事务
    }
    public static void commit() throws SQLException {
        Connection conn=getConnection();
        conn.commit();
        TH.remove();//移除ThreadLocal
        closeAll(conn);//关闭
    }
    public static void rollback()  {
        Connection conn= null;
        try {
            conn = getConnection();
            conn.rollback();
            TH.remove();//移除ThreadLocal
            closeAll(conn);//关闭接口
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void closeAll(AutoCloseable...ac){
        for (AutoCloseable c:ac){
            if (c!=null){
                try {
                    c.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值