• java
  • go
  • 数据库
  • linux
  • 中间件
  • 书
  • 源码
  • 夕拾

  • java
  • go
  • 数据库
  • linux
  • 中间件
  • 书
  • 源码
  • 夕拾

spring-mapper代理对象的创建

  • 引言
  • mapper代理对象创建的全流程
  • 重要的类
    • mapper工厂bean
      • mapperScannerConfigure
      • mapperFactoryBean

引言

使用spring全家桶一段时间了,ioc的前后接口也稍微有些了解,比较好奇mybatis中如何将mapper代理
此文基于 mybatis-spring 源码做分析

mapper代理对象创建的全流程

重要的类

mapper工厂bean

MapperFactoryBean继承了SqlSessionDaoSupport 实现了 FactoryBean,其继承关系如下:

1
2
3
4
5
6
7
8
9
10
/**
* BeanFactory that enables injection of MyBatis mapper interfaces.
* It can be set up with a SqlSessionFactory or a
* pre-configured SqlSessionTemplate.
*/
public class MapperFactoryBean<T> extends SqlSessionDaoSupport implements FactoryBean<T>

public abstract class SqlSessionDaoSupport extends DaoSupport

public abstract class DaoSupport implements InitializingBean

mapperScannerConfigure

1
2
3
4
public class MapperScannerConfigurer
implements BeanDefinitionRegistryPostProcessor, InitializingBean, ApplicationContextAware, BeanNameAware{

}

mapperFactoryBean

checkDaoConfig方法将mapperInterface写入到sqlSession

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private Class<T> mapperInterface;


/**
* 其在DaoSupport中 afterPropertiesSet中被调用
*/
@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();
}
}
}
spring-ioc 部分interface接口
go基础知识
  1. 1. 引言
  2. 2. mapper代理对象创建的全流程
  3. 3. 重要的类
    1. 3.1. mapper工厂bean
      1. 3.1.1. mapperScannerConfigure
      2. 3.1.2. mapperFactoryBean
© 2023 haoxp
Hexo theme