Mybatis Mapper.xml 配置文件中 resultMap 节点的源码解析

相关文章

Mybatis 解析配置文件的源码解析java

Mybatis 类型转换源码分析数据库

Mybatis 数据源和数据库链接池源码解析(DataSource)mybatis

Mybatis Mapper 接口源码解析(binding包)app

Mybatis 解析 SQL 源码分析一源码分析

前言

在上篇文章 Mybatis 解析 SQL 源码分析一 介绍了 Maper.xml 配置文件的解析,可是没有解析 resultMap 节点,由于该解析比较复杂,也比较难理解,全部单独拿出来进行解析。post

在使用 Mybatis 的时候,都会使用resultMap节点来绑定列与bean属性的对应关系,可是通常就只会使用其简单的属性,他还有一些比较复杂的属性能够实现一些高级的功能,在没查看源码以前,我也只会简单的使用,不少高级的用法都没有使用过,经过此次学习,但愿能在工做使用,可以写出简洁高效的SQL。学习

resultMap的定义

先来看看 resultMap 节点的官方定义:测试

简单的使用:fetch

<resultMap id="userResultMap" type="User">
  <id property="id" column="user_id" />
  <result property="username" column="user_name"/>
  <result property="password" column="hashed_password"/>
</resultMap>

会把列名和属性名进行绑定,该节点一共有 4 个属性:ui

1. id :表示该 resultMap,共其余的语句调用

2. type:表示其对于的pojo类型,可使用别名,也可使用全限定类名

3. autoMapping:若是设置这个属性,MyBatis将会为这个ResultMap开启或者关闭自动映射。这个属性会覆盖全局的属性 autoMappingBehavior。默认值为:unset。

4. extends:继承,一个 resultMap 能够继承另外一个 resultMap,这个属性是否是没有用过 ? ^^

接下来看下它能够有哪些子节点:

  • constructor - 用于注入结果到构造方法中
  • id – 标识ID列
  • result – 表示通常列
  • association – 关联查询
  • collection – 查询集合
  • discriminator - 鉴别器:mybatis可使用discriminator判断某列的值,而后根据某列的值改变封装行为

constructor 

在查询数据库获得数据后,会把对应列的值赋值给javabean对象对应的属性,默认状况下mybatis会调用实体类的无参构造方法建立一个实体类,而后再给各个属性赋值,若是没有构造方法的时候,可使用 constructor 节点进行绑定,如现有以下的构造方法:

public Person(int id, String name, String job, int age) {
        this.id = id;
        this.name = name;
        this.job = job;
        this.age = age;
    }

 则,可使用 constructor  节点进行绑定:

<resultMap id="queryPersonMap" type="mybatis.pojo.Person" >
        <constructor>
            <idArg column="id" javaType="int"/>
            <arg column="name" javaType="string" />
            <arg column="job" javaType="string" />
            <arg column="age" javaType="int" />
        </constructor>
    </resultMap>

association 

关联查询,在级联中有一对1、一对多、多对多等关系,association主要是用来解决一对一关系的,association 能够有多种使用方式:

好比如今有一个 Person 类,它有一个 Address 属性,关联 Address 对象:

public class Person implements Serializable {

    private int id;

    private String name;

    private String job;

    private int age;

    private Address address;
}

public class Address {
    private int id;

    private String name;

    private long number;

}

关联查询方式一:

<resultMap id="queryPersonMap" type="mybatis.pojo.Person" >
        <id column="id" property="id"/>
        <result column="name" property="name" />
        <result column="job" property="job" />
        <result column="age" property="age"/>
        <association property="address" column="address_id" javaType="mybatis.pojo.Address" select="queryAddress" />
    </resultMap>

    <select id="queryAddress" resultType="mybatis.pojo.Address">
        select * from address where id = #{id}
    </select>

关联查询方式二:

<resultMap id="queryPersonMap" type="mybatis.pojo.Person" >
        <id column="id" property="id"/>
        <result column="name" property="name" />
        <result column="job" property="job" />
        <result column="age" property="age"/>
        <association property="address" column="address_id" javaType="mybatis.pojo.Address" resultMap="addressMap"/>
    </resultMap>
    
    <resultMap id="addressMap" type="mybatis.pojo.Address">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="number" property="number"/>
    </resultMap>

关联查询方式三:

<resultMap id="queryPersonMap" type="mybatis.pojo.Person" >
        <id column="id" property="id"/>
        <result column="name" property="name" />
        <result column="job" property="job" />
        <result column="age" property="age"/>
        <association property="address" javaType="mybatis.pojo.Address">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="number" property="number"/>
        </association>
    </resultMap>

collection 

collection 集合,若是pojo对象有一个属性是集合类型的,可使用collection 来进行查询:

public class Person implements Serializable {

    private int id;

    private String name;

    private String job;

    private int age;

    private List<Address> addressList;
}
<resultMap id="queryPersonMap" type="mybatis.pojo.Person" >
        <id column="id" property="id"/>
        <result column="name" property="name" />
        <result column="job" property="job" />
        <result column="age" property="age"/>
        <collection property="addressList" javaType="ArrayList" ofType="mybatis.pojo.Address">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="number" property="number"/>
        </collection>
    </resultMap>

固然还有其余的方法,具体能够参考官网。

discriminator

鉴别器,mybatis可使用discriminator判断某列的值,而后根据某列的值改变封装行为,有点像 Java的 switch 语句,鉴别器指定了 column 和 javaType 属性。 列是 MyBatis 查找比较值的地方。 JavaType 是须要被用来保证等价测试的合适类型,

好比某列的值等于多少,则返回1,等于多少返回2等等。

<resultMap id="vehicleResult" type="Vehicle">
  <id property="id" column="id" />
  <result property="vin" column="vin"/>
  <result property="year" column="year"/>
  <result property="make" column="make"/>
  <result property="model" column="model"/>
  <result property="color" column="color"/>
  <discriminator javaType="int" column="vehicle_type">
    <case value="1" resultMap="carResult"/>
    <case value="2" resultMap="truckResult"/>
    <case value="3" resultMap="vanResult"/>
    <case value="4" resultMap="suvResult"/>
  </discriminator>
</resultMap>

以上就是 resultMap 节点的所有使用方法,下面是一个比较复杂的例子,源码解析会按照其来解析,例子来自于官方文档。

<resultMap id="detailedBlogResultMap" type="Blog">
  <constructor>
    <idArg column="blog_id" javaType="int"/>
    <arg column="name" javaType="string" />
  </constructor>

  <id column="id" property="id" />
  <result property="title" column="blog_title"/>

  <association property="author" javaType="Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
  </association>

  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
  </collection>
  
  <discriminator javaType="int" column="draft">
    <case value="1" resultType="DraftPost"/>
  </discriminator>
</resultMap>

resultMap 源码解析

首先须要说明的是,一个 resultMap 节点会解析成一个 ResultMap 对象,而每一个子节点(除了discriminator节点)会被解析成 ResultMapping 对象,即一个 ResultMap 包含的是 ResultMapping 对象的集合。

先来看看 ResultMapping 的一个声明:

public class ResultMapping {
  // configuration 对象
  private Configuration configuration;
  private String property;
  private String column;
  private Class<?> javaType;
  private JdbcType jdbcType;
  private TypeHandler<?> typeHandler;
  // 对应的是 resultMap 属性,经过id来引用其余的resultMap
  private String nestedResultMapId;
  // 对应的是 select 属性,经过id来引用其余的select节点的定义
  private String nestedQueryId;
  private Set<String> notNullColumns;
  private String columnPrefix;
  // 处理后的标志,标志有两个 id和constructor
  private List<ResultFlag> flags;
  // 对应节点的column属性拆分后生成的结果,composites.size()>0会使column为null
  private List<ResultMapping> composites;
  private String resultSet;
  private String foreignColumn;
  private boolean lazy;
|

ResultMap 的声明以下:

public class ResultMap {
  // ID,表示一个resultMap
  private String id;
  // 该resultMap对应的Javabean类型
  private Class<?> type;
  // 对应的是除了discriminator节点外的其余节点
  private List<ResultMapping> resultMappings;
  // id 节点的映射集合
  private List<ResultMapping> idResultMappings;
  // 构造节点的集合
  private List<ResultMapping> constructorResultMappings;
  // 记录了映射关系中 不带有contructot节点的的映射关系
  private List<ResultMapping> propertyResultMappings;
  // column集合
  private Set<String> mappedColumns;
  // discriminator 节点
  private Discriminator discriminator;
  private boolean hasNestedResultMaps;
  private boolean hasNestedQueries;
  private Boolean autoMapping;
}

解析:

resultMapElements(context.evalNodes("/mapper/resultMap"));

  private void resultMapElements(List<XNode> list) throws Exception {
    for (XNode resultMapNode : list) {
      try {
        // 解析每一个 resultMap 节点
        resultMapElement(resultMapNode);
      } catch (IncompleteElementException e) {
        // ignore, it will be retried
      }
    }
  }

  private ResultMap resultMapElement(XNode resultMapNode) throws Exception {
    // 注意这里传入的是一个空的集合
    return resultMapElement(resultMapNode, Collections.<ResultMapping> emptyList());
  }

主要的解析方法:

private ResultMap resultMapElement(XNode resultMapNode, List<ResultMapping> additionalResultMappings) throws Exception {
    ErrorContext.instance().activity("processing " + resultMapNode.getValueBasedIdentifier());
    // ID 属性
    String id = resultMapNode.getStringAttribute("id", resultMapNode.getValueBasedIdentifier());
    // type属性
    String type = resultMapNode.getStringAttribute("type",resultMapNode.getStringAttribute("ofType",        resultMapNode.getStringAttribute("resultType",resultMapNode.getStringAttribute("javaType"))));
    // extends 属性
    String extend = resultMapNode.getStringAttribute("extends");
    // autoMapping 属性
    Boolean autoMapping = resultMapNode.getBooleanAttribute("autoMapping");
    // 从注册的类型管理器里面查找对应的类型
    Class<?> typeClass = resolveClass(type);
    // discriminator 节点
    Discriminator discriminator = null;
    List<ResultMapping> resultMappings = new ArrayList<ResultMapping>();
    resultMappings.addAll(additionalResultMappings);
    // 处理子节点
    List<XNode> resultChildren = resultMapNode.getChildren();
    for (XNode resultChild : resultChildren) {
      if ("constructor".equals(resultChild.getName())) {
        // 处理 constructor 节点
        processConstructorElement(resultChild, typeClass, resultMappings);
      } else if ("discriminator".equals(resultChild.getName())) {
        // 处理discriminator节点
        discriminator = processDiscriminatorElement(resultChild, typeClass, resultMappings);
      } else {
        List<ResultFlag> flags = new ArrayList<ResultFlag>();
        if ("id".equals(resultChild.getName())) {
          flags.add(ResultFlag.ID);
        }
        // 处理其余节点,建立 resultMapping 对象并添加到集合中
        resultMappings.add(buildResultMappingFromContext(resultChild, typeClass, flags));
      }
    }
    ResultMapResolver resultMapResolver = new ResultMapResolver(builderAssistant, id, typeClass, extend, discriminator, resultMappings, autoMapping);
    try {
      // 建立表明该 resultMap 节点的 ResultMap 对象并添加到 ResultMap 集合中。
      return resultMapResolver.resolve();
    } catch (IncompleteElementException  e) {
      // 解析失败,添加到集合,从新解析
      configuration.addIncompleteResultMap(resultMapResolver);
      throw e;
    }
  }

处理 constructor 节点:

private void processConstructorElement(XNode resultChild, Class<?> resultType, List<ResultMapping> resultMappings) throws Exception {
    List<XNode> argChildren = resultChild.getChildren();
    for (XNode argChild : argChildren) {
      List<ResultFlag> flags = new ArrayList<ResultFlag>();
      // 向集合中添加 contrucator 标志
      flags.add(ResultFlag.CONSTRUCTOR);
      if ("idArg".equals(argChild.getName())) {
        // 添加id标志
        flags.add(ResultFlag.ID);
      }
      // 建立 ResultMapping 对象并添加到集合中
      resultMappings.add(buildResultMappingFromContext(argChild, resultType, flags));
    }
  }

建立 ResultMapping 对象:

private ResultMapping buildResultMappingFromContext(XNode context, Class<?> resultType, List<ResultFlag> flags) throws Exception {
    // 解析节点的属性
    String property = context.getStringAttribute("property");
    String column = context.getStringAttribute("column");
    String javaType = context.getStringAttribute("javaType");
    String jdbcType = context.getStringAttribute("jdbcType");
    String nestedSelect = context.getStringAttribute("select");
    String nestedResultMap = context.getStringAttribute("resultMap",
        processNestedResultMappings(context, Collections.<ResultMapping> emptyList()));
    String notNullColumn = context.getStringAttribute("notNullColumn");
    String columnPrefix = context.getStringAttribute("columnPrefix");
    String typeHandler = context.getStringAttribute("typeHandler");
    String resultSet = context.getStringAttribute("resultSet");
    String foreignColumn = context.getStringAttribute("foreignColumn");
    boolean lazy = "lazy".equals(context.getStringAttribute("fetchType", configuration.isLazyLoadingEnabled() ? "lazy" : "eager"));
    Class<?> javaTypeClass = resolveClass(javaType);
    @SuppressWarnings("unchecked")
    // 对应的 typeHandler 类型
    Class<? extends TypeHandler<?>> typeHandlerClass = (Class<? extends TypeHandler<?>>) resolveClass(typeHandler);
    JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
    // 建立 ResultMapping 对象
    return builderAssistant.buildResultMapping(resultType, property, column, javaTypeClass, jdbcTypeEnum, nestedSelect, nestedResultMap, notNullColumn, columnPrefix, typeHandlerClass, flags, resultSet, foreignColumn, lazy);
  }

以后是建立 ResultMapped 对象并添加到集合中:

ResultMapResolver resultMapResolver = new ResultMapResolver(builderAssistant, id, typeClass, extend, discriminator, resultMappings, autoMapping);
// 调用的使用 builderAssistant 的 addResultMap 方法
return resultMapResolver.resolve();
public ResultMap addResultMap(String id, Class<?> type, String extend, Discriminator discriminator, List<ResultMapping> resultMappings, Boolean autoMapping) {
    // 为 id 加上 namespace即 namespace.id
    id = applyCurrentNamespace(id, false);
    extend = applyCurrentNamespace(extend, true);

    if (extend != null) {
      if (!configuration.hasResultMap(extend)) {
        throw new IncompleteElementException("Could not find a parent resultmap with id '" + extend + "'");
      }
       // 获取父级的resultMap
      ResultMap resultMap = configuration.getResultMap(extend);
      List<ResultMapping> extendedResultMappings = new ArrayList<ResultMapping>(resultMap.getResultMappings());
      // 由于上面添加过一次,如今要删除重复的
      extendedResultMappings.removeAll(resultMappings);
      // Remove parent constructor if this resultMap declares a constructor.
      boolean declaresConstructor = false;
      for (ResultMapping resultMapping : resultMappings) {
        if (resultMapping.getFlags().contains(ResultFlag.CONSTRUCTOR)) {
          declaresConstructor = true;
          break;
        }
      }
      if (declaresConstructor) {
        Iterator<ResultMapping> extendedResultMappingsIter = extendedResultMappings.iterator();
        while (extendedResultMappingsIter.hasNext()) {
          if (extendedResultMappingsIter.next().getFlags().contains(ResultFlag.CONSTRUCTOR)) {
            extendedResultMappingsIter.remove();
          }
        }
      }
      resultMappings.addAll(extendedResultMappings);
    }
    // 建立 resultMap 
    ResultMap resultMap = new ResultMap.Builder(configuration, id, type, resultMappings, autoMapping)
        .discriminator(discriminator)
        .build();
    // 添加到集合
    configuration.addResultMap(resultMap);
    return resultMap;
  }

到这里,就把 resultMap 节点解析完毕了,以后在解析 Mapper.xml 文件的其余节点,参考 Mybatis 解析 SQL 源码分析一

相关文章
相关标签/搜索