mybatis--MapperScannerConfigurer
一般我们这样配置
<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的更多相关文章
- Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring 非原创[只为记录],原博文地址:https://www.cnblogs.com/ ...
- (转)Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring Mybatis在与Spring集成的时候可以配置MapperFactoryBea ...
- Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 - 推酷 - 360安全浏览器 7.1
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring - 大新博客 时间 2014-02-11 21:08:00 博客园-所有随笔区 ...
- Mybatis下面的MapperScannerConfigurer 扫描器
Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring Mybatis在与Spring集成的时候可以配置 Ma ...
- MapperScannerConfigurer(转)
转:http://blog.csdn.net/ranmudaofa/article/details/8508028 原文:http://www.cnblogs.com/daxin/p/3545040. ...
- 【MyBatis学习笔记】
[MyBatis学习笔记]系列之预备篇一:ant的下载与安装 [MyBatis学习笔记]系列之预备篇二:ant入门示例 [MyBatis学习笔记]系列之一:MyBatis入门示例 [MyBatis学习 ...
- Mybatis中映射器实现方式总结
一种是通过XML文件方式(由一个java接口和一个XML文件构成) RoleMapper rm = session.getMapper(RoleMapper.class); List<Role& ...
- 【mybatis源码学习】mybatis和spring框架整合,我们依赖的mapper的接口真相
转载至:https://www.cnblogs.com/jpfss/p/7799806.html Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注 ...
- springboot2 + mybatis 多种方式实现多数据配置
业务系统复杂程度增加,为了解决数据库I/O瓶颈,很自然会进行拆库拆表分服务来应对.这就会出现一个系统中可能会访问多处数据库,需要配置多个数据源. 第一种场景:项目服务从其它多处数据库取基础数据进行业务 ...
- HiKariCP的数据源配置
<!-- Hikari Datasource --> <bean id="dataSourceHikari" class="com.zaxxer.hik ...
随机推荐
- Spring JTA应用JOTM & Atomikos III Atomikos
前面简单介绍了JOTM如何在Spring中配置,并如何使用它的JTA事务,本节将介绍Atomikos如何与Spring集成,并使用它的JTA事务. Atomikos,是一个基于Java的开源事务管理器 ...
- Odoo的Domain (一)
Odoo 的Domain:多个条件的列表. 条件:(字段名,操作符,值)三元式(列表或者元组) 字段名:当前模型的字段或者是通过点操作符访问的Many2one/Many2Many对象,当是Many2M ...
- Oracle:递归查询(树形结构数据)
今天要做一个查询功能:查询某用户所属部门,且包含该部门的所有上级部门信息.偶然找到了一个方法,特意来做个笔记.分享给和我一样的菜鸟,哈哈 查询子节点 1 select * 2 from d_arc_d ...
- VC++ 6.0中实现三叉切分窗口与多视图 [转]
一.引用 当用户需要同时对文当的不同部分进行编辑时,常常会用到切分窗口;这些窗口可以都是相同的视,或者一个窗口为列表视,而另一个为树型视图.应用程序框架有多种方式来表示多视图,切分窗口是其中的方式之一 ...
- 一条结合where、group、orderby的linq语法
DataTable dt = (from x in dsResult.Tables[0].AsEnumerable() where DataTrans.CBoolean(x["IsCheck ...
- 分享一个导出Excel时页面不跳转的小技巧
今天在点击客户档案导出的时候,发现先是打开了一个新标签,然后新标签自动关掉,弹出一个文件下载确认的窗口,点击确认后开始下载导出的Excel文件.这样的过程感觉窗口闪来闪去,而且可能会给用户带来困惑,是 ...
- Android ndk下用AssetManager读取assets的资源
转自:http://www.cppblog.com/johndragon/archive/2012/12/28/196754.html 在使用 cocos2dx 在 Android 上进行游戏开发时, ...
- 深入理解Oracle索引(25):一招鲜、吃遍天之单字段索引创建思路
本文较短.不过实用性很好.还是记录之. ㈠ 先别看SQL语句.看执行计划.挑出走全表扫的表 ㈡ 回头看SQL语句.分析上述表的约束字段有哪些.检查各个约束字段的索引是否存在 ㈢ 选择 ...
- php计算脚本执行时间
利用PHP的microtime实现 function getCurrentTime () { list ($msec, $sec) = explode(" ", microtime ...
- cdoj 15 Kastenlauf dfs
Kastenlauf Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/3 De ...