实现3级分类效果如图
菜单管理数据库设计
CREATE TABLE `pms_category` ( `cat_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '分类id', `name` char(50) DEFAULT NULL COMMENT '分类名称', `parent_cid` bigint(20) DEFAULT NULL COMMENT '父分类id', `cat_level` int(11) DEFAULT NULL COMMENT '层级', `show_status` tinyint(4) DEFAULT NULL COMMENT '是否显示[0-不显示,1显示]', `sort` int(11) DEFAULT NULL COMMENT '排序', `icon` char(255) DEFAULT NULL COMMENT '图标地址', `product_unit` char(50) DEFAULT NULL COMMENT '计量单位', `product_count` int(11) DEFAULT NULL COMMENT '商品数量', PRIMARY KEY (`cat_id`) USING BTREE, KEY `parent_cid` (`parent_cid`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1437 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='商品三级分类';
菜单管理实体类
@Data
@TableName("pms_category")
public class CategoryEntity implements Serializable {
private static final long serialVersionUID = 1L;
@TableId
private Long catId;
private String name;
private Long parentCid;
private Integer catLevel;
private Integer showStatus;
private Integer sort;
private String icon;
private String productUnit;
private Integer productCount;
@TableField(exist = false)
private List children;
}
Controller层
@RestController
@RequestMapping("product/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@RequestMapping("/list/tree")
public R list(){
List entityList = categoryService.listwitTree();
return R.ok().put("data", entityList);
}
}
Service接口
public interface CategoryService extends IService{ //三级菜单分类树 List listwitTree(); }
Service接口实现类
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl implements CategoryService {
@Override
public List listwitTree() {
//1.查出所有分类
List entities = baseMapper.selectList(null);
//2.组装成父子树形结构
//2.1)、找到所有的一级分类
List level1Menus = entities.stream().filter((categoryEntity) -> {
return categoryEntity.getParentCid() == 0;
}).map((menu)->{
menu.setChildren(getchildren(menu,entities));
return menu;
}).sorted((menu1,menu2)->{
return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
}).collect(Collectors.toList());
return level1Menus;
}
//递归查找所有菜单的子菜单
private List getchildren(CategoryEntity root,List all){
List children = all.stream().filter((categoryEntity) -> {
return categoryEntity.getParentCid() == root.getCatId();
}).map((categoryEntity) -> {
//1.找到子菜单
categoryEntity.setChildren(getchildren(categoryEntity,all));
return categoryEntity;
}).sorted((menu1,menu2)->{
//2.菜单的排序
return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
}).collect(Collectors.toList());
return children;
}
}
返回成功的Json对象
{
"msg": "success",
"code": 0,
"data": [
{
"catId": 2,
"name": "手机",
"parentCid": 0,
"catLevel": 1,
"showStatus": 1,
"sort": 0,
"icon": null,
"productUnit": null,
"productCount": 0,
"children": [
{
"catId": 34,
"name": "手机通讯",
"parentCid": 2,
"catLevel": 2,
"showStatus": 1,
"sort": 0,
"icon": null,
"productUnit": null,
"productCount": 0,
"children": [
{
"catId": 225,
"name": "手机",
"parentCid": 34,
"catLevel": 3,
"showStatus": 1,
"sort": 0,
"icon": "",
"productUnit": null,
"productCount": 0,
"children": []
}
]
}
]
}
]
}



