单独使用MyBaits步骤:创建SqlSessionFactory类,调用openSession方法创建出SqlSession会话对象出会话窗口
public class MyBatis {
//建造一个工厂
static SqlSessionFactory sessionFactory;
static {//获得配置文件
String r="com/tlm/config/mybatis-config.xml";
InputStream inputStream=null;
try {
inputStream= Resources.getResourceAsStream( r );
sessionFactory=new SqlSessionFactoryBuilder().build( inputStream );
} catch (IOException e) {
e.printStackTrace();
}
}
//创建SqlSession会话对象
public static SqlSession getSqlSession(){
return sessionFactory.openSession();
}
}
利用会话窗口结合反射的动态代理模式实现接口的动态代理对象,结合接口与映射文件完成持久层结构
public List整合使用,步骤:getStudents() { List list=null; //得到Sqlsession SqlSession sqlSession= MyBatis.getSqlSession(); try {// 反射的动态机制 给出一个代理接口对象 StudentDao studentDao=sqlSession.getMapper( StudentDao.class ); list=studentDao.getStudents(); }catch (Exception e){ e.printStackTrace(); }finally { sqlSession.close(); } return list; }
1.导入相关jar包;
2.在Spring框架中配置MyBatis :
重点:整合框架用到的对象不是SqlSessionFactory而是用到SqlSessionFactoryBean



