2.1MyBatis——ORM对象关系映射

概括的说,MyBatis中,对于映射关系的声明是由开发者在xml文件手动完成的。比如对查询方法而言,你需要显式声明ResultType或ResultMap,这里其实就是在定义数据库字段和Java属性之间的映射关系。

下面我们以简单的查询方法为例,探索MyBatis如何将数据库字段转换为具体对象的字段属性,即ORM的具体过程。
首先我们先验证ORM这一过程确实存在。

1. 验证映射配置

<!--方法1-->
<select id="queryWithoutType">
	select * from user where id = #{id}
</select>
<!--方法2-->
<select id="queryWithReturnType" resultType="org.wyy.dto.User">
	select * from user where id = #{id}
</select>

<resultMap id="customMap" type="org.wyy.dto.User"></resultMap>
<!--方法3-->
<select id="queryWithReturnMap" resultMap="customMap">
	select * from user where id = #{id}
</select>

这里我们定义三个相同逻辑的方法,方法1不声明映射关系。分别运行三个方法,方法2和方法3正常执行,方法1抛出异常如下:

org.mybatis.spring.MyBatisSystemException: 
nested exception is org.apache.ibatis.executor.ExecutorException: 
A query was run and no Result Maps were found for the Mapped Statement 
'org.wyy.mapper.UserMapper.queryWithoutType'.  
It's likely that neither a Result Type nor a Result Map was specified.

说明Mybatis中,ORM的映射转换需要在xml的每个方法中手动配置,否则无法进行查询。

2.ResultType和ResultMap

对查询方法未做任何配置时,异常提示It's likely that neither a Result Type nor a Result Map was specified.,即至少需要ResultTypeResultMap其中一个,那么它们有什么关系呢?看一下源码:

在这里插入图片描述

2.1ResultMap是最终的ORM依据

从上图可以看出,当ResultMap为空但ResultType不为空时(即<select>标签配置了resultType属性),XxMapper.xml加载后会Mybatis默认创建一个后缀为-InlineResultMap对象,所以MyBatis中映射转换最终参照的都是ResultMap对象

2.2ResultType和ResultMap的使用区别

既然最终都是使用ResultMap对象进行关系映射,为什么还要设计ResultType呢?它们在使用时的区别又是什么呢?
先看下ResultMap对象的结构:

private Configuration configuration;
private String id;
private Class<?> type;
private List<ResultMapping> resultMappings;
private List<ResultMapping> idResultMappings;
private List<ResultMapping> constructorResultMappings;
private List<ResultMapping> propertyResultMappings;
private Set<String> mappedColumns;
private Set<String> mappedProperties;
private Discriminator discriminator;
private boolean hasNestedResultMaps;
private boolean hasNestedQueries;
private Boolean autoMapping;

加载:前面的查询我们提到,在MyBatis加载Mapper.xml文件时,如果某个查询方法定义了resultType属性,那么会为其自动生成ResultMap对象,这个ResultMap其实相当简单,只有id 和 type属性。
执行:当查询方法被调用时,在ResultSetHandler中会进行如下判断:

public void handleRowValues(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
    if (resultMap.hasNestedResultMaps()) {
      ensureNoRowBounds();
      checkResultHandler();
      // 嵌套类型
      handleRowValuesForNestedResultMap(rsw, resultMap, resultHandler, rowBounds, parentMapping);
    } else {
    // 简单类型
      handleRowValuesForSimpleResultMap(rsw, resultMap, resultHandler, rowBounds, parentMapping);
    }
  }

结合ResultMap对象的属性ResultType自动生成的简单ResultMap对象,以及这里处理结果集的判断,可以看出,MyBatis中将结果集映射进行了分类:简单和复杂嵌套。

  • 对于简单的ResultMap,直接使用<select>标签声明的映射类型(这里是org.wyy.dto.User)创建映射器Reflector,并使用MyBatis内置的类型处理器TypeHandler(如StringTypeHandler、LongTypeHandler等)进行处理
  • 对于存在嵌套的类型,则需要通过自定义的ResultMap进行列名和字段的映射绑定,以及指定所需的类型转换器

区别和使用:

  • 对单表查询而言,如果数据库字段和对象的属性名一致,没必编写映射关系,直接使用ResultType即可。这也是为什么即使ResultType最终会生成ResultMap,却依然保留ResultType的原因:使用ResultType可以简化配置。
  • 对于查询结果集不能与对象属性一一匹配的情况,则必须通过显示定义ResultMap来声明映射关系,特殊类型可能还需要定义类型转换器
<resultMap id="customMap" type="org.wyy.dto.User">
	<result column="" jdbcType="VARCHAR" 
			property="" javaType="" 
			typeHandler="org.apache.ibatis.type.StringTypeHandler" />
</resultMap>

3.具体的转换逻辑

有了前面的描述,我们知道,数据库列名和对象属性之间的映射是通过ResultMap维护的。有了这层映射关系,如果是我们,该如何将数据从结果集装填到对象中呢?

  1. 获取当前查询方法的ResultMap,里面包含列和属性的对应关系
  2. 使用列名获取结果集中对应的数据
  3. 根据映射关系,将获取到的列数据赋值给对象的相应属性


    其实MyBatis中也是这样做的:
private boolean applyAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix) throws SQLException {
	// 映射关系的获取
    List<UnMappedColumnAutoMapping> autoMapping = createAutomaticMappings(rsw, resultMap, metaObject, columnPrefix);
    boolean foundValues = false;
    if (!autoMapping.isEmpty()) {
      // 每一列和属性的映射关系,分别遍历
      for (UnMappedColumnAutoMapping mapping : autoMapping) {
      	// 通过类型处理器typeHandler,从结果集中获取列的值
        final Object value = mapping.typeHandler.getResult(rsw.getResultSet(), mapping.column);
        if (value != null) {
          foundValues = true;
        }
        if (value != null || (configuration.isCallSettersOnNulls() && !mapping.primitive)) {
          // 将取出的列的value设置给对应的对象属性,完成columen --> property的赋值
          metaObject.setValue(mapping.property, value);
        }
      }
    }
    return foundValues;
  }

3.1 TypeHandle类型转换

上面代码可以看到,从结果集中获取值并转化是由类型处理器完成的。MyBatis中内置了常用的类型处理器,用来完成java type 和jdbc type之间的转换

// java type + jdbc type + handler
  public <T> void register(Class<T> type, JdbcType jdbcType, TypeHandler<? extends T> handler) {
    register((Type) type, jdbcType, handler);
  }

// 内部注册
public TypeHandlerRegistry() {
    register(Boolean.class, new BooleanTypeHandler());
    register(boolean.class, new BooleanTypeHandler());
    register(JdbcType.BOOLEAN, new BooleanTypeHandler());
    register(JdbcType.BIT, new BooleanTypeHandler());
    ...
}

对于无法转换的类型,则可以通过自定义类型转换器进行扩展:

// column varchar  --> java property LocalDate
<resultMap id="customMap" type="org.wyy.dto.User">
	<result column="update_time" property="updateTime" typeHandler="org.wyy.typeHandler.CustomTypeHandler"/>
</resultMap>

@MappedTypes(LocalDate.class)
public class CustomTypeHandler extends BaseTypeHandler<LocalDate> {}

5.总结

  1. . Mybatis中ORM是我们通过ResultType或ResultMap手动完成关系映射的,所以ResultType和ResultMap至少需要一个,否则报错;
  2. ResultType属性简化了ORM配置,但复杂对象或对象嵌套,则必须使用ResultMap;
  3. 以查询为例,转换的流程是先查询映射关系,再查询具体列的值,然后通过类型转换器转换为Java属性,最后赋值;
  4. MyBatis内置了许多类型转换器,使得大部分场景下不需要显式配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值