博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis--MapperScannerConfigurer
阅读量:7225 次
发布时间:2019-06-29

本文共 2487 字,大约阅读时间需要 8 分钟。

一般我们这样配置

内部使用 ClassPathMapperScanner 来扫描包下面的mapper接口,每个接口构建一个BeanDefinitionHolder(beanclass为MapperFactoryBean)

当需要mapperinterface实例时,由MapperFactoryBean工厂产生

public T getObject() throws Exception {        return this.getSqlSession().getMapper(this.mapperInterface);    }

可以吧this.getSqlSession()看做是sqlSessionFactory的封装SqlSessionTemplate,实际调用

public 
T getMapper(Class
type) {
return this.getConfiguration().getMapper(type, this); }

这里的this.getConfiguration()实际是

return this.sqlSessionFactory.getConfiguration();

 

走到这步,就必须来看SqlSessionFactoryBean做了什么吧?关键方法如下:

protected SqlSessionFactory buildSqlSessionFactory() throws IOException {    XMLConfigBuilder xmlConfigBuilder = null;    Configuration configuration;    ..................     if(this.transactionFactory == null) {            this.transactionFactory = new SpringManagedTransactionFactory();     }     Environment var29 = new Environment(this.environment, this.transactionFactory, this.dataSource);     configuration.setEnvironment(var29);   ...............               if(!ObjectUtils.isEmpty(this.mapperLocations)) {           XMLMapperBuilder e = new XMLMapperBuilder(var33.getInputStream(), configuration, var33.toString(), configuration.getSqlFragments());           e.parse();        }        return this.sqlSessionFactoryBuilder.build(configuration);}

红色部分,设置configuration->environment->springManagedTransactionFactory

XMLMapperBuilder 详细过程见  ,parse中xml的namespace相应的class注册给configuration

public 
void addMapper(Class
type) {
this.mapperRegistry.addMapper(type); }

最后的return调用 SqlSessionFactoryBuilder

public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config); }

这里的Configuration对象很关键

 

总结如下:org.mybatis.spring.SqlSessionFactoryBean解析mapper xml配置,根据namespace添加class mapper组装Configuration对象,包装为DefaultSqlSessionFactory;

org.mybatis.spring.mapper.MapperScannerConfigurer扫描basePackage下的mapper接口,封装为MapperFactoryBean注册给spring容器,当调用"mapper接口"时去DefaultSqlSessionFactory-》Configuration获得接口类的代理类

MapperProxy mapperProxy = new MapperProxy(sqlSession, this.mapperInterface, this.methodCache);

进一步的调用过程见 

 

如何在调用中事务起作用,见下篇

 

转载于:https://www.cnblogs.com/yhzh/p/5587465.html

你可能感兴趣的文章
gitlab-ci配置详解(一)
查看>>
听说你叫Java(二)–Servlet请求
查看>>
案例分享〡三拾众筹持续交付开发流程支撑创新业务
查看>>
FreeWheel业务系统微服务化过程经验分享
查看>>
移动互联网下半场,iOS开发者如何“高薪”成长?
查看>>
Atlassian是怎样进行持续交付的?且听 Steve Smith一一道来
查看>>
Web Storage相关
查看>>
[PHP内核探索]PHP中的哈希表
查看>>
Apache-drill Architechture
查看>>
WordPress 5.2 Beta 3 发布,要求 PHP 5.6.20 以上版本
查看>>
通通连起来——无处不在的流
查看>>
互联网+时代,看云计算如何改变传统行业
查看>>
ZFS ARC & L2ARC zfs-$ver/module/zfs/arc.c
查看>>
c++类默认拷贝构造函数---浅复制
查看>>
2019年最火热的Golang项目
查看>>
可实现RSSD云硬盘120万IOPS的SPDK IO路径优化实践
查看>>
Vue项目部署遇到的坑(你肯定会遇到!)
查看>>
资源分享计划第三期 0511
查看>>
awk 文本处理
查看>>
【JSConf EU 2018】主题总结 (部分主题已有中文文章)
查看>>