1.获取SqlSessionFactory
/**
* @author:滕鹏飞
* @version: 1.0
* @Created on: 2018-8-31 下午2:10:45
* 类说明:加载配置文件,生成session
*/
public class DBUtil {
public static SqlSessionFactory sessionFactory;
static{
try {
//使用Mybatis提供的Resources类加载mybatis的配置文件
Reader reader = Resources.getResourceAsReader("mybatis.cfg.xml");
//构建session工厂
sessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
//创建能执行映射文件中SQL的SQLsession
public static SqlSession getSession(){
return sessionFactory.openSession(true);
}
}