We can get Student along with the Address details using a nested Select query 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>
<select id="findAddressById" parameterType="int" resultMap="AddressResult">
SELECT * FROM ADDRESSES WHERE ADDR_ID = #{id}
</select> <resultMap type="Student" id="StudentWithAddressResult">
<id property="studId" column="stud_id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<association property="address" column="addr_id" select="findAddressById"/>
</resultMap>
<select id="findStudentWithAddress" parameterType="int" resultMap="StudentWithAddressResult">
SELECT * FROM STUDENTS WHERE STUD_ID = #{Id}
</select>

In this approach, the <association> element's select attribute is set to the statement id findAddressById. Here, two separate SQL statements will be executed against the database, the first one called findStudentById to load student details and the second one called findAddressById to load its address details.

The addr_id column value will be passed as input to the selectAddressById statement.

We can invoke the findStudentWithAddress mapped statement as follows:

StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.selectStudentWithAddress(studId);
System.out.println(student);
System.out.println(student.getAddress());

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

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

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

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

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

  3. MyBatis(3.2.3) - One-to-one mapping using nested ResultMap

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

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

    上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...

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

    上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...

  6. mybatis的嵌套查询(嵌套查询nested select和嵌套结果nested results查询)区别

    (转自:http://blog.csdn.net/canot/article/details/51485955) Mybatis表现关联关系比hibernate简单,没有分那么细致one-to-man ...

  7. 深入浅出Mybatis系列(八)---mapper映射文件配置之select、resultMap[转]

    上篇<深入浅出Mybatis系列(七)---mapper映射文件配置之insert.update.delete>介绍了insert.update.delete的用法,本篇将介绍select ...

  8. mybatis中union可以用if判断连接,但是<select>中第一个select语句不能被if判断,因此可以从dual表中查询null来凑齐。union如果使用order by排序,那么只能放在最后一个查询语句的位置,并且不能带表名。

    <!-- 一址多证纳税人分析表 --> <select id="yzdznsrlistPage" parameterType="page" r ...

  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. CodeForces 706C Hard problem (水DP)

    题意:对于给定的n个字符串,可以花费a[i]  将其倒序,问是否可以将其排成从大到小的字典序,且花费最小是多少. 析:很明显的水DP,如果不是水DP,我也不会做.... 这个就要二维,d[2][max ...

  2. Django 使用原生SQL

    def dictfetchall(cursor): "将游标返回的结果保存到一个字典对象中" desc = cursor.description return [ dict(zip ...

  3. mysql 报错之创建自定义函数

    I experienced this error while trying to alter one of my stored procedures remotely on a master serv ...

  4. 常用小方法 or 语法

    --> 获取外部文件 def groovyUtils = new GroovyUtils( context ) def xmlFilePath = groovyUtils.getProjectP ...

  5. 目录启动CXF启动报告LinkageError异常以及Java的endorsed机制

    本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~ Exception in thread "main" java.lang.LinkageError: JA ...

  6. C++ 中复杂的声明

    1.方法也是有类型的,方法的类型由返回类型和形参表决定.比如int F (int)的类型就是去掉方法名,int (int). 2.对于方法类型,在返回类型和形参表之间,加上一个名称F,就表示一个特定的 ...

  7. pjsip视频通信开发(上层应用)之拨号键盘下部份拨号和删除功能

    我们开发的是视频电话,所以既可以视频通话,可以只有音频的通话,所以底部含有两个按钮,最后一个就是删除功能,如果输入错误,我们可以删除输入的内容. 这里我们要通过重写LinearLayout来实现这部份 ...

  8. [MODx] 1. Add Html5 template into the MODx

    1. Connet MODx by SSH: Go to the MODx cloud; Find you current user and right click selet Edit Cloud; ...

  9. 合并js文件minify实例

    将min目录放入项目中后,js中引入方式是: <script type="text/javascript" src="__PUBLIC__/min/?b=publi ...

  10. Javascript-显示系统时间

    /*JS-显示系统时间*/function showLocale(objD) {    var str, colorhead, colorfoot;    var yy = objD.getYear( ...