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>

 

©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页