商品分类无限层级递归

商品分类递归
注意点:使用递归实现商品分类。一定需要控制循环跳出,我这里是用list是否为null控制,不然会发生死循环,造成内存溢出
/**
* 递归所有分类,组成列表
* @return
*/
@Override
public List selectProductCategoryList() {
//查询父类
List list = productCategoryMapper.selectLevelList(0);
if(list != null && !list.isEmpty()){
List modelList = ProductCategoryModel.convertFromBoToModel(list);
for(ProductCategoryModel model:modelList){
model.setChildren(findChildCategory(model.getCategoryId()));
}
return modelList;
}
return null;
}

/** * 递归算法,根据父节点算出子类 * @return */ private List findChildCategory(Integer categoryId){ //根据父类id查询子节点 List boList = productCategoryMapper.selectParentList(categoryId); //中间bo转换成model List【商品分类无限层级递归】 modelList = ProductCategoryModel.convertFromBoToModel(boList); if(modelList != null && !modelList.isEmpty()) { for (ProductCategoryModel model : modelList) { model.setChildren(findChildCategory(model.getCategoryId())); } } return modelList; }

    推荐阅读