六.MyBatis

MyBatis的执行步骤

  1. 创建SqlSessionFactory对象
  2. 通过SqlSessionFactory获取SqlSession对象
  3. 通过SqlSession获取Mapper代理对象
  4. 通过Mapper代理对象,执行数据库操作
  5. 执行成果,使用SqlSession提交事务
  6. 执行失败,使用SqlSession回滚事务
  7. 关闭会话

MyBatis有哪些Executor执行器?它们之间的区别是什么?

默认有四种执行器:SimpleExecutor、ReuseExecutor、BatchExecutor、CachingExecutor

  1. SimleExecutor:每次执行update或select操作,就会创建一个statement对象,用完立刻关闭
  2. ReuseExecutor:执行update或select操作,以sql作为key查找缓存的statement对象,存在就使用,不存在就创建,不关闭statement对象,而是放置于缓存Map<String,Statement>内,拱下一次使用
  3. BatchExcutor:执行update操作,将所有sql都添加到批处理中,等待统一执行。它缓存了多个statement对象,每个statement对象都是调用addBatch方法完毕后,等待一次执行executeBatch批处理。
  4. CachingExecutor:二级缓存功能

MyBatis的插件运行原理?以及如何编写一个插件

Mybatis仅可以编写针对ParameterHandler、ResultSetHandler、StatementHandler、Executor这4种接口的插件
使用JDK的动态代理实现,每当执行这4种接口对象的方法时,都会进入拦截方法
编写插件步骤:

  1. 实现MyBatis的Interceptor接口,并实现intercepet方法
  2. 给插件编写注解,指定拦截哪一个接口的哪些方法
  3. 最后在配置文件中配置你编写的插件

猜你喜欢

转载自blog.csdn.net/qq_37629227/article/details/112919545