把mybatis和spring集成在一起,向一个框架一样使用。用的技术是ioc。
为什么是ioc:
能把spring和mybatis集成在一起,像一个框架,是因为ioc能创建对象。可以把mybatis框架中的对象交给spring统一创建,开发人员从spring中获取对象,然后就不用同时面对两个或多个框架了,就面对一个spring。
mybatis使用步骤,对象
1、定义dao接口:StudentDao
2、定义mapper文件 StudentDao.xml
3、定义mybatis的主配置文件 mybatis.xml
4、创建dao的代理对象,StudentDao dao = SqlSession.getMapper(StudentDao.class);
List<Student> students = dao.selectStudents();
要使用dao对象,需要使用getMapper()方法,怎么能使用getMapper()方法,需要哪些条件
1、获取SqlSession对象,需要使用SqlSessionFactory的openSession()方法
2、创建SqlSessionFactory对象。通过读取mybatis的主配置文件,能创建SqlSessionFactory对象
总结就是:需要SqlSessionFactory对象,使用Factory能获取SqlSession,有了SqlSession就能有dao,目的就是获取dao对象,Factory创建需要读取主配置文件
原文:https://www.cnblogs.com/stu-jyj3621/p/14532594.html