We can get Student along with the Address details using a nested ResultMap as follows:

<resultMap type="Address" id="AddressResult">
<id property="addrId" column="addr_id"/>
<result property="street" column="street"/>
<result property="city" column="city"/>
<result property="state" column="state"/>
<result property="zip" column="zip"/>
<result property="country" column="country"/>
</resultMap>
<resultMap type="Student" id="StudentWithAddressResult">
<id property="studId" column="stud_id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<association property="address" resultMap="AddressResult"/>
</resultMap> <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>

The <association> element can be used to load the has-one type of associations. In the preceding example, we used the <association> element, referencing another <resultMap> that is declared in the same XML file.

We can also use <association> with an inline resultMap query as follows:

<resultMap type="Student" id="StudentWithAddressResult">
<id property="studId" column="stud_id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<association property="address" javaType="Address">
<id property="addrId" column="addr_id"/>
<result property="street" column="street"/>
<result property="city" column="city"/>
<result property="state" column="state"/>
<result property="zip" column="zip"/>
<result property="country" column="country"/>
</association>
</resultMap>

Using the nested ResultMap approach, the association data will be loaded using a single query (along with joins if required).

MyBatis(3.2.3) - One-to-one mapping using nested ResultMap的更多相关文章

  1. MyBatis学习 之 二、SQL语句映射文件(1)resultMap

    目录(?)[-] 二SQL语句映射文件1resultMap resultMap idresult constructor association联合 使用select实现联合 使用resultMap实 ...

  2. Mybatis 自动从数据库生成entity,mapping,dao接口

    1.下载需要的jar包 mybatis-generator-core-1.3.2.jar,mysql-connector-java-5.1.39.jar 2.把上面的jar包放到某个目录,并在该目录下 ...

  3. MyBatis---使用MyBatis Generator生成Dto、Dao、Mapping

    由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类.Dao接口类甚至Mappi ...

  4. MyBatis(3.2.3) - One-to-one mapping using nested Select

    We can get Student along with the Address details using a nested Select query as follows: <result ...

  5. mybatis项目启动报错 The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)".

    启动项目报错 2018-02-26 17:09:51,535 ERROR [org.springframework.web.context.ContextLoader] - Context initi ...

  6. SpringBoot+MyBatis中自动根据@Table注解和@Column注解生成ResultMap

    其实我一点都不想用mybatis,好多地方得自己写,比如这里. 使用mybatis要写大量的xml,烦的一批.最烦人的莫过于写各种resultmap,就是数据库字段和实体属性做映射.我认为这里应该是m ...

  7. mybatis二(参数处理和map封装及自定义resultMap)

    .单个参数 mybatis不会做特殊处理. #{参数名/任意名}:取出参数值. .多个参数 mybatis会做特殊处理. 多个参数会被封装成 一个map. key:param1...paramN,或者 ...

  8. 深入浅出Mybatis系列八-mapper映射文件配置之select、resultMap

    注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之inse ...

  9. MyBatis(3.2.3) - One-to-many mapping

    In the sample domain model, a tutor can teach one or more courses. This means that there is a one-to ...

随机推荐

  1. 转载 C#中静态类和非静态类比较

    转载原地址: http://www.cnblogs.com/NothingIsImpossible/archive/2010/07/28/1786706.html C#静态方法与非静态方法的区别不仅仅 ...

  2. C#枚举数值与名称的转换

    在应用枚举的时候,时常需要将枚举和数值相互转换的情况.有时候还需要转换成相应的中文.下面介绍一种方法. 首先建立一个枚举: /// <summary> /// 颜色 /// </su ...

  3. 创建类模式(零):简单/静态工厂(Static Factory)

    定义 简单工厂模式属于创建型模式,但不属于23种GOF设计模式之一,这也是为什么该模式标记为零的原因.简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例.简单工厂模式是工厂模式家族中最简单实用的 ...

  4. Objective-C: NSFileManager 的使用

    在Objective-C 中的 Foundation 框架中,文件操作是由NSFileManager 类来实现的. 下面通过例子来说明如何创建一个文件,并向文件中写内容,以及如何读出文件中的内容: - ...

  5. 比较器comparable与comparator的使用

    在Java学习和使用里,工具类与算法类(collections和Arrays)也是我们使用比较多的,在它们里面就包含了comparable与comparator这两种比较器. 一.比较器的分类与概念 ...

  6. Intel® RealSense™ SDK Architecture

    In this article, we highlight some of the key changes that you can expect to see in the Intel RealSe ...

  7. 创建虚拟交换机(New-VMSwitch)

    #获取网卡列表Get-NetAdapter

  8. 微信开发模式(php)

    才開始学习微信开发模式和PHP,在此记录以供后阅. <?php /** * wechat php test */ //define your token define( "TOKEN& ...

  9. C#-datagriview的表头高度的设置

  10. Java中Queue类实现

    原先在java编程中,Queue的实现都是用LinkedList Queue queue = new LinkedList(); 但正如jdk中所说的那样: 注意,此实现不是同步的.如果多个线程同时访 ...