天天看點

Mybatis 示例之 Association

接下來的文章中,關于mybatis的示例,全部來自于mybatis代碼中的單元測試代碼,通過這些代碼能夠學習mybatis中很有用的知識,這些内容在doc文檔中可能隻是簡單提到了,或者有一些文字說明,通過這些單元測試能更直覺的了解如何在mybatis使用這些内容。

這一節内容為association關聯的結果查詢,就是在查詢出結果後,根據查詢的列和resultmap定義的對應關系,來建立對象并寫入值。

association – 一個複雜的類型關聯;許多結果将包成這種類型

嵌入結果映射 – 結果映射自身的關聯,或者參考一個

(注:“參考一個”,這裡參考一個是通過對象的key來唯一确定的,如果key值一樣,就直接用已經存在的這個對象。)

association是resultmap中的一個配置選項,下面是用到的類的uml圖:

Mybatis 示例之 Association

car對象中包含了engine和brakes兩個對象。mapper是接口對象。associationtest是該測試對象。

sql表結構和資料:

[sql] view

plain copy

Mybatis 示例之 Association
Mybatis 示例之 Association

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檔案:

[html] view

Mybatis 示例之 Association
Mybatis 示例之 Association

<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 type="org.apache.ibatis.submitted.associationtest.brakes" id="brakesresult">  

        <result column="brakestype" property="type"/>  

    <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 id="getcars2" resultmap="carresult">  

    select 1 as carid, cartype, enginetype, enginecylinders, brakestype from cars where carid in (1,2)  

</mapper>  

其中的一個測試用例:

[java] view

Mybatis 示例之 Association
Mybatis 示例之 Association

@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傳回值:

Mybatis 示例之 Association

association是嵌套查詢中最簡單的一種情況,像上述例子中,一般我們都會用一個car對面包含所有的屬性,這裡的例子使用了嵌套對象,使對像的結構更鮮明。不過一般情況下很少會拆分一個對象為多個,用的多的時候是多表查詢的嵌套。

上面xml中的

carresult和engieresult,brakesresult都是分别定義,carresult引用了另外兩個resultmap。

對于不需要重用嵌套對象的情況,還可以直接這麼寫,把上面的xml修改後:

Mybatis 示例之 Association
Mybatis 示例之 Association

<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">  

    </association>  

    <association property="brakes" resultmap="brakesresult"/>  

</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