@

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. <自动化测试方案_2>第二章、自动化测试是什么?(What)

    第二章.自动化测试是什么?(What) 自动化测试是相对于手工测试而言:通过脚本自动去执行测试用例,从而代替人完成测试工作. 自动化测试相对手工测试优缺点 测试方式 优点 缺点 手工测试 1,完整的对 ...

  2. 转载:python生成以及打开json、csv和txt文件

    原文地址:https://blog.csdn.net/weixin_42555131/article/details/82012642 生成txt文件: mesg = "hello worl ...

  3. abseil初体验[google开源的C++库]

    Google公开了其项目内部使用的一系列C++库,具体介绍参考: http://www.infoq.com/cn/news/2017/10/abseil?utm_source=infoq&ut ...

  4. 洗礼灵魂,修炼python(89)-- 知识拾遗篇 —— 进程

    进程 1.含义:计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位.说白了就是一个程序的执行实例. 执行一个程序就是一个进程,比如你打开浏览器看到我的博客,浏览器本身是一 ...

  5. UIWebView的高度不对问题

    一般情况,在- (void)webViewDidFinishLoad:(UIWebView *) webView方法里添加如下代码: CGSize actualSize = [webView size ...

  6. C语言的main函数到底该怎么写

    公众号[编程珠玑]:专注但不限于分享计算机编程基础,Linux,C语言,C++,Python,数据库等编程相关[原创]技术文章,号内包含大量经典电子书和视频学习资源.欢迎一起交流学习,一起修炼计算机“ ...

  7. 【递推】ZSC1074: 数学、阿牛的EOF牛肉串

    Description 今年的ACM暑期集训队一共有18人,分为6支队伍.其中有一个叫做EOF的队伍,由04级的阿牛.XC以及05级的COY组成.在共同的集训生活中,大家建立了深厚的友谊,阿牛准备做点 ...

  8. python 反射、md5加密

    一.issubclass,type,isinstance 1.issubclass :判断xx类是否是yyy类型(包括子类),用于类之间的判定 class GrandF: pass class Fat ...

  9. VsCode放大缩小

    如图:vscode界面突然缩小了 通过快捷键 ctrl+shift+ + 放大  ctrl+shift+ - 缩小

  10. chrome的source工具

    http://www.cnblogs.com/ys-ys/p/5597717.html http://www.cnblogs.com/strick/p/5556434.html