Mybatis中mapper是怎么和XMl关联起来的

Mybatis中mapper是怎么和XMl关联起来的

从源码来分析,通过Mybatis的都知道,必须指定nameSpace为Mapper的全限定类名。这样就能关联起来。Mapper的实现肯定是动态dialing,在InvocationHandler中做增强。这里就来分析分析具体是怎么做的?
在下面分析的时候,源码看起来比较枯燥,并且涉及到的东西很多。分析的时候设计的东西多,容易走偏。我尽量回归主题。

1. XML文件解析

这里的xml解析比较繁琐,如果逐行来分析的话,很多很多,这里就挑主线来分析了。之后会分块来分话题来做分析。

解析总的配置文件

如果从经典的Mybatis创建SqlSessionFactory开始,那肯定能看到下面的代码

代码里面的有的注释,是我看源码的时候写的,有的写的比较离谱。有的记录我之前看的时候的困惑。之后看的时候又看懂了。所以就保留在这里了。

  private void parseConfiguration(XNode root) {
    try {
      // issue #117 read properties first
      propertiesElement(root.evalNode("properties")); //解析properties标签,并把他放在 parser和config的 Variables 里面
      Properties settings = settingsAsProperties(root.evalNode("settings"));//加载setting标签
      loadCustomVfs(settings); //lcnote 这里的vfs是啥?怎么用 我知道这个
      //我现在知道他的vfs是什么了,vfs(virtual file system)他抽象出了几个api,通过这些api就可以访问文件系统上的资源;比如在
      // 在解析   mapperElement(root.evalNode("mappers"));的时候,如果指定package,就可以通过VFS来获取包路径下面所有的class文件。
      // 并且会将他添加到mappe里面,和spring中的classPathSacnner一样差不多,可以指定过滤器。


      loadCustomLogImpl(settings);
      typeAliasesElement(root.evalNode("typeAliases"));

      //从这里开始,都是解析具体的标签,new出对象,将标签下面的属性设置进去,
      // 从解析的这里基本也能看出mybatis里面重要的几个点,首先是objectFactory,objectFactory。objectFactory,plugins
      pluginElement(root.evalNode("plugins"));
      objectFactoryElement(root.evalNode("objectFactory"));
      objectWrapperFactoryElement(root.evalNode("objectFactory"));
      reflectorFactoryElement(root.evalNode("objectFactory"));

      //这里就具体设置setting标签了
      settingsElement(settings);

      // read it after objectFactory and objectWrapperFactory issue #631
      environmentsElement(root.evalNode("environments"));
      databaseIdProviderElement(root.evalNode("databaseIdProvider"));
      typeHandlerElement(root.evalNode("typeHandlers"));

      //lcnote 这里是重点,解析mapper文件,
      mapperElement(root.evalNode("mappers"));
    } catch (Exception e) {
      throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
  }

直接看是怎么解析mapper文件的。

从这个代码里面可以看到,mappers标签下面是可以写两种标签。packagemapper标签。对于两种有不同的解析方法。

解析package标签

这里只是截取了部分的源码。还会将好几个源码都拼接在一块,便于看

 if ("package".equals(child.getName())) {
          String mapperPackage = child.getStringAttribute("name");
          configuration.addMappers(mapperPackage);
        }
//下面是 configuration.addMappers(mapperPackage)的方法
public void addMappers(String packageName) {
  //mapperRegistry是一个注册mapper的注册器,并且里面维护了很多的所有的mapper组成的对象。
    mapperRegistry.addMappers(packageName);
  }
//下面是mapperRegistry.addMappers(packageName);
  public void addMappers(String packageName) {
    addMappers(packageName, Object.class);
  }
//    addMappers(packageName, Object.class); 方法,

//packageName表示要扫描的包的路径
//superType表示要找的类是这个类的子类。

  public void addMappers(String packageName, Class<?> superType) 
  {
    //resolverUtil就是一个在指定包下,找指定的类的子类集合的一个工具类。
    ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
    resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
    
    //找到了合适的Class,将他添加到mapper里面。
    Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
    for (Class<?> mapperClass : mapperSet) {
      addMapper(mapperClass);
    }
  }
//resolverUtil.find(new ResolverUtil.IsA(superType), packageName);

//扫描给定包下(包括子路径下面的所有的类。调用Test方法来匹配,匹配到的class调用getClasses就可以获取的到。)
  public ResolverUtil<T> find(Test test, String packageName) {
    String path = getPackagePath(packageName);

    try {
      List<String> children = VFS.getInstance().list(path);
      for (String child : children) {
        if (child.endsWith(".class")) {
          //加载class对象,调用ResolverUtil里面的静态内部类IsA(实现了Test接口)做匹配。
          addIfMatching(test, child);
        }
      }
    } catch (IOException ioe) {
      log.error("Could not read package: " + packageName, ioe);
    }

    return this;
  }
//addIfMatching(test, child);

  protected void addIfMatching(Test test, String fqn) {
    try {
      String externalName = fqn.substring(0, fqn.indexOf('.')).replace('/', '.');
      ClassLoader loader = getClassLoader();
      if (log.isDebugEnabled()) {
        log.debug("Checking to see if class " + externalName + " matches criteria [" + test + "]");
      }
      
      Class<?> type = loader.loadClass(externalName);
      if (test.matches(type)) {
        matches.add((Class<T>) type);
      }
    } catch (Throwable t) {
      log.warn("Could not examine class '" + fqn + "'" + " due to a "
          + t.getClass().getName() + " with message: " + t.getMessage());
    }
  }


//****************************重点***********************************
//   public <T> void addMapper(Class<T> type) 方法,将上面找的,合适的class实例化之后要加载到mapperRegistry里面去。
// 并且这个方法是mapperRegistry里面的。
  public <T> void addMapper(Class<T> type) {
    if (type.isInterface()) {
      if (hasMapper(type)) {//mapper只能注册一次
        throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
      }
      boolean loadCompleted = false;
      try {
        
        // 用mapper new出MapperProxyFactory,放在knownMappers里面
        knownMappers.put(type, new MapperProxyFactory<>(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.
        // 这个是很重要的,在解析之前的添加类型,因此,他会自动尝试绑定解析mapper。如果类型知道,没啥事,
        // 这里我觉得是解析mapper里面的注解。
        MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
        parser.parse();
        loadCompleted = true;
      } finally {
        if (!loadCompleted) {
          knownMappers.remove(type);
        }
      }
    }
  }

这里面出现的几个重要的类

  1. MapperRegistry: mapper的注册器,保存所有的mapper。
  2. Configuration:Configuration对象报错了Mybatis运行期间能用到的所有的数据。
  3. MapperProxyFactory :代理mapper的创建工厂,这里面没有啥特殊的,就是调用创建代理对象的方式来创建对象。
  4. MapperAnnotationBuilder: 解析mapper里面的注解。这些注解和XMl的功能是一样的,但是不推荐使用
解析mapper标签

看源码的时候有这种感觉,哇哦,这居然可以这样用,这个框架居然还有这种功能。

{		

          String resource = child.getStringAttribute("resource");
          String url = child.getStringAttribute("url");
          String mapperClass = child.getStringAttribute("class");
          if (resource != null && url == null && mapperClass == null) {
            ErrorContext.instance().resource(resource);
          
            
            //resource和url的加载操作是一直的,就是resource的来源不一样。
            
            
            // 这里就会加载resource,解析mapper文件,构建mapperStatement对象,
            try(InputStream inputStream = Resources.getResourceAsStream(resource)) {
              XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
              mapperParser.parse();//lcnote 这里的解析操作和配置文件解析操作是一样的。都是构建XMLMapperBuilder,然后调用parse方法
            }
          } else if (resource == null && url != null && mapperClass == null) {
            ErrorContext.instance().resource(url);
            try(InputStream inputStream = Resources.getUrlAsStream(url)){
              XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
              mapperParser.parse();
            }
          } else if (resource == null && url == null && mapperClass != null) {
            
            //这里没有什么特殊,就是什么解析package标签,得到mapper之后加载的过程,
            Class<?> mapperInterface = Resources.classForName(mapperClass);
            configuration.addMapper(mapperInterface);
          } else {
            throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
          }
        }

从这里可以看到,支持三种属性,resource,url和class,并且加载的顺序也是resource优先,url和class,并且三个不能同时指定。

从上面可以看出,resource和url的加载操作是一致的,就是resource的来源不一样。class的加载和解析package标签,得到mapper之后加载的过程,是一致的,这里就直接看XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());开始了

构建XMLMapperBuilder

这里要说说BaseBuilder,这个类在Mybatis中是很基础的类。好多解析都是继承与他,才开始做解析的。

在这里插入图片描述

还有一点点的说明,MapperBuilderAssistant确实是一个工具类,先看看他的构造

public class MapperBuilderAssistant extends BaseBuilder {
  private String currentNamespace; // 当前解析的nameSpace
  private final String resource;  // 当前nameSpace对应的resource文件
  private Cache currentCache;     // 当前的缓存,对应的mapper标签里面的cache标签。
  private boolean unresolvedCacheRef; // issue #676
}

这个类对应的就是一个mapper文件解析时候产生的所有的东西。比如resultMap,sql,select,update,等等。这些相关的东西。都会通过这个对象添加到BaseBuilder里面去。

XMLMapperBuilder继承与BaseBuilder,XMLMapperBuilder主要是用来解析配置文件中的mappers中的mapper标签。

// 看看构造类
  public XMLMapperBuilder(InputStream inputStream, Configuration configuration, String resource, Map<String, XNode> sqlFragments) {
    this(new XPathParser(inputStream, true, configuration.getVariables(), new XMLMapperEntityResolver()),
        configuration, resource, sqlFragments);
  }

  private XMLMapperBuilder(XPathParser parser, Configuration configuration, String resource, Map<String, XNode> sqlFragments) {
    super(configuration);
    this.builderAssistant = new MapperBuilderAssistant(configuration, resource);
    this.parser = parser;
    this.sqlFragments = sqlFragments;
    this.resource = resource;
  }

//super的构造方法
 public BaseBuilder(Configuration configuration) {
    this.configuration = configuration;
    this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();//从配置文件中获取 typeAliases标签相关内容
    this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();//从配置文件中获取typeHandlers
  }

构造函数没有什么可说的,重点是下面的Parse方法

上面出现的几个重要的类

  1. XMLMapperEntityResolver :xmlMapper的实体解析器。继承与EntityResolver。它是org.xml.sax包中的对象。
  2. BaseBuilder:所有xml解析的基础类。
调用XMLMapperBuilder的parse方法
  public void parse() {
     //configuration里面保存了加载过的resource集合,这里先判断一下
    if (!configuration.isResourceLoaded(resource)) { //会去configuration里面的一个set里面去查找
      
      // 这里是重点,重点就是解析mapper标签
      configurationElement(parser.evalNode("/mapper"));//解析mapper标签
      configuration.addLoadedResource(resource);//添加到已经加载过的集合中
      bindMapperForNamespace(); //尝试通过nameSpace来加载配置文件。
      //注意,这里说的是尝试,nameSpace并不必须和Mapper接口保持一致。
    }
    
    //下面的操作也很有意思。
    //解析xml的时候,如果报错(IncompleteElementException)不会立即抛出,而是会将这些报错的缓存起来,在上面的都解析完成之后,在尝试一下。
    // 
    parsePendingResultMaps(); 
    parsePendingCacheRefs(); 
    parsePendingStatements(); 
  }

这里主要就是解析mapper标签,并且尝试通过nameSpace来加载对应的mapper。如果加载到了,就会调用上面的MapperRegistry将mapper注册到里面。

这里的解析操作和之前解析configuration标签的操作很类似,先解析父标签在解析子标签。下面看看具体是怎么解析的

private void configurationElement(XNode context) {
    try {
      // 解析namespace
      String namespace = context.getStringAttribute("namespace");
      if (namespace == null || namespace.isEmpty()) {
        throw new BuilderException("Mapper's namespace cannot be empty");
      }
      builderAssistant.setCurrentNamespace(namespace);
      cacheRefElement(context.evalNode("cache-ref"));//解析各个标签元素
      // 解析cache标签
      cacheElement(context.evalNode("cache"));
      parameterMapElement(context.evalNodes("/mapper/parameterMap"));
      
      resultMapElements(context.evalNodes("/mapper/resultMap"));
      //解析sql
      sqlElement(context.evalNodes("/mapper/sql"));
      
      //waring 这里很重要,真正的开始解析select|insert|update|delete标签
      buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
    } catch (Exception e) {
      throw new BuilderException("Error parsing Mapper XML. The XML location is '" + resource + "'. Cause: " + e, e);
    }
  }

说明

  1. 解析parameterMap,在经过上面的解析之后,构建 ParameterMapping,添加到builderAssistant里面。在builderAssistant里面会转换成 ParameterMap,最后添加到 Configuration对象parameterMaps属性里面。这个Configuration就是全局通用。并且一个mapper里面能有多个parameterMap标签。

  2. 解析cache,得到对应的属性元素的值,构建Cache对象,添加到configuration里面,将builderAssistant中的currentCache赋值为当前的cache对象。并且一个Mapper只能有一个cache标签。

  3. 解析resultMap,这里的解析相比前面两个就比较复杂了,resultMap下面有很多标签。

     for (XNode resultChild : resultChildren) {
          if ("constructor".equals(resultChild.getName())) {
            //这个很简单了,通过构造方法来设置参数
            processConstructorElement(resultChild, typeClass, resultMappings);
          } else if ("discriminator".equals(resultChild.getName())) {
            // lcnote 对应的Discriminator对象,现实中Discriminator标签没有用过,之后看看,看起来这个标签能实现swtich case的功能,而且还可以搭配resultMap
            // 来做一些有趣的事情。之前这个确实么有用过
            discriminator = processDiscriminatorElement(resultChild, typeClass, resultMappings);
          } else {
            List<ResultFlag> flags = new ArrayList<>();
            if ("id".equals(resultChild.getName())) {
              flags.add(ResultFlag.ID);
            }
            resultMappings.add(buildResultMappingFromContext(resultChild, typeClass, flags));
          }
        }
    
    • 对于constructor标签,构建ResultMapping对象添加到ResultMapping集合中。
    • 对于discriminator标签 我没用过,之后在写写吧。构建Discriminator对象。
    • 对于别的标签,构建ResultMapping对象添加到ResultMapping集合中。

    这都是一个resultMap标签下面的东西,在解析完一个resultMap之后,会将上面相关的对象组装成ResultMapResolver,调用resultMapResolver.resolve();方法,构建成ResultMap对象,放在Configuration中。所以ResultMap就是resultmap标签对应的实体类

    1. 解析sql标签。将xnode和id(sql标签指定的id)放在XMLMapperBuilder对象的sqlFragments中sqlFragments是一个StrictMap继承与HashMap,重写了里面的put,和get方法,主要是在put和get的时候增加了判断。sqlFragments存放的是sql片段,注意,解析这里的时候并没有处理sql里面的动态标签的部分。要知道动态标签是随着参数来确定的。这里只是一个简单的把他存起来了。并且sql标签是多个。

    2. 解析select|insert|update|delete标签。这是重点。为了清楚,还是对着源码来看吧,select|insert|update|delete标签是多个。所以这里是循环解析,下面的代码只是循环体里面的解析操作。

        public void parseStatementNode() {
          String id = context.getStringAttribute("id");
          String databaseId = context.getStringAttribute("databaseId");
          //知道的,在mybatis里面是可以指定dataBase的,并且也可以在标签里面指定要应用的databaseid。
          // 这里就是一个判断,如果不是当前要应用的,就不会解析。。
          if (!databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) {
            return;
          }
         
          String nodeName = context.getNode().getNodeName();
          // 通过标签的名字来判断sql的类型。
          SqlCommandType sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase(Locale.ENGLISH));
          boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
          
          //如果没有指定flushCache,并且是select类型,默认是false。
          boolean flushCache = context.getBooleanAttribute("flushCache", !isSelect);
          //如果没有指定 useCache ,并且是select类型,默认是true。
          boolean useCache = context.getBooleanAttribute("useCache", isSelect);
          
          // 这个标签是啥意思,我没用过。这种还是建议看看mybatis的官方文档。
          boolean resultOrdered = context.getBooleanAttribute("resultOrdered", false);
      
          // Include Fragments before parsing
          // 在解析sql之前,想将includ标签解析。看这个名字也能看得出来。这就是用来处理<include>标签的。
          // 这也就解释了,之前在解析sql标签的时候为啥这么简单了。sql标签最终是要用在 Statement里面的。
          // 在Statement里面也是要写动态sql的,所以,在真正开始解析标签之前,就先把他包含进来。一块放在
          // 后面的解析操作里面。一块解析
          XMLIncludeTransformer includeParser = new XMLIncludeTransformer(configuration, builderAssistant);
          includeParser.applyIncludes(context.getNode());
         
          String parameterType = context.getStringAttribute("parameterType");
          Class<?> parameterTypeClass = resolveClass(parameterType);
          
          String lang = context.getStringAttribute("lang");
          LanguageDriver langDriver = getLanguageDriver(lang);
      
          // Parse selectKey after includes and remove them.
          // 解析selectkey
          processSelectKeyNodes(id, parameterTypeClass, langDriver);
          //lcnote 解析sql selectKey在解析之前已经remove掉了
          // Parse the SQL (pre: <selectKey> and <include> were parsed and removed)
          
          
          
          //这里判断是否需要使用useGeneratedKeys,这里还维护了一个缓存。可以看看,id就是selcet标签的id
          KeyGenerator keyGenerator;
          String keyStatementId = id + SelectKeyGenerator.SELECT_KEY_SUFFIX;
          keyStatementId = builderAssistant.applyCurrentNamespace(keyStatementId, true); // eg:org.apache.ibatis.domain.mybatis.mapper.StudentMapper.listAllStudent!selectKey
          if (configuration.hasKeyGenerator(keyStatementId)) {
            keyGenerator = configuration.getKeyGenerator(keyStatementId);
          } else {
            keyGenerator = context.getBooleanAttribute("useGeneratedKeys",
                configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType))
                ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
          }
         //  说实话,Mybatis的langDriver我还真不知道是什么,之后在分析分析
          SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass);
          StatementType statementType = StatementType.valueOf(context.getStringAttribute("statementType", StatementType.PREPARED.toString()));
          Integer fetchSize = context.getIntAttribute("fetchSize");
          Integer timeout = context.getIntAttribute("timeout");
          String parameterMap = context.getStringAttribute("parameterMap");
          String resultType = context.getStringAttribute("resultType");
          Class<?> resultTypeClass = resolveClass(resultType);
          String resultMap = context.getStringAttribute("resultMap");
          String resultSetType = context.getStringAttribute("resultSetType");
          ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType);
          if (resultSetTypeEnum == null) {
            resultSetTypeEnum = configuration.getDefaultResultSetType();
          }
          String keyProperty = context.getStringAttribute("keyProperty");
          String keyColumn = context.getStringAttribute("keyColumn");
          String resultSets = context.getStringAttribute("resultSets");
       
          
          //看到这里就知道,肯定是通过builderAssistant,将组装好的MappedStatement添加到
          // configuration里面维护了statement的map,key就是namespace+mapper的id、  
          builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType,
              fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass,
              resultSetTypeEnum, flushCache, useCache, resultOrdered,
              keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets);
        }
      

调用XMLMapperBuilder的parse方法,会解析Mapper.xml文件

  1. 首先要知道,每一个xml元素组成的数据范围,在Mybatis中肯定是要有一个对应的对象的。
  2. 在处理xml的时候,肯定要用到别名和类型处理器。这俩是通用的。之后还会见到。
  3. 将解析好的对象都要添加到configuration里面。
  4. 在解析mapper文件的时候还是比较有意思的。比如,在sql标签里面可以用${}来引用环境变量。还支持嵌套引用。
  5. 确实,在看源码的时候发现居然有这种用法,有的东西没有用过。之后在详细的看看吧
  6. 在解析的时候如果报错了,不会立即抛出。而是把他放在一个集合里面,等所有的xml文件解析完成了,在尝试解析一下。

尝试通过nameSpace绑定mapper

  private void bindMapperForNamespace() {
     // 前面说过,builderAssistant对应的是一个mapper解析期间的工具类。拿到namespace
    String namespace = builderAssistant.getCurrentNamespace();
    if (namespace != null) {
      Class<?> boundType = null;
      try {
        //尝试通过全限定类名加载
        boundType = Resources.classForName(namespace);
      } catch (ClassNotFoundException e) {
        // ignore, bound type is not required
      }
      if (boundType != null && !configuration.hasMapper(boundType)) {
        // Spring may not know the real resource name so we set a flag
        // to prevent loading again this resource from the mapper interface
        // look at MapperAnnotationBuilder#loadXmlResource
        configuration.addLoadedResource("namespace:" + namespace);
        configuration.addMapper(boundType);
      }
    }
  }

到这里就很明确了,在解析xml文件的时候会生成对应的标签,然后将它们添加到configuration里面,然后通过nameSpace加载class类,如果nameSpace要和Mapper对应起来,还是必须要一样的,如果不需要对应的话,那没事了, 随便写。

将加载到的class添加到configuration里面。configuration里面维护着一个map,key是class,value是MapperProxyFactory。 要注意 configuration.addMapper(boundType);方法。下面我们会看看这个方法。

通过mapper标签里面的nameSpace做缓存。并且生成代理对象创建工厂。

这个方法是MapperRegistry里面的。


  public <T> void addMapper(Class<T> type) {
    if (type.isInterface()) {
      if (hasMapper(type)) {//mapper只能注册一次
        throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
      }
      boolean loadCompleted = false;
      try {
        knownMappers.put(type, new MapperProxyFactory<>(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.
        // 这个是很重要的,在解析之前的添加类型,因此,他会自动尝试绑定解析mapper。如果类型知道,没啥事,
        // 解析mapper里面的注解。
        MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
        parser.parse();
        loadCompleted = true;
      } finally {
        if (!loadCompleted) {
          knownMappers.remove(type);
        }
      }
    }
  }

重点是MapperProxyFactory


/**
 * @author Lasse Voss
 * lcnote mapper代理对象的创建工厂
 */
public class MapperProxyFactory<T> {
  // 需要代理的接口,也就是mapper
  private final Class<T> mapperInterface;
  
  //保存的缓存,避免new处重复对象。
  private final Map<Method, MapperMethodInvoker> methodCache = new ConcurrentHashMap<>();

  public MapperProxyFactory(Class<T> mapperInterface) {
    this.mapperInterface = mapperInterface;
  }

  public Class<T> getMapperInterface() {
    return mapperInterface;
  }

  public Map<Method, MapperMethodInvoker> getMethodCache() {
    return methodCache;
  }
   // 这个new操作,没有啥特殊的,就简单的调用创建代理对象的方法来创建。
  @SuppressWarnings("unchecked")
  protected T newInstance(MapperProxy<T> mapperProxy) {
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }

  public T newInstance(SqlSession sqlSession) {
    final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }
}

2. Mapper动态代理的创建

添加的 InvocationHandler长什么样子?

下面就是重点中的重点 MapperProxy,这里的太长了,我就挑重要的讲了,MapperProxy实现了InvocationHandler,那肯定是动态代理。mapper肯定是利用反射来和xml文件关联的。下面的这个方法不是在new MapperProxyFactory时候调用的,在调用的时候会通过MapperProxyFactory创建出来,这里就顺着上面的看下来了。

在方法调用的时候,对于default的方法,将方法封装成MapperMethod,然后再用 PlainMethodInvoker包装调用。

/** waring, 这也是比较重要的,在mapper调用的时候实现的InvocationHandler
 * @author Clinton Begin
 * @author Eduardo Macarron
 */
public class MapperProxy<T> implements InvocationHandler, Serializable {

  private static final long serialVersionUID = -4724728412955527868L;
  private static final int ALLOWED_MODES = MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED
      | MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PUBLIC;
  private static final Constructor<Lookup> lookupConstructor;
  private final SqlSession sqlSession;
  private final Class<T> mapperInterface;
  private final Map<Method, MapperMethodInvoker> methodCache;

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      if (Object.class.equals(method.getDeclaringClass())) {//如果是object类里面的方法,直接调用就好了
        return method.invoke(this, args);
      } else {
        return cachedInvoker(method).invoke(proxy, method, args, sqlSession);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
  }

  private MapperMethodInvoker cachedInvoker(Method method) throws Throwable {
    try {
      return MapUtil.computeIfAbsent(methodCache, method, m -> {
        if (m.isDefault()) {//如果接口里面的方法是default的
          try {
            if (privateLookupInMethod == null) {
              return new DefaultMethodInvoker(getMethodHandleJava8(method));
            } else {
              return new DefaultMethodInvoker(getMethodHandleJava9(method));
            }
          } catch (IllegalAccessException | InstantiationException | InvocationTargetException
              | NoSuchMethodException e) {
            throw new RuntimeException(e);
          }
        } else {
          //lcnote  MapperMethod代表一个mapper方法。里面包括方法对应的dataId,还有对应的sql类型,还有方法的具体的签名信息,包括方法返回值,param参数。mapkey注解
          return new PlainMethodInvoker(new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));
        }
      });
    } catch (RuntimeException re) {
      Throwable cause = re.getCause();
      throw cause == null ? re : cause;
    }
  }


  // mapper方法调用接口,
  interface MapperMethodInvoker {
    Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable;
  }

  private static class PlainMethodInvoker implements MapperMethodInvoker {
    private final MapperMethod mapperMethod;
   // 这只是实现mapper调用的工具类而已.
    public PlainMethodInvoker(MapperMethod mapperMethod) {
      super();
      this.mapperMethod = mapperMethod;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args, SqlSession sqlSession) throws Throwable {
      return mapperMethod.execute(sqlSession, args);
    }
  }
}
什么时候创建对象?

从SqlSession的getMapper方法开始。从这里就开始创建代理对象了。

  public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
    if (mapperProxyFactory == null) {
      throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
    }
    try {
      // 调用MapperProxyFactory来创建mapper实例。
      return mapperProxyFactory.newInstance(sqlSession);
    } catch (Exception e) {
      throw new BindingException("Error getting mapper instance. Cause: " + e, e);
    }
  }

newInstance就会创建出MapperProxy对象。MapperProxy对象在上面介绍了。

可见,这里是利用动态代理来创建了一个MapperProxy对象。

MapperProxy里面有什么?
SqlCommand

表示关联的mapper和这方法关联的sql的类型

会通过Mapper的全限定类名从Configuration里面找出MapperStatement。赋值id。

判断此方法是那种sql类型。

MethodSignature

表示一个方法的主要信息。

    private final boolean returnsMany; 
    private final boolean returnsMap; //返回值是不是一个map,只要mapKey不为null,这就是true
    private final boolean returnsVoid; //标志位,是否没有返回值
    private final boolean returnsCursor; //是否返回了一个Cursor
    private final boolean returnsOptional;
    private final Class<?> returnType; //这个方法真正返回的类型,比如List<Student>真实返回的类型就是List
    private final String mapKey; //mapkey就是MapKey注解里面的value,关于这个MapKey的作用,之后可以写一篇文章来分析分析
    private final Integer resultHandlerIndex;
    private final Integer rowBoundsIndex;
    private final ParamNameResolver paramNameResolver;
MapperMethod

这对象里面包含了上面两个对象,在真正执行的时候,会调用execute方法。

Mapper和XML就关联起来了,在往下面就要执行sql了,下面的步骤肯定有通过全限定类名找到MapperStatement,处理入参,处理动态sql。这里会调用OGNL来解析。然后执行并且处理结果。后面的流程就之后在说。

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值