MybatisUtil的工具类 代理对象

package util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 
 * @version 1.0
 * @date 2022/1/10 21:31
 */
public class SqlSessionUtil {
    private static SqlSessionFactory sqlSessionFactory;

    static {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<>();

    /**
     * 获取连接
     *
     * @return
     */
    public static SqlSession getSession() {
        //那到sqlSession对象
        SqlSession session = threadLocal.get();

        if (session == null) {
            //如果没有则创建
            session = sqlSessionFactory.openSession();
            //向线程池中放一个session对象
            threadLocal.set(session);
        }
        return session;
    }

    /**
     * 关闭连接
     */
    public static void close(SqlSession session) {
        if (session != null) {
            //关闭
            session.close();
            //必须要写 这一步  刷新线程池的sqlsession状态
            threadLocal.remove();
        }
    }
}

代理对象 用来处理事务

package util;

import org.apache.ibatis.session.SqlSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author 
 * @version 1.0
 * @date 2022/1/10 22:09
 */
public class TransactionProxy implements InvocationHandler {
    //真实对象
    Object object;

    //有参构造
    public TransactionProxy(Object object) {
        this.object = object;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //方法的返回值类型
        Object obj = null;
        SqlSession session = null;
        try {
            //获取SQLsession对象
            session = SqlSessionUtil.getSession();
            obj = method.invoke(object, args);
            session.commit();
        } catch (Exception e) {
            //session不为null则回滚
            assert session != null;
            session.rollback();
            e.printStackTrace();
        } finally {
            //关闭流
            SqlSessionUtil.close(session);
        }
        return obj;
    }

    /**
     * 获取代理类对象
     *
     * @return
     */
    public Object getProxy() {
        return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), this);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值