MyBatis 本是 apache 的一个开源项目 iBatis, 2010 年这个项目由 apache software foundation 迁移到了 google code,并且改名为 MyBatis 。
这篇文章主要介绍了 Mybatis 入门示例代码之 Association,需要的的朋友参考下
接下来的文章中,关于 Mybatis 的示例,全部来自于 Mybatis 代码中的单元测试代码,通过这些代码能够学习 Mybatis 中很有用的知识,这些内容在 doc 文档中可能只是简单提到了,或者有一些文字说明,通过这些单元测试能更直观的了解如何在 Mybatis 使用这些内容。
这一节内容为 Association 关联的结果查询,就是在查询出结果后,根据查询的列和 resultMap 定义的对应关系,来创建对象并写入值。
(注:"参考一个",这里参考一个是通过对象的 Key 来唯一确定的,如果 Key 值一样,就直接用已经存在的这个对象。)
association 是 resultMap 中的一个配置选项,下面是用到的类的 UML 图:
Car 对象中包含了 Engine 和 Brakes 两个对象。Mapper 是接口对象。AssociationTest 是该测试对象。
SQL 表结构和数据:
- 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 文件:
- namespace="org.apache.ibatis.submitted.associationtest.Mapper">
- type="org.apache.ibatis.submitted.associationtest.Car" id="carResult">
- column="carid" property="id" />
- column="cartype" property="type" />
- property="engine" resultMap="engineResult" />
- property="brakes" resultMap="brakesResult" />
- type="org.apache.ibatis.submitted.associationtest.Engine" id="engineResult">
- column="enginetype" property="type" />
- column="enginecylinders" property="cylinders" />
- type="org.apache.ibatis.submitted.associationtest.Brakes" id="brakesResult">
- column="brakesType" property="type" />
- id="getCars" resultMap="carResult">
- select * from cars
- id="getCarsNonUnique" resultMap="carResult">
- select 1 as carid, cartype, enginetype, enginecylinders, brakestype from
- cars
- id="getCars2" resultMap="carResult">
- select 1 as carid, cartype, enginetype, enginecylinders, brakestype from
- cars where carid in (1,2)
其中的一个测试用例:
- @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 修改后:
- type="org.apache.ibatis.submitted.associationtest.Car" id="carResult">
- column="carid" property="id" />
- column="cartype" property="type" />
- property="engine" javaType="org.apache.ibatis.submitted.associationtest.Engine">
- column="enginetype" property="type" />
- column="enginecylinders" property="cylinders" />
- property="brakes" resultMap="brakesResult" />
为了对比和区分,这里指修改了 Engine, 在 association 元素上增加了属性 javaType,元素内增加了 result 映射。
如果有 association 方面问题可以参考(或在此留言):
http://mybatis.github.io/mybatis-3/zh/sqlmap-xml.html
本节源码请看官方 Git:
以上所述是小编给大家介绍的 Mybatis 入门示例代码之 Association,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 PHPERZ 网站的支持!
来源: http://www.phperz.com/article/17/1207/358225.html