模拟转账

model

public class Account {
    private int id;
    private String name;
    private double 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 double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String

    toString() {
        return "UD{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

dao层

//接口
public interface IAccountDao {
    /**
     * 根据名字查找账户信息
     *
     * @param name
     * @return
     */
    public Account findAccount(String name) throws SQLException;

    /**
     * 根据名字查找账号信息
     *
     * @param account
     */
    public void updateAccount(Account account) throws SQLException;
}

//实现类
public class AccountDao implements IAccountDao {
    private Connection con;

    @Override
    public Account findAccount(String name) {
        QueryRunner qr = new QueryRunner();
        String sql = "SELECT * FROM account WHERE name = ?";
        Account account = null;
        try {
            account = qr.query(ManagerThreadLocal.getConnection(), sql, new BeanHandler<Account>(Account.class), name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return account;
    }

    @Override
    public void updateAccount(Account account) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql = "UPDATE account SET MONEY = ? WHERE ID = ?";
        qr.update(ManagerThreadLocal.getConnection(), sql, account.getMoney(), account.getId());
    }
}

service层

//接口
public interface IAccountService {
    //转账
    public void transfer(String from, String to, double amount);
}

//实现类
public class AccountServiceImpl implements IAccountService {
    @Override
    public void transfer(String from, String to, double amount) {
        try {
            IAccountDao iAccountDao = new AccountDao();
            //开启事务
            ManagerThreadLocal.beginTransaction();
            //1.获取from的账号信息
            Account fromAccount = iAccountDao.findAccount(from);
            //2.获取to的账号信息
            Account toAccount = iAccountDao.findAccount(to);
            //3.修改model的money信息
            fromAccount.setMoney(fromAccount.getMoney() - amount);
            toAccount.setMoney(toAccount.getMoney() + amount);
            //4.操作数据库
            iAccountDao.updateAccount(fromAccount);
            //int i = 10 / 0;
            iAccountDao.updateAccount(toAccount);
            //提交事务
            ManagerThreadLocal.commitTransaction();
        } catch (SQLException e) {
            //回滚事务
            ManagerThreadLocal.rollbackTransaction();
        } finally {
            ManagerThreadLocal.close();//放回连接池
        }
    }
}

THREADLOCAL

public class ManagerThreadLocal {
    private static ThreadLocal<Connection> tl = new ThreadLocal<>();

    public static Connection getConnection() {
        Connection con = tl.get();
        //第一次为空
        if (con == null) {
            try {
                con = DBC3P0Utils.getConnection();
                tl.set(con);
                System.out.println(1);
                return con;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            con = tl.get();
            System.out.println(2);
            return con;
        }
        return null;
    }

    /**
     * 开启事务
     */
    public static void beginTransaction() {
        try {
            getConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 回滚事务
     */
    public static void rollbackTransaction() {
        try {
            getConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 提交事务
     */
    public static void commitTransaction() {
        try {
            getConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /**
     * 关闭连接
     */
    public static void close() {
        try {
            getConnection().close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

单元测试

public class T01 {
    @Test
    public void test() {
        IAccountService iAccountService = new AccountServiceImpl();
        iAccountService.transfer("aaa","bbb",100);
    }
    @Test
    public void test2() {
        Connection con1 = ManagerThreadLocal.getConnection();
        Connection con2 = ManagerThreadLocal.getConnection();
        System.out.println(con1);
        System.out.println(con2);
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值