项目整理
Factory是mybatis连接数据库的工具类,进行了封装避免每次都要写重复步骤。
package factory;
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;
public class SqlsessionFactory {
private static SqlSessionFactory sqlSessionFactory;
static {
//mybatis框架都在xml配置文件中加载配置文件
//从 XML 文件中构建 SqlSessionFactory 的实例
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
}
public SqlSessionFactory SqlsessionFactory(){
return sqlSessionFactory;
}
}