一般我们这样配置

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
lazy-init="false">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:sqlmapper/*Mapper.xml"/>
<property name="plugins">
<list>
<bean class="***">
<property name="dialect">
<bean class="***"/>
</property>
</bean>
</list>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="*.*.*" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

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

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

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

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

public <T> T getMapper(Class<T> 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 详细过程见 mybatis缓存创建过程 ,parse中xml的namespace相应的class注册给configuration

public <T> void addMapper(Class<T> 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);

进一步的调用过程见 mybatis缓存

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

mybatis--MapperScannerConfigurer的更多相关文章

  1. Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring

    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring 非原创[只为记录],原博文地址:https://www.cnblogs.com/ ...

  2. (转)Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring

    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring Mybatis在与Spring集成的时候可以配置MapperFactoryBea ...

  3. Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 - 推酷 - 360安全浏览器 7.1

    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 时间 2014-02-11 21:08:00  博客园-所有随笔区 ...

  4. Mybatis下面的MapperScannerConfigurer 扫描器

    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring Mybatis在与Spring集成的时候可以配置              Ma ...

  5. MapperScannerConfigurer(转)

    转:http://blog.csdn.net/ranmudaofa/article/details/8508028 原文:http://www.cnblogs.com/daxin/p/3545040. ...

  6. 【MyBatis学习笔记】

    [MyBatis学习笔记]系列之预备篇一:ant的下载与安装 [MyBatis学习笔记]系列之预备篇二:ant入门示例 [MyBatis学习笔记]系列之一:MyBatis入门示例 [MyBatis学习 ...

  7. Mybatis中映射器实现方式总结

    一种是通过XML文件方式(由一个java接口和一个XML文件构成) RoleMapper rm = session.getMapper(RoleMapper.class); List<Role& ...

  8. 【mybatis源码学习】mybatis和spring框架整合,我们依赖的mapper的接口真相

    转载至:https://www.cnblogs.com/jpfss/p/7799806.html Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注 ...

  9. springboot2 + mybatis 多种方式实现多数据配置

    业务系统复杂程度增加,为了解决数据库I/O瓶颈,很自然会进行拆库拆表分服务来应对.这就会出现一个系统中可能会访问多处数据库,需要配置多个数据源. 第一种场景:项目服务从其它多处数据库取基础数据进行业务 ...

  10. HiKariCP的数据源配置

    <!-- Hikari Datasource --> <bean id="dataSourceHikari" class="com.zaxxer.hik ...

随机推荐

  1. 1005. 继续(3n+1)猜想 (25)

    卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证的时候, ...

  2. 【转】Android TouchEvent事件传递机制

    Android TouchEvent事件传递机制   事件机制参考地址: http://www.cnblogs.com/sunzn/archive/2013/05/10/3064129.html ht ...

  3. android 动画属性(一)之Animation

    Animation 在android 程序当中很多时候要用到动画效果,而动画效果主要是Animation来实现的,API给出的解释: 其中包含4种动画效果 AlphaAnimation 渐变透明度 R ...

  4. 如果浏览器自动调用quirks模式打开的话

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-03-21) 则肯定你的html的声明,没有写好. 今天遇到几个,前面莫名其妙的多了个空格(在网页上看源码是多空格,复制到n ...

  5. 全代码实现ios-3

    决定做ios开发的时候,看了很多本关于ios开发的书籍,其中有国内的人写的,也有根据外国的书翻译过来的. 很可惜,这些书里的例子没办法照搬过来,因为Xcode更新换代太快了,而这些书本的内容更新的速度 ...

  6. 【转】android出现注: 某些输入文件使用或覆盖了已过时的 API。 注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。 注: 某些输入文件使用了未经检查或不安全的操作。 注

    使用Android studio打包应用程序出现如下错误: 注: 某些输入文件使用或覆盖了已过时的 API. 注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译. 注: 某些 ...

  7. hotfix分析

    使用System Update Readiness Tool for Windows Server 2008 R2 x64 可以分析hotfix是否有安装失败的情况 示例:http://blogs.t ...

  8. Python在Windows上的安装

    1:下载 http://dlsw.baidu.com/sw-search-sp/soft/6e/17016/python-3.3.5.1395976247.msi 2:直接next安装 3:验证安装是 ...

  9. 使用GLSL实现的海洋效果 【转】

    http://bbs.osgchina.org/viewthread.php?tid=342&extra=page%3D3 虽说自己原创的部分并不算多,不过总算是调试通过了,中间有多次严重的死 ...

  10. 【android开发】Android防止内存溢出浅析

    近期项目做得差点儿相同了,測试出现了一些问题,当中一个就是内存溢出问题,在三星手机上測试最easy出现内存溢出,在其它手机上,比方华为就没有发生,也是比較郁闷.这个问题在之前的公司,做项目时也遇到过, ...