body
{
font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif;
font-size: 10.5pt;
line-height: 1.5;
}
html, body
{

}
h1 {
font-size:1.5em;
font-weight:bold;
}
h2 {
font-size:1.4em;
font-weight:bold;
}
h3 {
font-size:1.3em;
font-weight:bold;
}
h4 {
font-size:1.2em;
font-weight:bold;
}
h5 {
font-size:1.1em;
font-weight:bold;
}
h6 {
font-size:1.0em;
font-weight:bold;
}
img {
border:0;
max-width: 100%;
}
blockquote {
margin-top:0px;
margin-bottom:0px;
}
table {
border-collapse:collapse;
border:1px solid #bbbbbb;
}
td {
border-collapse:collapse;
border:1px solid #bbbbbb;
}

Mybatis 示例之 Association - 偶尔记一下 - 博客频道 - CSDN.NET

接下来的文章中,关于Mybatis的示例,全部来自于Mybatis代码中的单元测试代码,通过这些代码能够学习Mybatis中很有用的知识,
这些内容在doc文档中可能只是简单提到了,或者有一些文字说明,通过这些单元测试能更直观的了解如何在Mybatis使用这些内容。

这一节内容为Association关联的结果查询,就是在查询出结果后,根据查询的列和resultMap定义的对应关系,来创建对象并写入值。

  • association – 一个复杂的类型关联;许多结果将包成这种类型

    • 嵌入结果映射 – 结果映射自身的关联,或者参考一个

(注:“参考一个”,这里参考一个是通过对象的Key来唯一确定的,如果Key值一样,就直接用已经存在的这个对象。)

association是resultMap中的一个配置选项,下面是用到的类的UML图:

Car对象中包含了Engine和Brakes两个对象。Mapper是接口对象。AssociationTest是该测试对象。

SQL表结构和数据:

  1. drop table cars if exists;  
  2.   
  3. create table cars (  
  4.   carid integer,  
  5.   cartype varchar(20),  
  6.   enginetype varchar(20),  
  7.   enginecylinders integer,  
  8.   brakestype varchar(20)  
  9. );  
  10.   
  11. insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(1, 'VW',   'Diesel', 4,    null);  
  12. insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(2, 'Opel',    null,    null, 'drum');  
  13. insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(3, 'Audi', 'Diesel', 4,    'disk');  
  14. insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(4, 'Ford', 'Gas',    8,    'drum');  
  1. drop table cars if exists;
  2.  
  3. create table cars (
  4. carid integer,
  5. cartype varchar(20),
  6. enginetype varchar(20),
  7. enginecylinders integer,
  8. brakestype varchar(20)
  9. );
  10.  
  11. insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(1, 'VW', 'Diesel', 4, null);
  12. insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(2, 'Opel', null, null, 'drum');
  13. insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(3, 'Audi', 'Diesel', 4, 'disk');
  14. insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(4, 'Ford', 'Gas', 8, 'drum');

Mapper.xml文件:

  1. <mapper namespace="org.apache.ibatis.submitted.associationtest.Mapper">  
  2.   
  3.     <resultMap type="org.apache.ibatis.submitted.associationtest.Car" id="carResult">  
  4.         <id column="carid" property="id"/>  
  5.         <result column="cartype" property="type"/>  
  6.         <association property="engine" resultMap="engineResult"/>  
  7.         <association property="brakes" resultMap="brakesResult"/>  
  8.     </resultMap>  
  9.     <resultMap type="org.apache.ibatis.submitted.associationtest.Engine" id="engineResult">  
  10.         <result column="enginetype" property="type"/>  
  11.         <result column="enginecylinders" property="cylinders"/>  
  12.     </resultMap>  
  13.     <resultMap type="org.apache.ibatis.submitted.associationtest.Brakes" id="brakesResult">  
  14.         <result column="brakesType" property="type"/>  
  15.     </resultMap>  
  16.   
  17.     <select id="getCars" resultMap="carResult">  
  18.     select * from cars  
  19.   </select>  
  20.   
  21.     <select id="getCarsNonUnique" resultMap="carResult">  
  22.     select 1 as carid, cartype, enginetype, enginecylinders, brakestype from cars  
  23.   </select>  
  24.   
  25.     <select id="getCars2" resultMap="carResult">  
  26.     select 1 as carid, cartype, enginetype, enginecylinders, brakestype from cars where carid in (1,2)  
  27.   </select>  
  28.   
  29. </mapper>  
  1. <mapper namespace="org.apache.ibatis.submitted.associationtest.Mapper">
  2.  
  3. <resultMap type="org.apache.ibatis.submitted.associationtest.Car" id="carResult">
  4. <id column="carid" property="id"/>
  5. <result column="cartype" property="type"/>
  6. <association property="engine" resultMap="engineResult"/>
  7. <association property="brakes" resultMap="brakesResult"/>
  8. </resultMap>
  9. <resultMap type="org.apache.ibatis.submitted.associationtest.Engine" id="engineResult">
  10. <result column="enginetype" property="type"/>
  11. <result column="enginecylinders" property="cylinders"/>
  12. </resultMap>
  13. <resultMap type="org.apache.ibatis.submitted.associationtest.Brakes" id="brakesResult">
  14. <result column="brakesType" property="type"/>
  15. </resultMap>
  16.  
  17. <select id="getCars" resultMap="carResult">
  18. select * from cars
  19. </select>
  20.  
  21. <select id="getCarsNonUnique" resultMap="carResult">
  22. select 1 as carid, cartype, enginetype, enginecylinders, brakestype from cars
  23. </select>
  24.  
  25. <select id="getCars2" resultMap="carResult">
  26. select 1 as carid, cartype, enginetype, enginecylinders, brakestype from cars where carid in (1,2)
  27. </select>
  28.  
  29. </mapper>

其中的一个测试用例:

  1. @Test  
  2.  public void shouldGetAllCars() {  
  3.    SqlSession sqlSession = sqlSessionFactory.openSession();  
  4.    try {  
  5.      Mapper mapper = sqlSession.getMapper(Mapper.class);  
  6.      List<Car> cars = mapper.getCars();  
  7.      Assert.assertEquals(4, cars.size());  
  8.      Assert.assertEquals("VW", cars.get(0).getType());  
  9.      Assert.assertNotNull(cars.get(0).getEngine());  
  10.      Assert.assertNull(cars.get(0).getBrakes());  
  11.      Assert.assertEquals("Opel", cars.get(1).getType());  
  12.      Assert.assertNull(cars.get(1).getEngine());  
  13.      Assert.assertNotNull(cars.get(1).getBrakes());  
  14.    } finally {  
  15.      sqlSession.close();  
  16.    }  
  17.  }  
  1. @Test
  2. public void shouldGetAllCars() {
  3. SqlSession sqlSession = sqlSessionFactory.openSession();
  4. try {
  5. Mapper mapper = sqlSession.getMapper(Mapper.class);
  6. List<Car> cars = mapper.getCars();
  7. Assert.assertEquals(4, cars.size());
  8. Assert.assertEquals("VW", cars.get(0).getType());
  9. Assert.assertNotNull(cars.get(0).getEngine());
  10. Assert.assertNull(cars.get(0).getBrakes());
  11. Assert.assertEquals("Opel", cars.get(1).getType());
  12. Assert.assertNull(cars.get(1).getEngine());
  13. Assert.assertNotNull(cars.get(1).getBrakes());
  14. } finally {
  15. sqlSession.close();
  16. }
  17. }

cars返回值:

association是嵌套查询中最简单的一种情况,像上述例子中,一般我们都会用一个Car对面包含所有的属性,这里的例子使用了嵌套对象,使对像的结构更鲜明。不过一般情况下很少会拆分一个对象为多个,用的多的时候是多表查询的嵌套。

上面XML中的

carResult和engieResult,brakesResult都是分别定义,carResult引用了另外两个resultMap。

对于不需要重用嵌套对象的情况,还可以直接这么写,把上面的XML修改后:

  1. <resultMap type="org.apache.ibatis.submitted.associationtest.Car" id="carResult">  
  2.     <id column="carid" property="id"/>  
  3.     <result column="cartype" property="type"/>  
  4.     <association property="engine" javaType="org.apache.ibatis.submitted.associationtest.Engine">  
  5.         <result column="enginetype" property="type"/>  
  6.         <result column="enginecylinders" property="cylinders"/>  
  7.     </association>  
  8.     <association property="brakes" resultMap="brakesResult"/>  
  9. </resultMap>  
  1. <resultMap type="org.apache.ibatis.submitted.associationtest.Car" id="carResult">
  2. <id column="carid" property="id"/>
  3. <result column="cartype" property="type"/>
  4. <association property="engine" javaType="org.apache.ibatis.submitted.associationtest.Engine">
  5. <result column="enginetype" property="type"/>
  6. <result column="enginecylinders" property="cylinders"/>
  7. </association>
  8. <association property="brakes" resultMap="brakesResult"/>
  9. </resultMap>

为了对比和区分,这里指修改了Engine,在association元素上增加了属性javaType,元素内增加了result映射。

如果有association方面问题可以参考(或在此留言):

http://mybatis.github.io/mybatis-3/zh/sqlmap-xml.html

本节源码请看官方git:

https://github.com/mybatis/mybatis-3/tree/master/src/test/java/org/apache/ibatis/submitted/associationtest

Mybatis 示例之 Association - 偶尔记一下 - 博客频道 - CSDN.NET的更多相关文章

  1. Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET

    Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET Boost::Thread使用示例 分类: C/C++ 2011-07-06 14:48 5926 ...

  2. boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET

    boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET     boost::crc_32_type crc32;       crc32. ...

  3. 让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET

    让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET 让 QtWebkit 支持跨域CROS 2013-05-23 22:05 450人阅读 评论 ...

  4. ibatis 学习笔记 3 - pfpfpfpfpf的专栏 - 博客频道 - CSDN.NET

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  5. Cannot call sendError() after the response has been committed - baiyangliu - 博客频道 - CSDN.NET

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  6. 最牛B的编码套路 - 呦呦鹿鸣 - 博客频道 - CSDN.NET

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  7. java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET

    java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了!      社区福利快来领取免费参加MDCC大会机会哦    Tag功能介绍—我们 ...

  8. Notepad++前端开发常用插件介绍 - BorisHuai前端修炼 - 博客频道 - CSDN

    Notepad++前端开发常用插件介绍 - BorisHuai前端修炼 - 博客频道 - CSDN.NET http://blog.csdn.net/borishuai/article/details ...

  9. 帧与场 - djf_1985的专栏 - 博客频道 - CSDN.NET

    帧与场 - djf_1985的专栏 - 博客频道 - CSDN.NET 电视信号是通过摄像机对自然景物的扫描并经光电转换形成的.扫描方式分为“逐行扫描”和“隔行扫描”.“逐行扫描”指每幅图像均是由电子 ...

随机推荐

  1. docker installation on ubuntu

    Ubuntu Docker is supported on these Ubuntu operating systems: Ubuntu Xenial 16.04 (LTS) Ubuntu Trust ...

  2. docker镜像与容器概念

    本文用图文并茂的方式介绍了容器.镜像的区别和Docker每个命令后面的技术细节,能够很好的帮助读者深入理解Docker. 这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(containe ...

  3. 源码篇:SDWebImage

    攀登,一步一个脚印,方能知其乐 源码篇:SDWebImage 源码来源:https://github.com/rs/SDWebImage 版本: 3.7 SDWebImage是一个开源的第三方库,它提 ...

  4. Swift 学习笔记 (三) 之循环引用浅析

    原创:转载请注明出处 110.自动引用计数实践 下面的例子展示了自动引用计数的工作机制.例子以一个简单的Person类开始,并定义了一个叫name的常量属性: class Person {     l ...

  5. PAT (Advanced Level) 1107. Social Clusters (30)

    简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  6. double类型之四舍五入

    题目: A - Tutor Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submi ...

  7. 提升html5的性能体验系列之一避免切页白屏

    窗体切换白屏的现实问题 HTML5的性能比原生差很多,比如切页时白屏.列表滚动不流畅.下拉刷新和上拉翻页卡顿.在低端Android手机上,很多原生App常用的功能和体验效果都很难使用HTML5技术模拟 ...

  8. 检查本功能是否在Excel中运行

    BOOL g_bParentIsNotEXCEL_EXE = FALSE; BOOL isNotEXCEL_EXE( DWORD dwProcessID ) { HANDLE hProcessSnap ...

  9. 简单查询plan

    -> alter session set statistics_level=all; select /*+ gathe_plan_statistics */ * from ts.ts_recor ...

  10. spider JAVA如何判断网页编码 (转载)

    原文链接 http://www.cnblogs.com/nanxin/archive/2013/03/27/2984320.html 前言 最近做一个搜索项目,需要爬取很多网站获取需要的信息.在爬取网 ...