java 查询所有部门,然后循环递归部门层级

List<DeptsVo>  list  = categoryService.findDeptNameByLevel();

//一次性查出所有数据

Integer i=-200;
//定义第一级的树形菜单的数据的父级
List<DeptsVo>  list1=new ArrayList<DeptsVo>();
for (DeptsVo deptsVo : list) {
    if (deptsVo.getDeptLevel().intValue()==2){
        list1.add(deptsVo);
        i--;
        Category category=new Category();
        category.setDeptId(deptsVo.getId());
        category.setCategoryId(i);
        category.setCategoryName(deptsVo.getName());
        category.setCategoryType("metadataCustomType");
        category.setParentCategoryId(-1);
        categorys.add(category);
    }
}
//递归子集
setChildren(categorys,list,list1,i);

 

 

public void setChildren( List<Category> categorys, List<DeptsVo>  list, List<DeptsVo>  list1,Integer i){
    // list1.clear();
    List<DeptsVo>  list2=new ArrayList<DeptsVo>();
    for (DeptsVo deptsVo:list1){
        for (DeptsVo deptsVo1:list){
            if (deptsVo.getId().equals(deptsVo1.getPid())){
                list2.add(deptsVo1);
// 循环数组可以去掉已经用过的数据,考虑数据量小,没去
                i--;
                setCategory(categorys,deptsVo1,i);
            }
        }
    }
    if (list2!=null&&list2.size()>0){
        //递归子集
        setChildren(categorys,list,list2,i);
    }

}
public void setCategory( List<Category> categorys,DeptsVo deptsVo,Integer i){
    Category category=new Category();
    Integer id =deptsVo.getId();
    String name = deptsVo.getName();
    category.setDeptId(id);
    category.setCategoryId(i);
    category.setCategoryName(name);
    category.setCategoryType("metadataCustomType");
    for (Category category1:categorys ){
        if (category1.getDeptId()!=null&&category1.getDeptId().equals(deptsVo.getPid())){
            category.setParentCategoryId(category1.getCategoryId());
              
            break;
        }
    }
    categorys.add(category);
}

这里是将部门数据放入树形菜单了,CategoryId是树形菜单id,setParentCategoryId树形菜单父id,id用的不是部门id,pid是上级部门id,自己生成的id用来做数形菜单的id,用复数是因为正数都被数据库已有数据使用,是数据库自建目录数据结构加上系统组织架构部门结构做数据目录展现

 

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值