MyBatis:ResultMap的繼承

當數據實體具有一對多,或多對多的關係時,如果需要分別編寫級聯獲取,非級聯獲取的接口,爲了避免定義多了ResultMap,可以使用ResultMap的extends屬性來優化。

優化前:

<resultMap type="com.bean.Topology" id="resultMapTopology">
    <id column="topology_pk" property="topology_pk" javaType="java.lang.Long"/>	
    <result column="topology_id" property="topology_id" javaType="java.lang.String"/>
</resultMap>

<resultMap type="com.bean.Topology" id="resultMapTopologyOnConnection">
    <id column="topology_pk" property="topology_pk" javaType="java.lang.Long"/>	
    <result column="topology_id" property="topology_id" javaType="java.lang.String"/>
		
    <collection 
        property="topology_hostlist" 
        column="topology_pk"             
        select="com.mapper.HostMapper.getHostListByTopology_Pk" />
</resultMap>

<!-- 獲取拓撲的全量數據 (非級聯獲取,不傳參) -->
<select id="getTopologyList" resultMap="resultMapTopology">
	select * from odl_table_topology
</select>
	
<!-- 獲取拓撲的全量數據 (級聯獲取,不傳參)  -->
<select id="getTopologyListOnConnection" resultMap="resultMapTopologyOnConnection">
	select * from odl_table_topology
</select>

優化後:

<resultMap type="com.bean.Topology" id="resultMapTopology">
    <id column="topology_pk" property="topology_pk" javaType="java.lang.Long"/>	
    <result column="topology_id" property="topology_id" javaType="java.lang.String"/>
</resultMap>

<resultMap type="com.bean.Topology" id="resultMapTopologyOnConnection" extends="resultMapTopology">
    <collection 
        property="topology_hostlist" 
        column="topology_pk"             
        select="com.mapper.HostMapper.getHostListByTopology_Pk" />
</resultMap>

<!-- 獲取拓撲的全量數據 (非級聯獲取,不傳參) -->
<select id="getTopologyList" resultMap="resultMapTopology">
	select * from odl_table_topology
</select>
	
<!-- 獲取拓撲的全量數據 (級聯獲取,不傳參)  -->
<select id="getTopologyListOnConnection" resultMap="resultMapTopologyOnConnection">
	select * from odl_table_topology
</select>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章