Mybatis 示例之 Association - 偶尔记一下 - 博客频道 - CSDN.NET
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表结构和数据:
- drop table cars if exists;
- create table cars (
- carid integer,
- cartype varchar(20),
- enginetype varchar(20),
- enginecylinders integer,
- brakestype varchar(20)
- );
- insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(1, 'VW', 'Diesel', 4, null);
- insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(2, 'Opel', null, null, 'drum');
- insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(3, 'Audi', 'Diesel', 4, 'disk');
- insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(4, 'Ford', 'Gas', 8, 'drum');
drop table cars if exists; create table cars (
carid integer,
cartype varchar(20),
enginetype varchar(20),
enginecylinders integer,
brakestype varchar(20)
); insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(1, 'VW', 'Diesel', 4, null);
insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(2, 'Opel', null, null, 'drum');
insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(3, 'Audi', 'Diesel', 4, 'disk');
insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(4, 'Ford', 'Gas', 8, 'drum');
Mapper.xml文件:
- <mapper namespace="org.apache.ibatis.submitted.associationtest.Mapper">
- <resultMap type="org.apache.ibatis.submitted.associationtest.Car" id="carResult">
- <id column="carid" property="id"/>
- <result column="cartype" property="type"/>
- <association property="engine" resultMap="engineResult"/>
- <association property="brakes" resultMap="brakesResult"/>
- </resultMap>
- <resultMap type="org.apache.ibatis.submitted.associationtest.Engine" id="engineResult">
- <result column="enginetype" property="type"/>
- <result column="enginecylinders" property="cylinders"/>
- </resultMap>
- <resultMap type="org.apache.ibatis.submitted.associationtest.Brakes" id="brakesResult">
- <result column="brakesType" property="type"/>
- </resultMap>
- <select id="getCars" resultMap="carResult">
- select * from cars
- </select>
- <select id="getCarsNonUnique" resultMap="carResult">
- select 1 as carid, cartype, enginetype, enginecylinders, brakestype from cars
- </select>
- <select id="getCars2" resultMap="carResult">
- select 1 as carid, cartype, enginetype, enginecylinders, brakestype from cars where carid in (1,2)
- </select>
- </mapper>
<mapper namespace="org.apache.ibatis.submitted.associationtest.Mapper"> <resultMap type="org.apache.ibatis.submitted.associationtest.Car" id="carResult">
<id column="carid" property="id"/>
<result column="cartype" property="type"/>
<association property="engine" resultMap="engineResult"/>
<association property="brakes" resultMap="brakesResult"/>
</resultMap>
<resultMap type="org.apache.ibatis.submitted.associationtest.Engine" id="engineResult">
<result column="enginetype" property="type"/>
<result column="enginecylinders" property="cylinders"/>
</resultMap>
<resultMap type="org.apache.ibatis.submitted.associationtest.Brakes" id="brakesResult">
<result column="brakesType" property="type"/>
</resultMap> <select id="getCars" resultMap="carResult">
select * from cars
</select> <select id="getCarsNonUnique" resultMap="carResult">
select 1 as carid, cartype, enginetype, enginecylinders, brakestype from cars
</select> <select id="getCars2" resultMap="carResult">
select 1 as carid, cartype, enginetype, enginecylinders, brakestype from cars where carid in (1,2)
</select> </mapper>
其中的一个测试用例:
- @Test
- public void shouldGetAllCars() {
- SqlSession sqlSession = sqlSessionFactory.openSession();
- try {
- Mapper mapper = sqlSession.getMapper(Mapper.class);
- List<Car> cars = mapper.getCars();
- Assert.assertEquals(4, cars.size());
- Assert.assertEquals("VW", cars.get(0).getType());
- Assert.assertNotNull(cars.get(0).getEngine());
- Assert.assertNull(cars.get(0).getBrakes());
- Assert.assertEquals("Opel", cars.get(1).getType());
- Assert.assertNull(cars.get(1).getEngine());
- Assert.assertNotNull(cars.get(1).getBrakes());
- } finally {
- sqlSession.close();
- }
- }
@Test
public void shouldGetAllCars() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
Mapper mapper = sqlSession.getMapper(Mapper.class);
List<Car> cars = mapper.getCars();
Assert.assertEquals(4, cars.size());
Assert.assertEquals("VW", cars.get(0).getType());
Assert.assertNotNull(cars.get(0).getEngine());
Assert.assertNull(cars.get(0).getBrakes());
Assert.assertEquals("Opel", cars.get(1).getType());
Assert.assertNull(cars.get(1).getEngine());
Assert.assertNotNull(cars.get(1).getBrakes());
} finally {
sqlSession.close();
}
}
cars返回值:
association是嵌套查询中最简单的一种情况,像上述例子中,一般我们都会用一个Car对面包含所有的属性,这里的例子使用了嵌套对象,使对像的结构更鲜明。不过一般情况下很少会拆分一个对象为多个,用的多的时候是多表查询的嵌套。
上面XML中的
carResult和engieResult,brakesResult都是分别定义,carResult引用了另外两个resultMap。
对于不需要重用嵌套对象的情况,还可以直接这么写,把上面的XML修改后:
- <resultMap type="org.apache.ibatis.submitted.associationtest.Car" id="carResult">
- <id column="carid" property="id"/>
- <result column="cartype" property="type"/>
- <association property="engine" javaType="org.apache.ibatis.submitted.associationtest.Engine">
- <result column="enginetype" property="type"/>
- <result column="enginecylinders" property="cylinders"/>
- </association>
- <association property="brakes" resultMap="brakesResult"/>
- </resultMap>
<resultMap type="org.apache.ibatis.submitted.associationtest.Car" id="carResult">
<id column="carid" property="id"/>
<result column="cartype" property="type"/>
<association property="engine" javaType="org.apache.ibatis.submitted.associationtest.Engine">
<result column="enginetype" property="type"/>
<result column="enginecylinders" property="cylinders"/>
</association>
<association property="brakes" resultMap="brakesResult"/>
</resultMap>
为了对比和区分,这里指修改了Engine,在association元素上增加了属性javaType,元素内增加了result映射。
如果有association方面问题可以参考(或在此留言):
http://mybatis.github.io/mybatis-3/zh/sqlmap-xml.html
本节源码请看官方git:
Mybatis 示例之 Association - 偶尔记一下 - 博客频道 - CSDN.NET的更多相关文章
- Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET
Boost::Thread使用示例 - CG-Animation - 博客频道 - CSDN.NET Boost::Thread使用示例 分类: C/C++ 2011-07-06 14:48 5926 ...
- boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET
boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET boost::crc_32_type crc32; crc32. ...
- 让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET
让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET 让 QtWebkit 支持跨域CROS 2013-05-23 22:05 450人阅读 评论 ...
- ibatis 学习笔记 3 - pfpfpfpfpf的专栏 - 博客频道 - CSDN.NET
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- Cannot call sendError() after the response has been committed - baiyangliu - 博客频道 - CSDN.NET
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- 最牛B的编码套路 - 呦呦鹿鸣 - 博客频道 - CSDN.NET
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET
java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了! 社区福利快来领取免费参加MDCC大会机会哦 Tag功能介绍—我们 ...
- Notepad++前端开发常用插件介绍 - BorisHuai前端修炼 - 博客频道 - CSDN
Notepad++前端开发常用插件介绍 - BorisHuai前端修炼 - 博客频道 - CSDN.NET http://blog.csdn.net/borishuai/article/details ...
- 帧与场 - djf_1985的专栏 - 博客频道 - CSDN.NET
帧与场 - djf_1985的专栏 - 博客频道 - CSDN.NET 电视信号是通过摄像机对自然景物的扫描并经光电转换形成的.扫描方式分为“逐行扫描”和“隔行扫描”.“逐行扫描”指每幅图像均是由电子 ...
随机推荐
- iptables基础知识
iptables防火墙可以用于创建过滤(filter)与NAT规则.所有Linux发行版都能使用iptables,因此理解如何配置 iptables将会帮助你更有效地管理Linux防火墙.如果你是第一 ...
- Zabbix3.0 安装Graphtree
zabbix中,想要集中展示图形,唯一的选择是screen,zatree可以解决这个问题,但是性能不是很好. Graphtree由OneOaas开发并开源出来,用来解决zabbix的图形展示问题,性能 ...
- 转 Apache Ant 实现自动化部署
Apache Ant 实现自动化部署 Apache Ant 实现自动化部署 http://www.netkiller.cn/journal/java.ant.html Mr. Neo Chen (陈景 ...
- 关于tag,viewWithTag
iOS SDK内置了一套搜寻机制,可通过tag来查找子视图. **苹果公司很少给子视图设置tag.笔者所知范围的唯一例外出现在UIAlertView中,该类会给按钮分别设置值为1.2的标签 viewW ...
- 函数求值一<找规律>
函数求值 题意: 定义函数g(n)为n最大的奇数因子.求f(n)=g(1)+g(2)+g(3)+-+g(n).1<=n<=10^8; 思路: 首先明白暴力没法过.问题是如何求解,二分.知道 ...
- mysql 数字字段的类型选择
bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储大小为 8 ...
- MVVM的架构设计与团队协作 with StoryBoard
今天写这篇博客是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇博客的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...
- 获取spring bean的utils
<span style="font-size:10px;">package com.record.util; import org.springframework.be ...
- elasticearch 安装
1.elasticsearch需要安装jdk1.7以上 2.在ubuntu下不能以root运行,需要建立专门账号 添加组 root@ubuntu:~/Downloads/elasticsearch-/ ...
- CodeForces 525D Arthur and Walls
广搜.看了官方题解才会的..... 定义2*2的小矩阵,有三个是点,一个是星,这样的小矩阵被称为元素块. 首先把所有元素块压入队列,每次取出对头,检查是否还是元素块,如果是 那么将那个*改为点,否则跳 ...