Mybatis初始化流程(二)

1. Properties variables的作用

通常,我们会单独配置jdbc.properties文件,保存于variables变量中,而Xml文件内可以使用${driver}占位符,读取时可动态替换占位符的值

2. 扫描package

<typeAliases>
	<typeAlias alias="Student" type="com.mybatis3.domain.Student" />
	<typeAlias alias="Teacher" type="com.mybatis3.domain.Teacher" />
	<package name="com.mybatis3.domain" />
</typeAliases>

前两个typeAlias,很容易理解,那么元素如何处理呢?

public void registerAliases(String packageName, Class<?> superType){
    ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>();
    resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
    Set<Class<? extends Class<?>>> typeSet = resolverUtil.getClasses();
    for(Class<?> type : typeSet){
      // 排除内部类、接口
      if (!type.isAnonymousClass() && !type.isInterface() && !type.isMemberClass()) {
        registerAlias(type);
      }
    }
  }

3. namespace如何映射Mapper接口

org.apache.ibatis.builder.xml.XMLMapperBuilder.bindMapperForNamespace()。

直接使用Class.forName(),成功找到就注册,找不到就什么也不做。

Mybatis中的namespace有两个功能。

  1. 和其名字含义一样,作为名称空间使用。namespace + id,就能找到对应的Sql。

  2. 作为Mapper接口的全限名使用,通过namespace,就能找到对应的Mapper接口(也有称Dao接口的)。Mybatis推荐的最佳实践,但并不强制使用。

Mapper接口注册至Configuration的MapperRegistry mapperRegistry内。

private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<Class<?>, MapperProxyFactory<?>>()

Mapper接口将通过MapperProxyFactory创建动态代理对象。

4. 一个MappedStatement被缓存了两个引用的原理及原因

configuration.addMappedStatement(statement);

调用上面一句话,往Map里放置一个MappedStatement对象,结果Map中变成两个元素。

com.mybatis3.mappers.StudentMapper.findAllStudents=org.apache.ibatis.mapping.MappedStatement@add0edd
findAllStudents=org.apache.ibatis.mapping.MappedStatement@add0edd

我们的问题是,为什么会变成两个元素?同一个对象,为什么要存有两个键的引用?

其实,在Mybatis中,这些Map,都是StrictMap类型,Mybatis在StrictMap内做了手脚。

protected static class StrictMap<V> extends HashMap<String, V> {

    public V put(String key, V value) {
      if (containsKey(key)) {
        throw new IllegalArgumentException(name + " already contains value for " + key);
      }
      if (key.contains(".")) {
        final String shortKey = getShortName(key);
        // 不存在shortKey键值,放进去
        if (super.get(shortKey) == null) {
          super.put(shortKey, value);
        } else {
        // 存在shortKey键值,填充占位对象Ambiguity
          super.put(shortKey, (V) new Ambiguity(shortKey));
        }
      }
      return super.put(key, value);
    }
}

Mybatis重写了put方法,将id和namespace+id的键,都put了进去,指向同一个MappedStatement对象。如果shortKey键值存在,就填充为占位符对象Ambiguity,属于覆盖操作

这样做的好处是,方便我们编程

Student std  = sqlSession.selectOne("findStudentById", 1);
Student std  = sqlSession.selectOne("com.mybatis3.mappers.StudentMapper.findStudentById", 1);

上面两句代码,是等价的,Mybatis不强制我们一定要加namespace名称空间,所以,这是存放两个键的良苦用心。

问题:不同namespace空间下的id,能否相同呢?(网上的说法是,不同名称空间下的id可以相同)

明白上述put原理后,就不难得出结论,namespace名称空间不同,而id相同时,使用namespace+id获取Sql,完全可以正确执行。如果只用id获取,那么,将导致错误。

org.apache.ibatis.session.Configuration.StrictMap.get()方法源码。

public V get(Object key) {
      V value = super.get(key);
      if (value == null) {
        throw new IllegalArgumentException(name + " does not contain value for " + key);
      }
      if (value instanceof Ambiguity) {
        throw new IllegalArgumentException(((Ambiguity) value).getSubject() + " is ambiguous in " + name
            + " (try using the full name including the namespace, or rename one of the entries)");
      }
      return value;
    }

get时,如果得到的是一个占位对象Ambiguity,就抛出异常,要求使用full name进行调用。full name就是namespace+id。Ambiguity意为模糊不清。

解决办法:

  1. 保证shortKey(即id)不重复。(好像有点难度,不推荐)

  2. 使用绑定Mapper接口调用方法,因为它总是转换为full name调用。(Mybatis最佳实践,推荐)

  3. 直接使用字符串full name调用。(退而求其次的方式,不推荐)

5. 初始化过程中的mapped和incomplete对象

翻译为搞定的和还没搞定的。这恐怕是Mybatis框架中比较奇葩的设计了,给人很多迷惑,我们来看看它具体是什么意思。

<resultMap type="Student" id="StudentResult" extends="Parent">
	<id property="studId" column="stud_id" />
	<result property="name" column="name" />
	<result property="email" column="email" />
	<result property="dob" column="dob" />
</resultMap>
	
<resultMap type="Student" id="Parent">
	<result property="phone" column="phone" />
</resultMap>

Mapper.xml中的很多元素,是可以指定父元素的,像上面extends="Parent"。然而,Mybatis解析元素时,是按顺序解析的,于是先解析的id="StudentResult"的元素,然而该元素继承自id="Parent"的元素,但是,Parent被配置在下面了,还没有解析到,内存中尚不存在,怎么办呢?Mybatis就把id="StudentResult"的元素标记为incomplete的,然后继续解析后续元素。等程序把id="Parent"的元素也解析完后,再回过头来解析id="StudentResult"的元素,就可以正确继承父元素的内容。

先标记 后解析能解析到的 回头再解析一边之前标记过的 这个顺序

简言之就是,你的父元素可以配置在你的后边,不限制非得配置在前面。无论你配置在哪儿,Mybatis都能“智能”的获取到,并正确继承。

这便是在Configuration对象内,有的叫mapped,有的叫incomplete的原因。

protected final Collection<XMLStatementBuilder> incompleteStatements = new LinkedList<XMLStatementBuilder>();
protected final Collection<CacheRefResolver> incompleteCacheRefs = new LinkedList<CacheRefResolver>();
protected final Collection<ResultMapResolver> incompleteResultMaps = new LinkedList<ResultMapResolver>();
protected final Collection<MethodResolver> incompleteMethods = new LinkedList<MethodResolver>();

org.apache.ibatis.builder.xml.XMLMapperBuilder.parse()方法内,触发了incomplete的再度解析。

public void parse() {
    if (!configuration.isResourceLoaded(resource)) {
      configurationElement(parser.evalNode("/mapper"));
      configuration.addLoadedResource(resource);
      bindMapperForNamespace();
    }
    // 执行incomplete的地方
    parsePendingResultMaps();
    parsePendingChacheRefs();
    parsePendingStatements();
  }

Pending含义为待定的,悬而未决的意思。

总结:有了这些细节储备,阅读源码就变得更加得心应手了。

posted @ 2021-08-11 18:19  hochan_100  阅读(58)  评论(0编辑  收藏  举报