mybatis源码分析-Mapper接口绑定过程分析

mybatis源码分析-Mapper接口绑定过程分析

Mapper接口绑定过程分析,之前的文章我们已经讲了,mapper节点的解析,生成MappedStatement,然后放入到了configuration的集合里面,我们下面看bindMapperForNamespace,绑定的过程

bindMapperForNamespace()解析

//Configuration里面 MapperRegistry mapperRegistry = new MapperRegistry(this);
//mapperRegistry里面有Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>, MapperProxyFactory<?>>(); key是类名,value是个反射的代理工厂
private void bindMapperForNamespace() {
  //当前的namespace
  String namespace = builderAssistant.getCurrentNamespace();
  if (namespace != null) {
    Class<?> boundType = null;
    try {
      //反射获得里面的mapper的类
      boundType = Resources.classForName(namespace);//反射获得class 类 class com,wjk,
    } catch (ClassNotFoundException e) {
      //ignore, bound type is not required
    }
    if (boundType != null) {
      //判断是否已经注入过了,如果没有注入 mapperRegistry是否有boundType
      if (!configuration.hasMapper(boundType)) {
        configuration.addLoadedResource("namespace:" + namespace);
        configuration.addMapper(boundType);
      }
    }
  }
}

configuration.addMapper(boundType)

public <T> void addMapper(Class<T> type) {
  if (type.isInterface()) {
    //是否已经绑定过Mapper
    if (hasMapper(type)) {
      throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
    }
    boolean loadCompleted = false;
    try {
			//MapperProxyFactory给我们的mapper生成代理类使用,type就是我们的mapper
      knownMappers.put(type, new MapperProxyFactory<T>(type));
      // It's important that the type is added before the parser is run
      // otherwise the binding may automatically be attempted by the
      // mapper parser. If the type is already known, it won't try.
      //下面是注解的类型,我们这边先不用
      MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
      parser.parse();
      loadCompleted = true;
    } finally {
      if (!loadCompleted) {
        knownMappers.remove(type);
      }
    }
  }
}

//判断是否已经新增过了
//Map<Class<?>, MapperProxyFactory<?>> knownMappers
public <T> boolean hasMapper(Class<T> type) {
  return knownMappers.containsKey(type);
}

总结:

我们已经把MappedStatement放入configuration里面了,然后再映射当前mapper的class,放入到configuration里面的MapperRegistry里面,具体是放到mapperRegistry的knownMappers里面(Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>, MapperProxyFactory<?>>()) MapperProxyFactory到时候给mapper类生成代理类使用
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值