MyBatis核心接口和类
三大对象:
1、SqISessionFactoryBuilder: 负责构建SqlSessionFactory,并且提供了多个build( )方法的重载
2、SqlSessionFactory: 创建SqlSession实例的工厂
3、SqlSession: 用于执行持久化操作的对象
三大对象获取的步骤:
(1)每个MyBatis的应用程序都以一个SqlSessionFactory对象的实例为核心。
(2)首先获取SqlSessionFactoryBuilder对象,可以根据XML配置文件的实例构建该对象。
(3)然后获取SqlSessionFactory对象,该对象实例可以通过SqlSessionFactoryBuilder对象来获得。
(4)有了SqlSessionFactory对象之后,通过SqlSessionFactory对象的openSession()方法就可以获取SqlSession实例,SqlSession对象中完全包含以数据库为背景的所有执行SQL操作的方法。
三大对象的声明周期和作用域:
《1》SqlSessionFactoryBuilder的最大特点是:用过即丢。一旦创建了SqlSessionFactoryBuilder对象之后,这个类就不再需要存在了,因此SqlSessionFactoryBuilder的最佳范围是存在方法体内,也就是局部变量而已。
《2》SqlSessionFactory对象一旦创建,就会在整个应用运行过程中始终存在,因此SqlSessionFactory的最佳作用域是Application(单例模式)。
《3》SqlSession对应着一次数据库回话。在每次访问数据库时都需要创建它,每个线程都有自己的SqlSession实例,SqlSession实例不能被共享,也不是线程安全的。因此最佳的作用域范围是request作用域或者方法体作用域内。