@

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. WPF:Metro样式ProgressBar(圆点横向移动),自适应宽度

    先看效果图: 最直观的,这是4个圆点在移动,就用一个横向的StackPanel表示这四个点吧. <StackPanel Orientation="Horizontal"> ...

  2. seajs的原理以及基本使用

    seajs模块化开发 模块化开发,把整个文件分割成一个一个小文件. 使用方法 使用方法特别简单,首先在官网中下载sea.js,然后在页面中引入. index.html // 1.路径 // 2.回调 ...

  3. html之CSS样式学习笔记

    本文内容: 字体样式 文本样式 背景样式 尺寸样式 盒子模型 边框样式 边距样式 浮动布局 定位布局 [CSS3]伸缩布局 标签显示方式 列表样式 [CSS3]过渡样式 [CSS3]变换样式之2D变形 ...

  4. matlab练习程序(点集配准的SVD法)

    上一篇博客中我们使用了四元数法计算ICP. 本篇我们使用SVD计算ICP. 下面是<视觉slam十四讲>中的计算方法: 计算步骤如下: 我们看到,只要求出了两组点之间的旋转,平移是非常容易 ...

  5. ERP口碑订单无法落桌的解决方法

    第一步,退出ERP 第二步,打开控制面板卸载erp 第三步,删除erp安装路径的所有文件 第四步,卸载sql服务,操作方法如下(win+R—输入cmd—输入sc delete mysql_sl 回车键 ...

  6. Springboot helloworld入门最经典例子

    一.建立maven java项目 导入springboot包 二.配置pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0 ...

  7. C++ 标准 和 C 标准 (截止到2019年03月)

    C++ 标准:维基百科 Year C++ Standard Informal name 1998 ISO/IEC 14882:1998[23] C++98 2003 ISO/IEC 14882:200 ...

  8. February 21st, 2018 Week 8th Wednesday

    Our life is what our thoughts make it. 我们的思想成就了我们的生活. The mind is everything. What you think, you be ...

  9. Mac轻量级服务器http-server

    刚想跑个Vue页面,发现我本地没有应用服务器(Tomcat/IIS...) 于是想下载了Tomcat,才发现我没有装JDK,而Mac的JDK下得好久,都下不下来,想想算了. 于是在网上找个轻量级的服务 ...

  10. DBUtils温习2

    上篇简单回顾了下DBUtils的简介和其常用的类,这篇博客结合C3P0连接池,做一个简单的增删改查操作 1.创建web项目,导入jar包 2.编写c3p0-config.xml和引入工具类 <? ...