2.1MyBatis——ORM对象关系映射

2.1MyBatis——ORM对象关系映射

  • 1. 验证映射配置
  • 2.ResultType和ResultMap
    • 2.1ResultMap是最终的ORM依据
    • 2.2ResultType和ResultMap的使用区别
  • 3.具体的转换逻辑
    • 3.1 TypeHandle类型转换
  • 5.总结

合集总览:Mybatis框架梳理   


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

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

1. 验证映射配置


<select id="queryWithoutType">
	select * from user where id = #{id}
select>

<select id="queryWithReturnType" resultType="org.wyy.dto.User">
	select * from user where id = #{id}
select>

<resultMap id="customMap" type="org.wyy.dto.User">resultMap>

<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.1MyBatis——ORM对象关系映射_第1张图片

2.1ResultMap是最终的ORM依据

从上图可以看出,当ResultMap为空但ResultType不为空时(即标签声明的映射类型(这里是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内置了许多类型转换器,使得大部分场景下不需要显式配置。

    你可能感兴趣的:(Java开源框架学习,mybatis,java)