Java递归全查询组织部门层级关系(SpringBoot版本)

目录

    • 1. 效果图
    • 2. 实体类
    • 3. 树形结构封装工具类(递归)
    • 4. Dao层
    • 5. Service层
    • 6. Controller层

1. 效果图

Java递归全查询组织部门层级关系(SpringBoot版本)_第1张图片
通过前面的封装完的Json,前端结合Layui做的效果图Java递归全查询组织部门层级关系(SpringBoot版本)_第2张图片

2. 实体类

/**
 * 组织部门实体类
 */
@Data
@ToString
public class Menu {
    //ID
    private String id;
    //机构名称
    private String name;
    //上级机构ID
    private String parentId;
    //其他属性省略,主要是这三个属性
}

3. 树形结构封装工具类(递归)

/**
 *树形结构封装类
 */
public class MenuTree {
    private static Map mapArray = new LinkedHashMap();
    private List menuCommon;
    private List list = new ArrayList();

    public List menuList(List menu){
        this.menuCommon = menu;
        for (Menu x : menu) {
            Map mapArr = new LinkedHashMap();
            if(x.getParentId().equals("0")){
                mapArr.put("id", x.getId());
                mapArr.put("title", x.getName());
                mapArr.put("spread",true);
                mapArr.put("parentId", x.getParentId());
                mapArr.put("children", menuChild(x.getId()));
                list.add(mapArr);
            }
        }
        return list;
    }


    private List menuChild(String id) {
        List lists = new ArrayList();
        for (Menu x : menuCommon) {
            Map childArray = new LinkedHashMap();
            if (x.getParentId().equals(id)) {
                childArray.put("id", x.getId());
                childArray.put("title", x.getName());
                childArray.put("spread",true);
                childArray.put("parentId", x.getParentId());
                childArray.put("children", menuChild(x.getId()));
                lists.add(childArray);
            }
        }
        return lists;
    }
}
 
  

4. Dao层

@Mapper
public interface TestDao {
    @Select("select id,parent_id parentId,name,phone,remark,active_flag activeFlag,create_user createUser,create_date createDate from t_menu")
    public List MenuList();
}

5. Service层

@Service
public class TestService {
    @Autowired
    private TestDao testDao;

    public List getMenuList(){
        //一步到位
        return new MenuTree().menuList(testDao.MenuList());
    }
}
 
  

6. Controller层

@Controller
public class TestController {
    @Autowired
    private TestService testService;

    @RequestMapping("/getMenuList")
    @ResponseBody
    public String getMenuList(){
        return JSON.toJSONString(testService.getMenuList());
    }
}

你可能感兴趣的:(Java递归全查询组织部门层级关系(SpringBoot版本))