Mybatis工具类创建

MybatisUtils.java

package com.lying.utils;

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:Lying
 * @Date:2020/9/6
 * @Description:com.lying.utils
 * @version:1.0
 */
public class MybatisUtils {
     
    private static ThreadLocal<SqlSession> threadLocal=new ThreadLocal<>();
    private static SqlSessionFactory sqlSessionFactory =null;
    static{
     
        //创建SqlSessionFactory
        InputStream is=null;
        try{
     
            is= Resources.getResourceAsStream("mybatis-cfg.xml");
        }catch(IOException e){
     
            e.printStackTrace();
        }
        //实例化
        sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
    }
    //获取SqlSession
    public static SqlSession getSqlSession(){
     
        SqlSession sqlSession=threadLocal.get();
        if(sqlSession==null){
     
            sqlSession=sqlSessionFactory.openSession();
            threadLocal.set(sqlSession);
        }
        return sqlSession;
    }
    //关闭SqlSession
    public static void closeSqlSession(){
     
        SqlSession sqlSession=threadLocal.get();
        if(sqlSession!=null){
     
            sqlSession.close();
            threadLocal.set(null);
        }
    }


}

你可能感兴趣的:(Mybatis,java,mybatis,java)