递归查找子菜单
package com.zzc.product.service.impl; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.zzc.common.utils.PageUtils; import com.zzc.common.utils.Query; import com.zzc.product.dao.CategoryDao; import com.zzc.product.entity.CategoryEntity; import com.zzc.product.service.CategoryService; @Service("categoryService") public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService { @Override public PageUtils queryPage(Map<String, Object> params) { IPage<CategoryEntity> page = this.page( new Query<CategoryEntity>().getPage(params), new QueryWrapper<CategoryEntity>() ); return new PageUtils(page); } @Override public List<CategoryEntity> listTree() { //先查询所有 List<CategoryEntity> categoryAll = baseMapper.selectList(null); //在树形展示 //能确定的就是一级菜单的parent_cid=0 List<CategoryEntity> list1 =null; //stream流式操作 list1 = categoryAll.stream().filter(category -> category.getParentCid() == 0).collect(Collectors.toList()); //二级菜单 list1.stream().map((menu)->{ menu.setChildren(getChildren(menu,categoryAll)); return menu; }).collect(Collectors.toList());; return list1; } @Override public void removeMuneByIds(List<Long> asList) { baseMapper.deleteBatchIds(asList); } @Override public Long[] findCatelogPath(Long catelogId) { CategoryEntity categoryEntity = baseMapper.selectById(catelogId); List<Long> path = new ArrayList<>(); path.add(categoryEntity.getCatId()); while (categoryEntity.getParentCid() != 0){ path.add(categoryEntity.getParentCid()); categoryEntity = baseMapper.selectById(categoryEntity.getParentCid()); } Collections.reverse(path); return path.toArray(new Long[path.size()]); } /** * 递归查找子菜单 * @return */ private List<CategoryEntity> getChildren(CategoryEntity curCategory,List<CategoryEntity> allCategory){ List<CategoryEntity> collect = allCategory.stream().filter((menu)-> { return menu.getParentCid().equals(curCategory.getCatId()); }).map((menu)-> { menu.setChildren(getChildren(menu,allCategory)); return menu; }).collect(Collectors.toList()); return collect; } }