mybatis在三层架构中使用事务

在三层架构中使用事务

  • 在web项目中使用ThreadLocal可以实现将线程与数据(SqlSession)之间的绑定,确保每个线程访问的数据是独立的,不会相互干扰。
  • ThreadLocal底层使用的是一个map,key是线程对象,value为我们自定义的对象(SqlSession对象)
  • 通过ThreadLocal的使用可以保证在一个线程中,使用的SqlSession对象始终为同一个,这样在进行事务操作时,就可以将提交事务和关闭session都放在servlet层

自己封装sqlsession工具类


public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    //全局的,用于分发各线程唯一session
    private static ThreadLocal<SqlSession> local =new ThreadLocal<>();
    static {
        try {
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static SqlSession getSqlSession() throws IOException {
        // 获取当前线程对应的SqlSession对象
        SqlSession session = local.get();
        if (session == null) {
            session = sqlSessionFactory.openSession();
            //将sqlSession对象绑定到当前线程上
            local.set(session);
        }
        return sqlSessionFactory.openSession();
    }
    public static void closeSqlSession() {
        //获取当前线程对应SqlSession对象
        SqlSession session = local.get();
        if (session != null) {
            //若当前SqlSession对象不为空
            //则关闭当前的SqlSession对象,并在ThreadLocal中移除以当前线程为key的键值对
            session.close();
            local.remove();
        }
    }
}

service层

public class CustomerServiceImpl implements CustomerService {

    private CustomerDao customerDao = new CustomerDaoImpl();

    @Override
    public void transfer(int tId, int sId, int capital) throws IOException {

        SqlSession sqlSession = MybatisUtils.getSqlSession();

        int tCapital = customerDao.searchCapital(tId);

        if(tCapital >= capital) {
            customerDao.subCapital(tId, capital);
            customerDao.addCapital(sId, capital);

        }else {
            System.out.println("the capital is not enough");
        }

        sqlSession.commit();
        MybatisUtils.closeSqlSession();

    }
}

dao层

public class CustomerDaoImpl implements CustomerDao {

    SqlSession sqlSession;

    @Override
    public void subCapital(int id, int capital) throws IOException {
        //由于mybatis底层获取参数是利用了get方法,故传递多个参数时,需要将其封装成一个map
        sqlSession = MybatisUtils.getSqlSession();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("capital", capital);
        sqlSession.update("subCapital",params);
    }

    @Override
    public void addCapital(int id, int capital) throws IOException {
        sqlSession = MybatisUtils.getSqlSession();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("capital", capital);
        sqlSession.update("addCapital",params);
    }

    @Override
    public int searchCapital(int id) throws IOException {
        sqlSession = MybatisUtils.getSqlSession();
        int capital = sqlSession.selectOne("selectCapital", id);
        return capital;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值