MyBatis工具类
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//使用Mybatis第一步:获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//有了sqlSessionFactory,可以从中获得SqlSession实例
//SqlSession 完全包含了面向数据库执行SQL命令所需的方法
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}