<!-- 这里的id的值作为下面的查询返回结果resultMap的值 -->
<!-- collection中的column属性可以为多个值,这里只有一个,它作为下面递归查询传递进去的参数 -->
<!-- ofType和javaType属性正好联合构成了数据Bean类Category中的childrenList属性的数据类型 -->
<!-- select的值为下面递归查询的select标签的id值 -->
<resultMap type="com.yuantiaokj.test.GoodsTasteFirstlevel" id="goodsTasteTree">
<id column="id" property="id"/>
<result column="storeId" property="storeId"/>
<result column="name" property="name"/>
<result column="goodsId" property="goodsId"/>
<collection property="goodsTasteSecondaries" ofType="com.yuantiaokj.test.GoodsTasteSecondary"
column="{id=id,goodsId=goodsId}" select="getGoodsTasteSecondaries"/>
</resultMap>
<!-- 先查询菜单根级目录 -->
<!-- 这里的返回结果必须为resultMap,并且值为上面构建的resultMap的id的值 -->
<!-- 根据菜品id获取一级菜品口味信息列表 -->
<select id="getGoodsTasteTree" parameterType="java.util.List" resultMap="goodsTasteTree">
SELECT DISTINCT GTFL.id,GTFL.storeId,GTFL.`name`, FLTI.`goodsId`
FROM `goods_taste_first_level` AS GTFL
LEFT JOIN `firstlevel_secondary_taste` AS FLTI
ON GTFL.`id` =FLTI.`firstLevelTasteId`
WHERE FLTI.`goodsId` in
<foreach item="goodId" collection="goodsIds" open="(" separator="," close=")">
#{goodId}
</foreach>
</select>
<!-- 再利用上次查询结果colliection中column的值cid做递归查询,查出所有子菜单 -->
<!-- 这里的返回结果必须为resultMap,并且值为上面构建的resultMap的id的值 -->
<!-- 根据菜品id和一级口味id获取二级菜品口味信息列表 -->
<select id="getGoodsTasteSecondaries" resultMap="goodsTasteTree">
SELECT DISTINCT GTS.id,GTS.storeId,GTS.firstLevelId,GTS.name,FST.`goodsId`
FROM `goods_taste_secondary` AS GTS
LEFT JOIN `firstlevel_secondary_taste` AS FST
ON GTS.id=FST.`secondaryTasteId`
WHERE FST.`goodsId`= #{goodsId} AND FST.`firstLevelTasteId` = #{id}
</select>
Mybatis实现树形数据查询,优化业务for循环
最新推荐文章于 2023-10-30 13:44:19 发布