日志模块

image.png
打印日志是通过动态代理来拦截执行的方法,添加打印日志逻辑。

org.apache.ibatis.logging.jdbc.ConnectionLogger#invoke
image.png

数据源

image.png
image.png
PoolState中有集合存放连接。
PooledConnection是一个动态代理对象,当PooledConnection调用close时,不真正的关闭连接,而是放入DateSource的连接池中。
image.png

Mapper接口映射

image.png
MapperRegistry中有

  • Map, MapperProxyFactory<?>> knownMappers = new HashMap<>();

image.png
Mybatis初始化时读mapper.xml,扫描mapper接口。通过addMapper()添加到集合中。
MapperRegistry#getMapper,通过MapperProxyFactory创建Mapper。
image.png
MapperProxyFactory#newInstance(SqlSession)
创建Mapper动态代理
image.png
生成Mapper动态代理过程需要使用MapperProxy
MapperProxy implements InvocationHandler
MapperProxy执行invoke()中调用到MapperMethod执行方法。
image.png
MapperMethod#execute
image.png
MapperMethod内部的 SqlCommand command; 记录了SQL语句的名称和类型。
SqlCommand中的信息从MappedStatement中来。
image.png
MappedStatement从Configuration中来。
image.png

缓存

BlockingCache
image.png
FifoCache
一个队列用来记录cache的顺序。
image.png
LruCache
LruCache使用LinkedHashMap来维护Lru
image.png
image.png
SoftCache
使用软引用

Mybatis流程

解析全部信息记录到Configuration中。

MappedStatementf

image.png