树形菜单查询

实体类

@Document(collection = "api_menu")
public class ApiMenu implements Serializable {

    private static final long serialVersionUID = -5754220183776638354L;

    @Id
    private Long id;//接口菜单id,自增

    private String title;//菜单名称

    private Long parentId;//父id

    private String method;//请求方法

    private String path;//请求地址

    private Map<String,List<ParmsItem>> reqParmsMap;//请求参数

    private Map<String,String> resParmsMap;//返回参数

    private String menuType;//菜单类型 M:菜单 I:接口

    private Integer apiStatus;//接口状态 1:未完成 2:已完成

    private String desc;//接口描述|菜单描述

    private Integer sort;//显示顺序

    private String addUser;//添加人

    private Date addTime;//添加时间

    private String upUser;//更新人

    private Date upTime;//更新时间

    @Transient
    private List<ApiMenu> chirdren;
}

controller

/**
 * 查询树形菜单
 * @return
 */
@GetMapping("/listWithTree")
public List<ApiMenu> list() {
    List<ApiMenu> listWithTree= mongoDbService.listWithTree();
    return listWithTree;
}

实现层

/**
  * 查询树形菜单
  * @return
  */
 public List<ApiMenu> listWithTree() {
     //1.查出所有分类
     List<ApiMenu> apiMenulist = mongoTemplate.findAll(ApiMenu.class);

     //2.构成父子的树形结构
     List<ApiMenu> level1Menus = apiMenulist.stream().filter(apiMenu ->
             //查询所有的一级分类
             apiMenu.getParentId() == 0
     ).map(menu ->{
         //查询所有的一级分类下的子菜单
         menu.setChirdren(getChildrens(menu,apiMenulist));
         return menu;
     }).sorted((menu1,menu2)->{
         //排序
         return (menu1.getSort()==null ? 0: menu1.getSort()) - (menu2.getSort()==null ? 0: menu2.getSort());
     }).collect(Collectors.toList());

     return level1Menus;
 }

 /**
  * 递归查询所有菜单的子菜单
  *
  * @param root  一级分类菜单
  * @param all   所有菜单
  * @return
  */
 private List<ApiMenu> getChildrens(ApiMenu root, List<ApiMenu> all) {
     List<ApiMenu> collect = all.stream().filter(categoryEntity -> {
         return categoryEntity.getParentId() == root.getId();
     }).map(categoryEntity -> {
         categoryEntity.setChirdren(getChildrens(categoryEntity, all));
         return categoryEntity;
     }).sorted((menu1, menu2) -> {
         return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
     }).collect(Collectors.toList());
     return collect;
 }

优化写法

//1.查出所有分类
  List<BackendCategory> list = backendCategoryDao.getAll();
  //实体转vo
  List<BackendCategoryTreeVO> backendCategoryTreeVO = list.stream().map((BackendCategory entity) -> {
      BackendCategoryTreeVO vo = new BackendCategoryTreeVO();
      vo.setId(entity.getId());
      vo.setParentId(entity.getParentId());
      vo.setTitle(entity.getTitle());
      vo.setLevel(entity.getLevel());
      vo.setDescription(entity.getDescription());
      vo.setSort(entity.getSort());
      vo.setStatus(entity.getStatus());
      return vo;

  }).collect(Collectors.toList());
  Map<Long,List<BackendCategoryTreeVO>> childrenMap = backendCategoryTreeVO.stream().collect(Collectors.groupingBy(BackendCategoryTreeVO::getParentId));
  List<BackendCategoryTreeVO> parentCategoryTreeVo = backendCategoryTreeVO.stream().filter(e -> childrenMap.containsKey(e.getId()) || e.getParentId().equals(NumberUtils.LONG_ZERO)).collect(Collectors.toList());
  for(BackendCategoryTreeVO vo: parentCategoryTreeVo){
      vo.setChildren(childrenMap.get(vo.getId()));
  }
  return parentCategoryTreeVo.stream().filter(c->c.parentId.equals(NumberUtils.LONG_ZERO)).collect(Collectors.toList());

实体

public class BackendCategoryTreeVO implements Serializable {


    @Schema(description = "父ID,没有填0")
    public Long parentId;
    @Schema(description = "类目id")
    private Long id;
    @Schema(description = "类目名称")
    private String title;

    @Schema(description = "层级")
    private Short level;

    @Schema(description = "描述")
    private String description;

    @Schema(description = "排序")
    private Integer sort;

    @Schema(description = "状态:1-启用、2-禁用")
    private Short status;

    @Schema(description = "子级属性")
    private List<BackendCategoryTreeVO> children;

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值