交流
商城
MCN
登入
注册
首页
提问
分享
讨论
建议
公告
动态
发表新帖
发表新帖
mybatis第4 章:MapperFactoryBean
分享
未结
0
910
李延
LV6
2021-06-06
悬赏:20积分
# 作用 我们使用的mapper的bean工厂。 我们在解析Mapper扫描的时候发现,最后创建的就是MapperFactoryBean对象。而它是FactoryBean的子类。所以我们最终使用的是对象会通过getObject获取。 # 继承关系  ## afterPropertiesSet 这个类继承InitializingBean。我们看一下它的初始化方法 ```java @Override public final void afterPropertiesSet() throws IllegalArgumentException, BeanInitializationException { // Let abstract subclasses check their configuration. checkDaoConfig(); // Let concrete implementations initialize themselves. try { initDao(); } catch (Exception ex) { throw new BeanInitializationException("Initialization of DAO failed", ex); } } ``` 我们具体看一下checkDaoConfig 和initDao ```java @Override protected void checkDaoConfig() { super.checkDaoConfig(); notNull(this.mapperInterface, "Property 'mapperInterface' is required"); Configuration configuration = getSqlSession().getConfiguration(); if (this.addToConfig && !configuration.hasMapper(this.mapperInterface)) { try { configuration.addMapper(this.mapperInterface); } catch (Exception e) { logger.error("Error while adding the mapper '" + this.mapperInterface + "' to configuration.", e); throw new IllegalArgumentException(e); } finally { ErrorContext.instance().reset(); } } } ``` 这里我们看到当没有xml文件被加载时,以注解的方法加载 ## getObject ```java @Override public T getObject() throws Exception { return getSqlSession().getMapper(this.mapperInterface); } ``` 这里我们看到是通过getSqlSession去获取对象的,而这里使用的是SqlSessionTemplate,我们看一下它的方法 ```java @Override public <T> T getMapper(Class<T> type) { return getConfiguration().getMapper(type, this); } ``` 我们继续看Configuration对象 ```java public <T> T getMapper(Class<T> type, SqlSession sqlSession) { return mapperRegistry.getMapper(type, sqlSession); } ``` 这里我们看到又是通过mapperRegistry来获取的数据。 # 总结 在getObject 方法中。我们最后追踪到是通过mapperRegistry对象获取的。 而mapperRegistry我们已经在前面见到过,其实我们一开始的mapping配置也是保存在了这里。 关于mapperRegistry我们单独说明
回帖
消灭零回复
提交回复
热议榜
java 相关知识分享
8
好的程序员与不好的程序员
6
写给工程师的十条精进原则
5
spring boot以jar包运行配置的logback日志文件没生成
5
一步一步分析SpringBoot启动源码(一)
5
MockMvc测试
5
【吐槽向】是不是有个吐槽的板块比较好玩
4
logstash jdbc同步mysql多表数据到elasticsearch
3
IntelliJ IDEA 优质License Server
3
.gitignore忽略规则
3
SpringBoot启动源码分析
3
一步一步分析SpringBoot启动源码(三)
3
2
一步一步分析SpringBoot启动源码(二)
2
积分不够将无法发表新帖
2
官方产品
Meta-Boot - 基于MCN
MCN - 快速构建SpringBoot应用
微信扫码关注公众号