需求是一个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"> 这个标签,将数据库查询的记录与出参返回值进行一个映射。
出参样式