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>

 

已标记关键词 清除标记
mybatis中返回resultmap,该map是一个javabean,这个Javabean同时继承了另外一个bean,怎么实现? 要求返回的既有子类中的属性,又有父类中的属性。 我是将父类封装成一个resultmap,子类也封装为一个resultmap,继承父类的resultmap。返回值是子类的resultmap。 其中写的sql语句一直报错“未明确定义列”。我已经试过用别名了。 子类的resultmap <resultMap id="result_host" extends="result_scan" type="com.cncert.satp.bean.HostScanResultBean"> <id column="host_result_id" property="host_result_id"/> <result column="host_ip" property="host_ip"/> <result column="host_port" property="host_port"/> <result column="vulnerability" property="vulnerability"/> <result column="tool_name" property="tool_name"/> <result column="tool_task_id" property="tool_task_id"/> <result column="result_id" property="result_id"/> <result column="gmt_create" property="gmt_create"/> <result column="gmt_modified" property="gmt_modified"/> </resultMap> 父类的resultmap <resultMap id="result_scan" type="com.cncert.satp.bean.ScanResultBean"> <id column="result_id" property="resultid"/> <result column="host_ip" property="resultsource"/> <result column="create_time" property="createdate"/> <result column="reliability" property="credibility"/> <association property="task" javaType="com.cncert.satp.bean.TaskBean"> <id column="task_id" property="taskid"></id> <result column="task_no" property="taskno"></result> <result column="task_resource" property="taskresource"></result> <result column="strategy_id" property="strategyid"></result> <result column="task_type" property="tasktype"></result> <result column="task_description" property="taskdescription"></result> <result column="task_status" property="taskstatus"></result> <result column="start_time" property="starttime"></result> <result column="finish_time" property="finishtime"></result> <result column="tool_name" property="toolname"></result> <result column="create_time" property="createtime"></result> <result column="gmt_create" property="gmt_create"></result> <result column="gmt_modified" property="gmt_modified"></result> </association> </resultMap> <sql id="result_sql"> result_id,task_id,reliability,result_status,create_time </sql> ``` <select id="getCodeResult" parameterType="int" resultMap="result_code"> select <include refid="result_sql"/>,source_result.* from source_result,scan_result where scan_result.result_id=source_result.result_id and scan_result.task_id=#{id} </select> ```
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页