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. Grand Central Dispatch(GCD)编程基础

    GCD:线程 http://blog.csdn.net/nono_love_lilith/article/details/7829557

  2. java中动态代理

    一.在java中怎样实现动态代理 1.我们要有一个接口,还要有一个接口的实现类,而这个实现类呢就是我们要代理的对象 接口: package org.dynamicproxy.test; public ...

  3. list对象排序

    在数据库中查出来的列表list中,往往需要对不同的字段重新排序,一般的做法都是使用排序的字段,重新到数据库中查询.如果不到数据库查询,直接在第一次查出来的list中排序,无疑会提高系统的性能. 只要把 ...

  4. .NET通用权限系统快速开发框架

    DEMO下载地址: http://download.csdn.net/detail/shecixiong/5372895 一.开发技术:B/S(.NET C# ) 1.Windows XP以上 (支援 ...

  5. 2014广州Java岗位面试汇总

    本文记录了最近一些朋友提供的面试经历,真实数据,仅供广州求职的朋友参考.为行文方便,一律用主语”我“进行.部分词语可能造成读者不良反应,敬请留意. 1  广州沣首信息科技有限公司 公司所在区域相对较偏 ...

  6. PowerShell管理IIS(新建站点、应用程序池、应用程序、虚拟目录等)

    #导入IIS管理模块 Import-Module WebAdministration #新建应用程序池 api.dd.com New-Item iis:\AppPools\api.dd.com Set ...

  7. URAL 1775 B - Space Bowling 计算几何

    B - Space BowlingTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  8. MyBatis之五:动态sql语句

    在mybatis 3 或以上的版本提供了4类标签,分别是:if,choose(when,otherwise),rim(where,set),foreach.接下来将分别介绍这几种标签的具体用法,映射x ...

  9. delphi 08 HTML组件

    ///HTML组件///后面的字符串为这个控件的ID号///直线          Line         (WebBrowser1.Document as IHTMLDocument2).exec ...

  10. Android播播放完SD卡指定文件夹音乐之后,自动播放下一首

    最近做一个项目,需要连续播放音乐,播放完一首歌之后,自动播放完下一首歌.不要重复播放. 代码如下: package com.example.asyncplayer_ex; import java.io ...