Mybatis一对多关联查询,返回结果集list中嵌套list实例(collection实现)

时间:2025-03-19 10:45:50
<select id="selectListByMercId" resultType="...MercSettlePojo"> <![CDATA[ select , t.settle_name as settleName <!-- 注意一定要让别名与pojo中的属性一致 --> from Settle t ]]> <where> merc_id = #{merc_id} </where> </select> <select id="selectMerchantList" parameterType="...MercPojo" resultMap="baseResultMap"> <![CDATA[ select ,m.merc_num,m.merc_name ... from Merchant m ]]> <where> <if test="mercNum != null and mercNum != ''"><![CDATA[and m.merc_num LIKE '%'||#{mercNum}||'%']]></if> <if test="mercName != null and mercName != ''"><![CDATA[and m.merc_name LIKE '%'||#{mercName}||'%']]></if> ... </where> </select> <resultMap id="baseResultMap" type="...MercPojo"> <result column="id" property="id" /> <result column="merc_num" property="mercNum" /> <result column="merc_name" property="mercName" /> ... <!-- 下面这句很重要:作用就是通过selectListByMercId查出list然后映射到属性mercSettleList中。其中,ofType为list的泛型,column为子select的入参(这里是id),select为子查询的mapperId--> <collection property="mercSettleList" ofType="...MercSettlePojo" column="{merc_id=id}" select="selectListByMercId"/> </resultMap>