java 递归查询树形菜单结构(个人认为是最简单的)

/**
	 * 获取无限级的类别资源
	 */
	@Override
	public List findTypeTree() throws BusinessException {
		Map map = new HashMap<>();
		map.put("EQ_parentId",0);
		Map filters = SearchFilter.parse(map);
		List orderList = new ArrayList<>();
		Order order = new Order("sortNum");
		orderList.add(order);
		Sort sort = new Sort(orderList);
		//默认查询出所有的一级类别
		List typeList = (List) typeInfoRepository.findAll(DynamicSpecifications.bySearchFilter(filters.values(), TypeInfo.class),sort);
		List list = new ArrayList<>();
		for (TypeInfo typeInfo : typeList) {
			JSONObject treeObject = new JSONObject(true);
			treeObject.put("id", typeInfo.getTypeId());
			treeObject.put("text", typeInfo.getName());
			treeObject.put("children", getChildren(typeInfo.getTypeId()));
			list.add(treeObject);
		}
		return list;
	}
	public List getChildren(Long parentId){
		List list = new ArrayList<>();
		List children = typeInfoRepository.findTypeInfoByParentIdAndSortNum(parentId);
		for (TypeInfo typeInfo : children) {
			JSONObject obj = new JSONObject(true);
			obj.put("id", typeInfo.getTypeId());
			obj.put("text", typeInfo.getName());
			obj.put("children", getChildren(typeInfo.getTypeId()));
			list.add(obj);
		}
		return list;
	} 
  

 getChildren()方法传入一级类别的id 作为父id查询出其子类别,然后递归掉用getChildren()方法 实现树形菜单结构树

 

你可能感兴趣的:(java 递归查询树形菜单结构(个人认为是最简单的))