mybatis实现固定三层树形结构的嵌套查询
entity:
@Data public class CategoryVo {private Long categoryId; //当前分类idprivate String categoryName; //当前分类名private List<CategoryVo> categoryChild; //子分类 }
mybatis:
<resultMap id="CategoryTreeRM" type="com.atguigu.gmall.web.CategoryVo"><id property="categoryId" column="c1id"></id><result property="categoryName" column="c1name"></result><collection property="categoryChild" ofType="com.atguigu.gmall.web.CategoryVo"><id property="categoryId" column="c2id"></id><result property="categoryName" column="c2name"></result><collection property="categoryChild" ofType="com.atguigu.gmall.web.CategoryVo"><id property="categoryId" column="c3id"></id><result property="categoryName" column="c3name"></result></collection></collection> </resultMap> <select id="getCategorysTree" resultMap="CategoryTreeRM">select bc1.id c1id,bc1.name c1name,bc2.id c2id,bc2.name c2name,bc3.id c3id,bc3.name c3namefrom base_category1 bc1left join base_category2 bc2 on bc2.category1_id = bc1.idleft join base_category3 bc3 on bc3.category2_id = bc2.id </select>