【MyBatis】MybatisUtil工具类的作用(五)

来源:互联网 发布:开源人工智能语音系统 编辑:程序博客网 时间:2024/05/16 14:18


1)在静态初始化块中加载mybatis配置文件和StudentMapper.xml文件一次

2)使用ThreadLocal对象让当前线程与SqlSession对象绑定在一起

3)获取当前线程中的SqlSession对象,如果没有的话,从SqlSessionFactory对象中获取SqlSession对象

4)获取当前线程中的SqlSession对象,再将其关闭,释放其占用的资源

/** * 工具类 * @author LFSenior */public class MybatisUtil {private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();private static SqlSessionFactory sqlSessionFactory;/** * 加载位于src/mybatis.xml配置文件 */static{try {Reader reader = Resources.getResourceAsReader("mybatis.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);} catch (IOException e) {e.printStackTrace();throw new RuntimeException(e);}}/** * 禁止外界通过new方法创建 */private MybatisUtil(){}/** * 获取SqlSession */public static SqlSession getSqlSession(){//从当前线程中获取SqlSession对象SqlSession sqlSession = threadLocal.get();//如果SqlSession对象为空if(sqlSession == null){//在SqlSessionFactory非空的情况下,获取SqlSession对象sqlSession = sqlSessionFactory.openSession();//将SqlSession对象与当前线程绑定在一起threadLocal.set(sqlSession);}//返回SqlSession对象return sqlSession;}/** * 关闭SqlSession与当前线程分开 */public static void closeSqlSession(){//从当前线程中获取SqlSession对象SqlSession sqlSession = threadLocal.get();//如果SqlSession对象非空if(sqlSession != null){//关闭SqlSession对象sqlSession.close();//分开当前线程与SqlSession对象的关系,目的是让GC尽早回收threadLocal.remove();}}}

  

 

 

原创粉丝点击