實際項目中的,接口對外VO 會出現 一對一 和 一對多的狀況,舉例:小區 下面有 樓棟 ,樓棟 下面有 房屋 , 房屋裏面又房間sql
小區Vo : districtVo {mybatis
id;ui
name;blog
List<buildVo> builds接口
}get
樓棟Vo :buildVo{io
id;List
name;select
did;jdbc
List<apartmentVo> apartments
}
房屋Vo :apartmentVo{
id;
name;
List<RoomVo>
} ..........
對外接口中 有個接口查詢全部得小區 列表
推薦方法
<select id = "getList" resultMap = "resultList">
select id , name
fomr
<select >
<resultMap id="resultList" type="****.districtVo">
<collection property="builds" column="{id=id}" select="getbuids"></collection> //此處collection 集合方式 column 必須對應getlist 查出列 ,mybatis 總動從sql 語句中拿出對應列匹配column 傳遞到下一個select 語句中此處的select語句也能夠寫在collection中
</resultMap>
<select id="getbuids" resultType="****.buildVo">
select
d.id as id,
d.name as name,
from t_build d
where d.did = #{id,jdbcType=VARCHAR}
</select>
一對多的一種寫法
若是想嵌套三層 在 查出 distict list 中 的buid 裏面能有 apartment的信息 在 getbuids 中 再定義一個resultMap 而後再嵌套一次。 mybatis 一對多狀況仍是比較好理解 ,瞭解清楚 <collection></collection> 中每一個標籤的意思
第二種映射關係 此處儘可能返回本身vo ,返回map 類型 有些列數據爲空的時候 map 返回直接沒有該屬性