一.运用场景
项目中前台要展示一个树形菜单,后台服务器需要返回一个树状目录,包含各级子目录。
二.需求分析
首先我们需要有一个对象,里面不仅要包含自身目录信息,还要包含其子目录的信息。比如一级目录下的二级目录有哪些,二级目录下的三级目录有哪些....等等。因为是与前台展示相关,需要新建一个vo包,在Category目录表对应的pojo基础上新建一个CategoryVO类用于,该类比Category多了一个字段即List<CategoryVO> 用于存放子目录的列表。
三.代码编写
/**
* 前台展示商品列表Service层
* @return List<CategoryVO> 商品目录列表(包含各级子目录)
*/
@Override
@Cacheable(value = "listForCustom")
public List<CategoryVO> listForCustom(){
List<CategoryVO> categoryVOList = new ArrayList<>();
recursivelyFindCategories(categoryVOList,0);
return categoryVOList;
}
/**
* 描述:递归获取目录及各级子目录
*/
private void recursivelyFindCategories(List<CategoryVO> categoryVOList,Integer parentId){
//根据父id找出该父id下的子目录集合
List<Category> categoryList = categoryMapper.selectListByParentId(parentId);
//判断子目录是否为空,如果子目录为空说明该目录没有子目录,开始对同级的下一个目录进行递归查询
if(!CollectionUtils.isEmpty(categoryList)){
//循环遍历查到的子目录
for (int i = 0; i < categoryList.size(); i++) {
//每次取出一个子目录,用一个Category对象接收
Category category = categoryList.get(i);
//新建一个CategoryVO对象,用于接收Category对象的属性,此时Category对象没有List<CategoryVO>,即子目录列表属性
CategoryVO categoryVO = new CategoryVO();
//复制Category对象的属性给CategoryVO
BeanUtils.copyProperties(category,categoryVO);
//将复制后的CategoryVO对象添加到集合里
categoryVOList.add(categoryVO);
/*
categoryVO.getChildCategory()得到的是当前categoryVO的子目录列表属性,
此时该集合为空,目的是要作为下一级目录列表的容器,作为参数一传入,
将该子目录id作为下一次查询需要的父id即可根据该id继续查到下一级目录列表
放到该集合里,下一级目录列表重复该操作直到查完最后一级子目录为止。
*/
recursivelyFindCategories(categoryVO.getChildCategory(),categoryVO.getId());
}
}
}
四.实现效果
{
"status": 10000,
"msg": "SUCCESS",
"data": [
{
"id": 3,
"name": "新鲜水果",
"type": 1,
"parentId": 0,
"orderNum": 1,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2019-12-28T09:11:26.000+0000",
"childCategory": [
{
"id": 4,
"name": "橘子橙子",
"type": 2,
"parentId": 3,
"orderNum": 1,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2019-12-28T08:25:10.000+0000",
"childCategory": [
{
"id": 19,
"name": "果冻橙",
"type": 3,
"parentId": 4,
"orderNum": 1,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2020-02-10T16:37:02.000+0000",
"childCategory": []
}
]
},
{
"id": 11,
"name": "草莓",
"type": 2,
"parentId": 3,
"orderNum": 2,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2019-12-28T07:44:42.000+0000",
"childCategory": []
},
{
"id": 12,
"name": "奇异果",
"type": 2,
"parentId": 3,
"orderNum": 3,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2019-12-28T08:25:12.000+0000",
"childCategory": []
},
{
"id": 14,
"name": "车厘子",
"type": 2,
"parentId": 3,
"orderNum": 4,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2019-12-28T08:25:12.000+0000",
"childCategory": []
},
{
"id": 28,
"name": "其他水果",
"type": 2,
"parentId": 3,
"orderNum": 4,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2019-12-28T08:25:12.000+0000",
"childCategory": []
}
]
},
{
"id": 5,
"name": "海鲜水产",
"type": 1,
"parentId": 0,
"orderNum": 2,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2019-12-28T08:25:20.000+0000",
"childCategory": [
{
"id": 7,
"name": "螃蟹",
"type": 2,
"parentId": 5,
"orderNum": 1,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2019-12-28T08:25:15.000+0000",
"childCategory": []
},
{
"id": 8,
"name": "鱼类",
"type": 2,
"parentId": 5,
"orderNum": 2,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2019-12-28T08:25:16.000+0000",
"childCategory": []
},
{
"id": 13,
"name": "海参",
"type": 2,
"parentId": 5,
"orderNum": 3,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2019-12-28T08:25:17.000+0000",
"childCategory": []
}
]
},
{
"id": 6,
"name": "精选肉类",
"type": 1,
"parentId": 0,
"orderNum": 3,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2019-12-28T08:25:21.000+0000",
"childCategory": [
{
"id": 16,
"name": "牛羊肉",
"type": 2,
"parentId": 6,
"orderNum": 1,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2019-12-28T08:25:18.000+0000",
"childCategory": []
}
]
},
{
"id": 9,
"name": "冷饮冻食",
"type": 1,
"parentId": 0,
"orderNum": 4,
"createTime": "2019-12-20T05:45:28.000+0000",
"updateTime": "2019-12-28T08:25:22.000+0000",
"childCategory": [
{
"id": 17,
"name": "冰淇淋",
"type": 2,
"parentId": 9,
"orderNum": 1,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2019-12-28T08:25:18.000+0000",
"childCategory": []
}
]
},
{
"id": 10,
"name": "蔬菜蛋品",
"type": 1,
"parentId": 0,
"orderNum": 5,
"createTime": "2019-12-20T05:45:28.000+0000",
"updateTime": "2019-12-28T08:25:23.000+0000",
"childCategory": [
{
"id": 18,
"name": "蔬菜综合",
"type": 2,
"parentId": 10,
"orderNum": 1,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2020-02-10T16:48:27.000+0000",
"childCategory": []
}
]
},
{
"id": 27,
"name": "美味菌菇",
"type": 1,
"parentId": 0,
"orderNum": 7,
"createTime": "2019-12-20T05:45:28.000+0000",
"updateTime": "2020-02-10T15:20:36.000+0000",
"childCategory": [
{
"id": 15,
"name": "火锅食材",
"type": 2,
"parentId": 27,
"orderNum": 5,
"createTime": "2019-12-17T17:17:00.000+0000",
"updateTime": "2020-02-10T16:42:33.000+0000",
"childCategory": []
}
]
}
]
}