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 ...
随机推荐
- UI进阶 科大讯飞(2) 语音合成(文字转换成语音)
科大讯飞开放平台.SDK下载.添加静态库.初始化见UI进阶 科大讯飞(1) 语音听写(语音转换成文字) 实现语音合成 功能实现步骤: 导入头文件 创建文字识别对象 指定文字识别后的回调代理对象 开启文 ...
- sql with(lock) 与事务
sql select查询语句 表后面携带 with(nolock) 会获取到 在事务中已经执行 但还未完成提交的 记录 即使表被锁住也能查询到 当事务最终执行失败时 查询到的记录可能没有啦 不 ...
- Routed Events【pluralsight】
Routing Strategies: Direct Bubbling Tunneling WHy use them? Any UIElement can be a listener Common h ...
- js打开新页面 关闭当前页 关闭父页面
js打开新页面.关闭当前页.关闭父页面 2010-04-29 14:04:13| 分类: 页面与JavaScript | 标签: |字号大中小 订阅 //关闭当前页面,并且打开新页面,(不 ...
- 栈的应用-四则表达式(C#代码实现)
->概念 中缀表达式 9+(3-1)*3+10/2 转换步骤 9 + 9 + ( 9 3 + ( - 9 3 1 + ( - ) 9 3 1 - + 9 3 1 - + * 9 3 1 - 3 ...
- bootstrap适配移动端
上次在pythonanywhere上挂上去的页面,是这个样子的 而在手机上看是这个样子的 总之简直不能看= = 看了一下学校几个微信公众号的页面.都是用的bootstrap,好吧我也去试试看好了. 在 ...
- NAT后面的FTP SERVER终极篇
原文引用:http://blog.chinaunix.net/uid-20592805-id-1918661.html 如果对于被动模式还有不同的意见,我们可以再看下这篇文章: http://ww ...
- 【M10】在构造方法内阻止资源泄漏
1.类中没有指针,如果对象构造过程中出现异常,C++保证已经构造好的那一部分自动销毁.注意:这里不是调用析构方法,而是编译器在你的构造方法中插入了一些代码,保证对已经构造好的对象析构. 2.类中有指针 ...
- CSS的魔法和魅力
其实我最开始学会的语言是HTML,我记得那还是大一的事情.当时我对床的兄弟DR放了一本HTML的书在床上,我因为没事就拿来看看.那本书大概只有50页左右,可是可以说如果没有这本书,今天Maybe我不会 ...
- BZOJ 2751: [HAOI2012]容易题(easy) 数学
2751: [HAOI2012]容易题(easy) 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2751 Description 为了使 ...