需求是一個pid下面會帶多個子id,前端傳一組pid集合,返回根據pid分組的所有子id及子id下的信息
<select id="listBvsCustByParentMemberIds" resultMap="sellerCodeMap"> select b.id as custMemberId, bvs.cust_code, mb.id as parentMemberId from member_base mb inner join bvs_parent_customer_info bvs on mb.seller_code = bvs.parent_code left join member_base b on b.seller_code = bvs.cust_code where bvs.parent_code != bvs.cust_code and mb.id in <foreach collection="memberIds" item="memberId" open="(" separator="," close=")"> #{memberId} </foreach> </select> <resultMap id="sellerCodeMap" type="com.yilihuo.cloud.service.user.dto.api.membercompany.ListBvsCustByParentMemberIdsResultDto"> <result property="parentMemberId" column="parentMemberId"/> <collection property="childMemberInfos" ofType="com.yilihuo.cloud.service.user.dto.api.membercompany.ChildMemberInfoDto"> <result property="custMemberId" column="custMemberId"></result> <result property="sellerCode" column="cust_code"></result> </collection> </resultMap>
通過<collection>標簽,將parentMemberId 相同的記錄存到ChildMemberInfoDto 里。然后返回,不需要其它邏輯.在一個就是需要
resultMap="sellerCodeMap"> 這個標簽,將數據庫查詢的記錄與出參返回值進行一個映射。
出參樣式