Mybatis 入門示例代碼之 Association
接下來的文章中,關(guān)于Mybatis的示例,全部來自于Mybatis代碼中的單元測試代碼,通過這些代碼能夠?qū)W習(xí)Mybatis中很有用的知識(shí),這些內(nèi)容在doc文檔中可能只是簡單提到了,或者有一些文字說明,通過這些單元測試能更直觀的了解如何在Mybatis使用這些內(nèi)容。
這一節(jié)內(nèi)容為Association關(guān)聯(lián)的結(jié)果查詢,就是在查詢出結(jié)果后,根據(jù)查詢的列和resultMap定義的對應(yīng)關(guān)系,來創(chuàng)建對象并寫入值。
- association – 一個(gè)復(fù)雜的類型關(guān)聯(lián);許多結(jié)果將包成這種類型
- 嵌入結(jié)果映射 – 結(jié)果映射自身的關(guān)聯(lián),或者參考一個(gè)
(注:“參考一個(gè)”,這里參考一個(gè)是通過對象的Key來唯一確定的,如果Key值一樣,就直接用已經(jīng)存在的這個(gè)對象。)
association是resultMap中的一個(gè)配置選項(xiàng),下面是用到的類的UML圖:

Car對象中包含了Engine和Brakes兩個(gè)對象。Mapper是接口對象。AssociationTest是該測試對象。
SQL表結(jié)構(gòu)和數(shù)據(jù):
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>
其中的一個(gè)測試用例:
@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是嵌套查詢中最簡單的一種情況,像上述例子中,一般我們都會(huì)用一個(gè)Car對面包含所有的屬性,這里的例子使用了嵌套對象,使對像的結(jié)構(gòu)更鮮明。不過一般情況下很少會(huì)拆分一個(gè)對象為多個(gè),用的多的時(shí)候是多表查詢的嵌套。
上面XML中的
carResult和engieResult,brakesResult都是分別定義,carResult引用了另外兩個(gè)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>
為了對比和區(qū)分,這里指修改了Engine,在association元素上增加了屬性javaType,元素內(nèi)增加了result映射。
如果有association方面問題可以參考(或在此留言):
http://mybatis.github.io/mybatis-3/zh/sqlmap-xml.html
本節(jié)源碼請看官方Git:
以上所述是小編給大家介紹的Mybatis 入門示例代碼之 Association,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
升級(jí)dubbo2.7.4.1版本平滑遷移到注冊中心nacos
這篇文章主要為大家介紹了2.7.4.1的dubbo平滑遷移到注冊中心nacos的兩種版本升級(jí)方案,以及為什要升級(jí),有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02
解析如何用兩個(gè)棧來實(shí)現(xiàn)隊(duì)列的方法
本篇文章是對如何用兩個(gè)棧實(shí)現(xiàn)隊(duì)列的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Spring整合quartz做定時(shí)任務(wù)的示例代碼
這篇文章主要介紹了在spring項(xiàng)目使用quartz做定時(shí)任務(wù),首先我這里的項(xiàng)目已經(jīng)是一個(gè)可以跑起來的完整項(xiàng)目,web.xml里面的配置我就不貼出來了,具體實(shí)例代碼跟隨小編一起看看吧2022-01-01
Java并發(fā)應(yīng)用之任務(wù)執(zhí)行分析
這篇文章主要為大家詳細(xì)介紹了JavaJava并發(fā)應(yīng)用編程中任務(wù)執(zhí)行分析的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-07-07
Java 關(guān)于時(shí)間復(fù)雜度和空間復(fù)雜度的深度刨析
算法復(fù)雜度分為時(shí)間復(fù)雜度和空間復(fù)雜度。其作用: 時(shí)間復(fù)雜度是度量算法執(zhí)行的時(shí)間長短;而空間復(fù)雜度是度量算法所需存儲(chǔ)空間的大小2021-11-11
java hasNextInt判斷是否為數(shù)字的方法
今天小編就為大家分享一篇java hasNextInt判斷是否為數(shù)字的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
SpringBoot自定義注解使用讀寫分離Mysql數(shù)據(jù)庫的實(shí)例教程
這篇文章主要給大家介紹了關(guān)于SpringBoot自定義注解使用讀寫分離Mysql數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

