public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { //获取工具类SqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } //sqlSessionFactory获取session实例 //自动提交事务 public static SqlSession getSqlSession() { return sqlSessionFactory.openSession(true); } }