package com.utils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; /*工具类:SqlSessionFactory数据库会话工厂工具*/ public class SqlSessionFactoryUtils { //定义一个SqlSessionFactory的对象 public static SqlSessionFactory sqlSessionFactory = null; //定义一个静态方法,获得SqlSessionFactory public static SqlSessionFactory getSqlSessionFactory() { //步骤1:读取mybatis 主配置文件。获取数据库相关的配置信息 InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream("mybatis-config.xml"); //步骤2:通过配置文件流input构建出工厂类 sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } return sqlSessionFactory; } }