Mybatis笔记
Mybatis是一个orm半自动化框架,目前在用以及学习次框架,所以追着代码研究Mybatis的一些原理,在此记录下笔记。
*mybatis初始化Configuration
这里没做过多研究,但之前有前辈的文章已经解析得很好了
http://blog.csdn.net/luanlouis/article/details/37744073
*把Mapper接口注册为bean,由Spring管理

MapperScannerConfigurer.postProcessBeanDefinitionRegistry-->
ClassPathMapperScanner.scan-->doScan
依次将此包下的mapper进行注册bean

Mybatis与数据库的交互方式有两种:
一种是传入statementId,有sqlsession执行相关操作
一种是直接调用mapper(实际还是sqlsession 执行相关操作)
有两种方式配置mapper.xml:
1.在SqlSessionFactoryBean配置mapperLocations

2.在上图中的mybatis.cfg.xml配置mapper节点:

*为mapper设置代理
在初始化的Configuration的过程中,会将mapper.xml文件进行解析,最终是XMLMapperBuilder类来解析。
XMLMapperBuilder.parse-->buildMapperForNamespace-->
Configuration.addMapper-->MapperRegistry.addMapper


由上图可知,最终是将namespace对应的类(Mapper接口),以键值对保存在MapperRegistry的knownMappers中,请注意,而值是一个改类所对应的代理工厂MapperProxyFactory
下面来看看,当spring获取mapper依赖的时候:
MapperFactoryBean.getObject

MapperRegistry.getMapper

最终返回的是一个代理

顺着源码一步步看,最终还是由sqlsession,查询MappedStateMent,调用Executor来执行相关命令



此图是实现sqlSession接口的DefualtSqlSession类的方法

以上纯属个人笔记,如有错误,请指导改正