一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Mybatis 入門示例代碼之 Association

Mybatis 入門示例代碼之 Association

2020-08-20 11:30isea533 Java教程

這篇文章主要介紹了Mybatis 入門示例代碼之 Association,需要的的朋友參考下

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

這一節內容為Association關聯的結果查詢,就是在查詢出結果后,根據查詢的列和resultMap定義的對應關系,來創建對象并寫入值。

  • association – 一個復雜的類型關聯;許多結果將包成這種類型
  • 嵌入結果映射 – 結果映射自身的關聯,或者參考一個

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

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

Mybatis 入門示例代碼之 Association

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', 4null);
insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(2, 'Opel'nullnull, '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返回值:

Mybatis 入門示例代碼之 Association

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:

https://github.com/mybatis/mybatis-3/tree/master/src/test/java/org/apache/ibatis/submitted/associationtest

以上所述是小編給大家介紹的Mybatis 入門示例代碼之 Association,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:http://blog.csdn.net/isea533/article/details/20868189

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产成人yy精品1024在线 | 国产在线视频第一页 | 放荡警察巨r麻麻出轨小说 范冰冰特黄xx大片 饭冈加奈子在线播放观看 法国老妇性xx在线播放 | 国产一区二区三区四区波多野结衣 | 91尤物在线视频 | 四虎永久免费地址在线网站 | 2020国产精品永久在线观看 | 996免费视频国产在线播放 | 和日本免费不卡在线v | 91精品综合久久久久久五月天 | 国产精品亚洲综合第一区 | 欧美另类变态 | 欧美一级在线视频 | 免费看国产一级片 | 数学老师扒开腿让我爽快 | 久久精品AV一区二区无码 | 亚洲视频中文字幕 | 太大了轻点阿受不了小说h 四色6677最新永久网站 | 免费一级欧美大片在线观看 | 性xxxx中国 | 免费观看视频网站 | 日本美女视频韩国视频网站免费 | 国产精品视频人人做人人爱 | 久久黄色小视频 | 96免费精品视频在线 | 亚洲一区二区三区久久精品 | 乌克兰成人性色生活片 | kayden kross喷水 | 精品视频在线观看免费 | 国产永久免费视频m3u8 | 男人网站视频 | 十六以下岁女子毛片免费 | 麻豆天美精东果冻传媒在线 | 美女的隐私无遮挡的网页 | 青草香蕉精品视频在线观看 | 亚洲女人国产香蕉久久精品 | 四虎影视网站 | 美国美女hd18 | 99久久精品国产综合一区 | 国产综合视频在线 | 天堂精品高清1区2区3区 |