mybatis关联查询有两个关键词,association是用于一对一和多对一,而collection是用于一对多的关系。本文主要介绍collection的用法。
首先看一下页面要求的数据的格式:
//获取端子信息
List<Map<String, Object>> portList = doneTaskDao.queryTroubleTaskPort(map); <resultMap id="troubleTaskPortMap" type="Map" > <result column="PORT_ID" property="PORT_ID"/> <result column="PORT_NO" property="PORT_NO"/> <result column="EQP_NO" property="EQP_NO"/> <result column="EQP_NAME" property="EQP_NAME"/> <result column="GLBM" property="GLBM"/> <result column="GLMC" property="GLMC"/> <result column="TASK_ID" property="TASK_ID"/> <collection property="portCheckedList" column="{TASK_ID=TASK_ID,PORT_ID=PORT_ID}" select="getPortCheckedList"></collection> </resultMap>
<select id="queryTroubleTaskPort" parameterType="map" resultMap="troubleTaskPortMap"> select distinct r.port_id, r.port_no, r.eqp_no, r.eqp_name, td.glbm, td.glmc, r.task_id from tb_cablecheck_record r, tb_cablecheck_taskdetail td where r.detail_id = td.detail_id and td.inspect_object_type = 1 and r.task_id = #{TASK_ID} order by to_number(regexp_substr(r.port_no, '[0-9]*[0-9]', 1)), to_number(regexp_substr(r.port_no, '[0-9]*[0-9]', 3)) </select>
<select id="getPortCheckedList" parameterType="map" resultType="map"> select r.record_id, r.record_type, r.descript, (select bs.staff_name from TB_BASE_STAFF bs where bs.staff_id=r.create_staff) create_staff, r.create_time, decode(r.ischeckok, 0, '合格', '不合格') ischeckok, p.photo_path, p.micro_photo_path from TB_CABLECHECK_RECORD r left join TB_CABLECHECK_PHOTO_REL pr on r.record_id=pr.object_id left join tb_cablecheck_photo p on p.photo_id=pr.photo_id where r.port_id = #{PORT_ID} and r.task_id = #{TASK_ID} </select>