MyBatis:ResultMap的继承

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29229567/article/details/84785420

当数据实体具有一对多,或多对多的关系时,如果需要分别编写级联获取,非级联获取的接口,为了避免定义多了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>

猜你喜欢

转载自blog.csdn.net/qq_29229567/article/details/84785420
今日推荐