@

ResultMapping 对象是 mybatis 的 <resultMap> 节点在 ResultMap 对象中基础组成部分.

ResultMapping 对象记录了结果集中一列与队友JavaBean中一个属性的对应关系。

1 成员变量

// Configuration 对象, 看过前面源码的应该知道这个对象的含义
private Configuration configuration;
// 对应相应 JavaBean 中的成员变量
private String property;
// 对应节点的 column 属性, 对应检索出来的列名(别名)
private String column;
// 对应节点的 javaType 属性
private Class<?> javaType;
// 对应节点的 jdbcType 属性, 表示映射列的JDBC属性
private JdbcType jdbcType;
// 类型处理器
private TypeHandler<?> typeHandler;
// 对应另一个 resultMap 的 id, 负责将结果集中的一部分映射成其他对象。
private String nestedResultMapId;
//
private String nestedQueryId;
// 对应节点的 notNullColumns 属性拆分后的结果
private Set<String> notNullColumns;
// 对应节点的 columnPrefix 属性
private String columnPrefix;
// 处理后的标记, 有两种:id和constructor
private List<ResultFlag> flags;
//
private List<ResultMapping> composites;
// 对应节点的 resultSet 属性
private String resultSet;
// 对应节点的 foreignColumn 属性
private String foreignColumn;
// 是否延迟加载, 对应节点的 fetchType 属性
private boolean lazy;

2 构造函数

  ResultMapping() {
}

就是一个空的构造函数

3 其他函数

3.1 setter 和 getter 函数

ResultMapping 对象创建使用的是建造者模式, 因此,只有部分成员变量含有 setter 函数。而除了 Configuration 对象, 其他都含有 getter 函数。

3.2 equals 和 hashCode 函数

该函数重写了 equals 方法。

  @Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
} ResultMapping that = (ResultMapping) o; if (property == null || !property.equals(that.property)) {
return false;
} return true;
}

而 equals 重写, 则基本上 hashCode 也要重写。 以 property 或 column 的 hashCode 作为其 hashCode

  @Override
public int hashCode() {
if (property != null) {
return property.hashCode();
} else if (column != null) {
return column.hashCode();
} else {
return 0;
}
}

3.3 toString 函数

  @Override
public String toString() {
final StringBuilder sb = new StringBuilder("ResultMapping{");
//sb.append("configuration=").append(configuration); // configuration doesn't have a useful .toString()
sb.append("property='").append(property).append('\'');
sb.append(", column='").append(column).append('\'');
sb.append(", javaType=").append(javaType);
sb.append(", jdbcType=").append(jdbcType);
//sb.append(", typeHandler=").append(typeHandler); // typeHandler also doesn't have a useful .toString()
sb.append(", nestedResultMapId='").append(nestedResultMapId).append('\'');
sb.append(", nestedQueryId='").append(nestedQueryId).append('\'');
sb.append(", notNullColumns=").append(notNullColumns);
sb.append(", columnPrefix='").append(columnPrefix).append('\'');
sb.append(", flags=").append(flags);
sb.append(", composites=").append(composites);
sb.append(", resultSet='").append(resultSet).append('\'');
sb.append(", foreignColumn='").append(foreignColumn).append('\'');
sb.append(", lazy=").append(lazy);
sb.append('}');
return sb.toString();
}

恨我们平常写的基本差不多。

4 内部类 Builder

ResultMapping 是使用建造者模式来进行创建的。

4.1 成员变量

private ResultMapping resultMapping = new ResultMapping();

4.2 构造函数

就是给部分属性赋值。

 public Builder(Configuration configuration, String property, String column, TypeHandler<?> typeHandler) {
this(configuration, property);
resultMapping.column = column;
resultMapping.typeHandler = typeHandler;
} public Builder(Configuration configuration, String property, String column, Class<?> javaType) {
this(configuration, property);
resultMapping.column = column;
resultMapping.javaType = javaType;
} public Builder(Configuration configuration, String property) {
resultMapping.configuration = configuration;
resultMapping.property = property;
resultMapping.flags = new ArrayList<ResultFlag>();
resultMapping.composites = new ArrayList<ResultMapping>();
resultMapping.lazy = configuration.isLazyLoadingEnabled();
}

4.3 建造者模式相关函数

除了 configuration, property, column, 其他成员变量都有类似如下的函数:

赋值后返回 Builder 对象本身。

public Builder javaType(Class<?> javaType) {
resultMapping.javaType = javaType;
return this;
}

建造对象的函数

public ResultMapping build() {
// 返回不可更改的 List
resultMapping.flags = Collections.unmodifiableList(resultMapping.flags);
// 返回不可更改的 List
resultMapping.composites = Collections.unmodifiableList(resultMapping.composites);
resolveTypeHandler();
// 校验
validate();
return resultMapping;
}

校验

// 对我们写的配置进行校验
private void validate() {
// 不可同时定义 nestedQueryId 和 nestedResultMapId
if (resultMapping.nestedQueryId != null && resultMapping.nestedResultMapId != null) {
throw new IllegalStateException("Cannot define both nestedQueryId and nestedResultMapId in property " + resultMapping.property);
}
// nestedQueryId 、 nestedResultMapId 和 typeHandler 不能同时为 null
if (resultMapping.nestedQueryId == null && resultMapping.nestedResultMapId == null && resultMapping.typeHandler == null) {
throw new IllegalStateException("No typehandler found for property " + resultMapping.property);
}
// Issue #4 and GH #39: column is optional only in nested resultmaps but not in the rest
if (resultMapping.nestedResultMapId == null && resultMapping.column == null && resultMapping.composites.isEmpty()) {
throw new IllegalStateException("Mapping is missing column attribute for property " + resultMapping.property);
}
if (resultMapping.getResultSet() != null) {
int numColumns = 0;
if (resultMapping.column != null) {
numColumns = resultMapping.column.split(",").length;
}
int numForeignColumns = 0;
if (resultMapping.foreignColumn != null) {
numForeignColumns = resultMapping.foreignColumn.split(",").length;
}
if (numColumns != numForeignColumns) {
throw new IllegalStateException("There should be the same number of columns and foreignColumns in property " + resultMapping.property);
}
}
}

一起学 mybatis

你想不想来学习 mybatis? 学习其使用和源码呢?那么, 在博客园关注我吧!!

我自己打算把这个源码系列更新完毕, 同时会更新相应的注释。快去 star 吧!!

mybatis最新源码和注释

mybatis百科-列映射类ResultMapping的更多相关文章

  1. mybatis百科-结果集映射类ResultMap

    目录 1 成员变量 2 构造函数 3 其他函数 3.1 setter 和 getter 函数 4 静态内部类 4.1 成员变量 4.2 构造函数 4.3 建造者相关的函数 4.4 获取配置的构造方法参 ...

  2. Mybatis自动生成实体类和实体映射工具

    Mybatis Mysql生成实体类 用到的Lib包: mybatis-generator-core-1.3.2.jarmysql-connector-java-5.1.30.jar 1. 创建一个文 ...

  3. Mybatis 高级结果映射 ResultMap Association Collection

    在阅读本文章时,先说几个mybatis中容易混淆的地方: 1. mybatis中的列不是数据库里的列而是查询里的列,可以是别名(如 select user_name as userName,这时col ...

  4. 5.Mybatis的输出映射(就是对查询的结果集的映射)

    Mybatis的输出映射,也就是对查询结果集的一个映射,主要有两种: 1.resultType(不需要配置,可以直接用) 一般是实体类 基本类型也可以 2.resultMap(需要配置resultMa ...

  5. Mybatis学习记录(六)----Mybatis的高级映射

    1.一对多查询 1.1 需求 查询订单及订单明细的信息. 1.2 sql语句 确定主查询表:订单表 确定关联查询表:订单明细表 在一对一查询基础上添加订单明细表关联即可. SELECT orders. ...

  6. MyBatis 的 XML 映射文件使用说明

    简介 文档参考地址:http://www.mybatis.org/mybatis-3/zh/index.html MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器 ...

  7. Mybatis学习—XML映射文件

    总结自 Mybatis官方中文文档 Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同 ...

  8. MyBatis多表映射demo

    三个实体类,作者.文章和评论. public class Author { private int id; private String username; private String nickna ...

  9. 转:mybatis 高级结果映射(http://blog.csdn.net/ilovejava_2010/article/details/8180521)

    高级结果映射 MyBatis的创建基于这样一个思想:数据库并不是您想怎样就怎样的.虽然我们希望所有的数据库遵守第三范式或BCNF(修正的第三范式),但它们不是.如果有一个数据库能够完美映射到所有应用程 ...

随机推荐

  1. 使用 float 存储小数?

    很多程序员就会使用 float 类型来存储小数.sql 的 float 类型和其他大多数编程语言的 float 类型一样, 根据IEEE 754 标准使用二进制格式编码实数数据. 但是很多程序员并不清 ...

  2. 【软件需求工程与建模 - 小组项目】第6周 - 成果展示2 - 软件需求规格说明书V4.3

    成果展示2 - 软件需求规格说明书V4.3

  3. Scala并发编程【进阶】

    package com.dingxin.entrance import java.text.SimpleDateFormat import java.util.Date import scala.ac ...

  4. Python自定义异常及抛出异常

    """ 自定义异常 """ class MyException(Exception): # 继承异常类 def __init__(self, ...

  5. Linux下安装JDK,Tomcat,Mysql详细教程

    1. 概述 今天教大家在linux下安装jdk,mysql以及tomcat.在javaweb开发中,最后完成的项目代码,一般都是布在linux服务器下的.因为linux服务器可以说是安全性稳定性都比w ...

  6. Uvision4创建工程

    创建工程 创建好工程会出现如下的界面 创建文件( .asm 或 .c ) 选择菜单栏中的File->New- 按 Ctrl+S 保存文件并填写文件名 添加到之前创建的工程中 选择Target1- ...

  7. 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树

    平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...

  8. Ubuntu18.04多个版本GCC编译器的切换

    今天make一个程序的时候,发现程序里面使用到了C++17的标准,而我的gcc仍然是4.8,考虑到系统是ubuntu18.04的,所以感觉应该gcc的版本不会这么低. cd到/usr/bin下,使用指 ...

  9. react 之 reflux 填坑

    注意:老铁些,在看这篇文章的之前,最好了解一下react 的全局状态管理库哦,不然可能会坐飞机. ^_^ React 之reflux (它是一个功能模块,需要安装引入): import Reflux ...

  10. Java JDK与JRE

    JDK与JRE JDK(Java Development Kit):Java开发工具包.包含JRE中的所有内容,以及用于开发小应用程序和应用程序的编译器和调试器等工具. JRE(Java Runtim ...