ResultMaps are used to map the SQL SELECT statement's results to JavaBeans properties. We can define ResultMaps and reference this resultMap query from
several SELECT statements.

We can extend one <resultMap> query from another <resultMap> query, thereby inheriting the column to do property mappings from the one that is being extended.

<resultMap type="Student" id="StudentResult">
<id property="studId" column="stud_id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<result property="phone" column="phone"/>
</resultMap> <resultMap type="Student" id="StudentWithAddressResult" extends="StudentResult">
<result property="address.addrId" column="addr_id"/>
<result property="address.street" column="street"/>
<result property="address.city" column="city"/>
<result property="address.state" column="state"/>
<result property="address.zip" column="zip"/>
<result property="address.country" column="country"/>
</resultMap>

The resultMap query with the ID StudentWithAddressResult extends the resultMap with the ID StudentResult.

Now you can use StudentResult resultMap if you want to map only the Student data as shown in the following code:

<select id="findStudentById" parameterType="int" resultMap="StudentResult">
SELECT * FROM STUDENTS WHERE STUD_ID=#{studId}
</select>

If you want to map the query results with Student along with the Address data, you can use resultMap with the ID StudentWithAddressResult as follows:

<select id="selectStudentWithAddress" parameterType="int" resultMap="StudentWithAddressResult">
SELECT STUD_ID, NAME, EMAIL, PHONE, A.ADDR_ID, STREET, CITY, STATE, ZIP, COUNTRY
FROM STUDENTS S LEFT OUTER JOIN ADDRESSES A
ON S.ADDR_ID = A.ADDR_ID
WHERE STUD_ID = #{studId}
</select>

MyBatis(3.2.3) - ResultMaps: Extending ResultMaps的更多相关文章

  1. 关于mybatis的思考(3)——ResultMaps的使用

    ResultMap元素在mybatis中非常重要,目的是告诉mybatis将从结果集中取出的数据转换为开发者需要的对象.     UserMapping.xml <!-- selectAll操作 ...

  2. Table of Contents - MyBatis

    Getting Started with MyBatis Hello World Integration with Spring Bootstrapping MyBatis Configuring M ...

  3. 【MyBatis源码分析】Configuration加载(下篇)

    元素设置 继续MyBatis的Configuration加载源码分析: private void parseConfiguration(XNode root) { try { Properties s ...

  4. Mybatis源码分析之结果集处理

    解析封装 ResultMap 是和结果集相关的东西,最初在解析 XML 的时候,于 parseStatementNode 方法中,针对每一个 select 节点进行解析,转换为 MappedState ...

  5. Java Persistence with MyBatis 3(中文版) 第三章 使用XML配置SQL映射器

    关系型数据库和SQL是经受时间考验和验证的数据存储机制.和其他的ORM 框架如Hibernate不同,MyBatis鼓励开发者可以直接使用数据库,而不是将其对开发者隐藏,因为这样可以充分发挥数据库服务 ...

  6. Mybaits 源码解析 (三)----- Mapper接口底层原理(为什么Mapper不用写实现类就能访问到数据库?)

    上一篇我们讲解到mapperElement方法用来解析mapper,我们这篇文章具体来看看mapper.xml的解析过程 mappers配置方式 mappers 标签下有许多 mapper 标签,每一 ...

  7. IBatisNet基础组件

    DomSqlMapBuilder DomSqlMapBuilder,其作用是根据配置文件创建SqlMap实例.可以通过这个组件从Stream, Uri, FileInfo, or XmlDocumen ...

  8. IbatisNet开发使用小结

    一.   介绍 平常做企业级应用,需求变化是经常的事,而很多基础代码重复也是很让人头疼的问题.所以很多人会使用一些ORM框架来增强项目的可维护性.可扩展性.IBatis.Net就是一个比较易用的ORM ...

  9. Mybatis逆向工程 —— ResultMaps collection already contains value for ***

    报错提示: Result Maps collection already contains value for ***. 遭遇场景: maven+ssm 项目中,采用了mybatis的逆向工程生成 p ...

随机推荐

  1. 转载C# 对象转Json序列化

    转载原地址:  http://www.cnblogs.com/plokmju/p/ObjectByJson.html JSON Json(JavaScript Object Notation) 是一种 ...

  2. Web.config之连接字介绍

    一.连接字配置方式 web.config中有两种方式来配置连接字:<appsetting>中配置,<connectionStrings>中配置. 1.<appsettin ...

  3. rsync服务架设(数据同步|文件增量备份)

        近期由于业务需要,需要将两台服务器数据保持同步.方案有很多,rsync是其中一种解决方案,本文对rsync的安装及配置进行简单说明,其他实现方式有兴趣可以研究.以下是本文提纲,供参考: rsy ...

  4. Spring+Ibatis集成开发实例

    首先简历数据库demo(本文选mysql) 数据库脚本: CREATE TABLE `ibatis` (  `id` varchar(20) NOT NULL,  `name` varchar(20) ...

  5. stm32上的Lava虚拟机开发进度汇报(3)

    感觉遇到一个瓶颈了,这几天都没有什么进度. 前几天把函数和一些最基本的伪指令实现了一下,能跑一点仅使用了绘图函数的lav,但是函数调用的问题一直没解决. 后来发现是粗心漏写了个++,解决了函数调用的问 ...

  6. RT-Thread学习笔记(2)

    这段时间稍微折腾了一下stm32,稍微知道了一点stm32程序的编写方法,所以再次拿起了rtt,因为这个东西确实很强大. 随手记录一下rtt的一些知识: 1.关于finsh 这是一个命令行系统,很好玩 ...

  7. 分享一个jQuery动态网格布局插件:Masonry(转)

    在线演示 Masonry是 一款非常强大的jQuery动态网格布局插件,可以帮助开发人员快速开发类似剪贴画的界面效果.和CSS中float的效果不太一样的地方在 于,float先水平排列,然后再垂直排 ...

  8. linux后端运行

    程序命令 & :将命令放入后台运行. Ctrl + z : 把一个正在运行的前端命令转移到后台运行,它等效于:程序命令 & :这样虽然把程序放在了后端运行,但是此时程序状态为暂停状态, ...

  9. JS中URL编码参数(UrlEncode)

    JS中URL编码参数(UrlEncode) 网上有很多文字作品写涉及在JS中呈现类似UrlEncode功能时都是自定义参数来呈现,其实JS中本身就有那样的参数.参数parameter由于用类似URL的 ...

  10. 新一代 PHP 加速插件 Zend Opcache

    参考:http://www.laogui.com/Zend-Opcache 大家知道目前PHP的缓存插件一般有三个:APC.eAccelerator.XCache,但未来它们可能都会消失,因为PHP ...