Mybatis 入門示例代碼之 Association
接下來的文章中,關(guān)于Mybatis的示例,全部來自于Mybatis代碼中的單元測試代碼,通過這些代碼能夠?qū)W習(xí)Mybatis中很有用的知識,這些內(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 – 一個復(fù)雜的類型關(guān)聯(lián);許多結(jié)果將包成這種類型
- 嵌入結(jié)果映射 – 結(jié)果映射自身的關(guān)聯(lián),或者參考一個
(注:“參考一個”,這里參考一個是通過對象的Key來唯一確定的,如果Key值一樣,就直接用已經(jīng)存在的這個對象。)
association是resultMap中的一個配置選項,下面是用到的類的UML圖:
Car對象中包含了Engine和Brakes兩個對象。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>
其中的一個測試用例:
@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對面包含所有的屬性,這里的例子使用了嵌套對象,使對像的結(jié)構(gòu)更鮮明。不過一般情況下很少會拆分一個對象為多個,用的多的時候是多表查詢的嵌套。
上面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>
為了對比和區(qū)分,這里指修改了Engine,在association元素上增加了屬性javaType,元素內(nèi)增加了result映射。
如果有association方面問題可以參考(或在此留言):
http://mybatis.github.io/mybatis-3/zh/sqlmap-xml.html
本節(jié)源碼請看官方Git:
以上所述是小編給大家介紹的Mybatis 入門示例代碼之 Association,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Java 使用poi把數(shù)據(jù)庫中數(shù)據(jù)導(dǎo)入Excel的解決方法
本篇文章介紹了,Java 使用poi把數(shù)據(jù)庫中數(shù)據(jù)導(dǎo)入Excel的解決方法。需要的朋友參考下2013-05-05SpringBoot基于Swagger2構(gòu)建API文檔過程解析
這篇文章主要介紹了SpringBoot基于Swagger2構(gòu)建API文檔過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11關(guān)于Spring自定義XML schema 擴展的問題(Spring面試高頻題)
今天給大家分享一道spring高頻率面試題關(guān)于Spring自定義XML schema 擴展的問題,今天以spring整合dubbo的實例給大家詳細(xì)講解下,感興趣的朋友跟隨小編一起看看吧2021-05-05Java中final,finally,finalize?有什么區(qū)別
這篇文章主要給大家分享的是?Java中final,finally,finalize?到底有什么區(qū)別,文章圍繞final,finally,finalize的相關(guān)資料展開詳細(xì)內(nèi)容,具有一定的參考的價值,需要的朋友可以參考一下2021-11-11Java實現(xiàn)整合文件上傳到FastDFS的方法詳細(xì)
FastDFS是一個開源的輕量級分布式文件系統(tǒng),對文件進(jìn)行管理,功能包括:文件存儲、文件同步、文件上傳、文件下載等,解決了大容量存儲和負(fù)載均衡的問題。本文將提供Java將文件上傳至FastDFS的示例代碼,需要的參考一下2022-02-02Java?String源碼contains題解重復(fù)疊加字符串匹配
這篇文章主要為大家介紹了Java?String源碼contains題解重復(fù)疊加字符串匹配示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11