天天看点

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