一般我们这样配置

<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. SQL Server sql 操作

    1.重命名表将表OLD重命名为NEW: EXEC sp_rename 'OLD','NEW' 2.重命名列将表table1中的列old重命名为new: EXEC sp_rename 'table1.o ...

  2. C++STL学习笔记_(3)stack

    10.2.4stack容器 Stack简介 ²  stack是堆栈容器,是一种"先进后出"的容器. ²  stack是简单地装饰deque容器而成为另外的一种容器. ²  #inc ...

  3. Head First设计模式-单例模式

    一.整体代码 Singleton.java public class Singleton { private static Singleton uniqueInstance; // other use ...

  4. Spring优势

    *  使用spring有什么好处? ◆Spring能有效地组织你的中间层对象,无论你是否选择使用了EJB.如果你仅仅使用了Struts或其他的包含了J2EE特有APIs的framework,你会发现S ...

  5. Spring MVC体系结构

    [Spring MVC类图]<Spring实战>中:<Spring3.0就这么简单>中:[http://blog.csdn.net/gstormspire/article/de ...

  6. git免登录-ssh-key

    1.生成ssh key公钥与私钥 ssh-keygen -t rsa -C "youname@example.com" 需输入三个内容:第一个,生成公私钥的路径及名称:后两个输入回 ...

  7. ISA中的WEB链

    在ISA Server 2004中提供了Web链功能,它就相当于将ISA Server配置为二级代理,可以将你的请求转发到上游的代理服务器或其他站点.使用Web链,你就可以实现条件路由,对不同的目的地 ...

  8. 如何自学Java 经典

    JAVA自学之路 JAVA自学之路 一:学会选择 为了就业,不少同学参加各种各样的培训. 决心做软件的,大多数人选的是java,或是.net,也有一些选择了手机.嵌入式.游戏.3G.测试等. 那么究竟 ...

  9. nginx-1.4.4 + tcp_proxy_module手动编译安装

    Nginx开源软件默认没有提供TCP协议的负载均衡,下面记录一下我的安装过程: 1. 下载nginx最新稳定版的源码 mkdir /software cd /software yum install ...

  10. BZOJ 1061: [Noi2008]志愿者招募 费用流

    1061: [Noi2008]志愿者招募 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1061 Description 申奥成功后,布布 ...