package com.yuyu.srdisassemble.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; public class MybatisUtil { private static final SqlSessionFactory factory; static { InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream("mybatis-config.xml"); factory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { throw new RuntimeException("Failed to initialize MyBatis SqlSessionFactory", e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public MybatisUtil( ) { } public static SqlSession getSqlSession() { return factory.openSession(); } public static void close(SqlSession sqlSession) { if (sqlSession != null) { sqlSession.close(); } } }