關聯的結果查詢

2022-06-29 02:21:14 字數 3832 閱讀 9044

association:用於物件間包含關係對映

方式一:通過association標籤來封裝結果集

<

namespace

>

<

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

>

>

查詢:

<

select id="getempanddept" resultmap="complexemp2">

select e.id id,e.last_name last_name,e.email email,e.gender gender,e.d_id d_id,d.id did,d.dept_name dept_name from tb1_emplyee e,tb1_dept d where e.d_id=d.id and e.id=

#select

>

方式二:通過association標籤實現分段查詢

<

resultmap

id="myempbystep"

type

="com.test.beans.employee"

>

<

id column

="id"

property

="id"

/>

<

result

column

="last_name"

property

="lastname"

/>

<

result

column

="email"

property

="email"

/>

<

result

column

="gender"

property

="gender"

/>

<

association

property

="dept"

select

column

="d_id"

>

association

>

resultmap

>

對應的department對映檔案:

<

namespace

>

<

select

id="getdeptbyid"

resulttype

="com.test.beans.department"

>

select id,dept_name departmentname from tb1_dept where id=#

select

>

>

查詢:

<

select

id="getempbyidstep"

resultmap

="myempbystep"

>

select * from tb1_emplyee where id=#

select

>

association標籤還可以實現懶載入的功能

什麼是懶載入呢?

前面的分步查詢,每查詢一次都會執行兩次sql(一次查詢員工,一次查詢部門)

有時候我們並不需要插敘部門的情況,所以懶查詢就可以幫我們解決這個問題,提高效率,減少資源的占用

懶載入的實現也非常簡單,只要在全域性配置檔案中新增如下的配置**即可:

<

settings

>

<

setting

name

="lazyloadingenabled"

value

="true"

/>

<

setting

name

="aggressivelazyloading"

value

="false"

/>

settings

>

mysql得到查詢結果的同時統計查詢結果的數目

做一些資料庫查詢,不僅希望得到要查詢的結果,還希望方便地統計一下查詢結果中有多少條記錄。我通常的做法是 q select from fromtable where where limit start,pagesize r mysql query q q select count from fromt...

mysql關聯子查詢 MySQL 關聯子查詢

mysql 關聯子查詢 關聯子查詢是指乙個包含對錶的引用的子查詢,該錶也顯示在外部查詢中。通俗一點來講,就是子查詢引用到了主查詢的資料資料。以乙個實際的例子來理解關聯子查詢 article 文章表 aidtitlecontentuid 文章1文章1正文內容.文章2文章2正文內容.文章3文章3正文內容...

關聯表查詢

1 imemoryrelationshipclassfactory fac mapcontext.createobject esrigeodatabase.memoryrelationshipclassfactory as imemoryrelationshipclassfactory irelat...