接下來的文章中,關于Mybatis的示例,全部來自于Mybatis代碼中的單元測試代碼,通過這些代碼能夠學習Mybatis中很有用的知識,這些內容在doc文檔中可能只是簡單提到了,或者有一些文字說明,通過這些單元測試能更直觀的了解如何在Mybatis使用這些內容。
這一節內容為Association關聯的結果查詢,就是在查詢出結果后,根據查詢的列和resultMap定義的對應關系,來創建對象并寫入值。
- association – 一個復雜的類型關聯;許多結果將包成這種類型
- 嵌入結果映射 – 結果映射自身的關聯,或者參考一個
(注:“參考一個”,這里參考一個是通過對象的Key來唯一確定的,如果Key值一樣,就直接用已經存在的這個對象。)
association是resultMap中的一個配置選項,下面是用到的類的UML圖:
Car對象中包含了Engine和Brakes兩個對象。Mapper是接口對象。AssociationTest是該測試對象。
SQL表結構和數據:
1
2
3
4
5
6
7
8
9
10
11
12
|
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文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<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> |
其中的一個測試用例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@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修改后:
1
2
3
4
5
6
7
8
9
|
<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,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/isea533/article/details/20868189