树形结构递归实现

树形结构递归实现

 /**
     * 获取所有子分类(不包括根节点)
     *
     * @param pid
     * @return
     */
    @Override
    public List getChilds(String pid) {
        List resourceTypeDTOs =new LinkedList<>();
        List typeEntities = resourceTypeDao.findByResourceTypePid(pid);
        if (typeEntities!=null&&typeEntities.size()>0){
            for (ResourceTypeEntity resourceTypeEntity:typeEntities){
                getChilds(resourceTypeEntity.getResourceTypeId());
                childTypes.add(resourceTypeEntity);
            }
            resourceTypeDTOs=ConvertUtil.convertList(new ResourceTypeDTO(), childTypes);
        }

        return resourceTypeDTOs;
    }

    /**
     * 获取所有子分类(sql实现)
     * SELECT * FROM res_type A START WITH A.res_type_id = 'service' CONNECT BY PRIOR A.res_type_id = A.res_type_pid ORDER BY A.res_type_pid;
     *
     * @param id
     * @param includeFlag
     * @return
     */
    @Override
    public List getChildsSql(String id, Integer includeFlag) {
        List resourceTypeDTOs=new ArrayList<>();
        List resourceTypeEntities = resourceTypeDao.findChilds(id, includeFlag);
        if (resourceTypeEntities!=null&&resourceTypeEntities.size()>0){
            resourceTypeDTOs= ConvertUtil.convertList(new ResourceTypeDTO(),resourceTypeEntities);
        }
        return resourceTypeDTOs;
    }

    /**
     * 获取所有子分类(包括根节点树形结构)
     *
     * @param
     * @return
     */
    @Override
    public ResourceTypeDTO getChildsTree(String id) {
        ResourceTypeEntity one = resourceTypeDao.findOne(id);
        ResourceTypeDTO resourceTypeDTO=new ResourceTypeDTO();
        ConvertUtil.convert(resourceTypeDTO,one);
        List typeEntities = resourceTypeDao.findByResourceTypePid(id);
        if (typeEntities!=null&&typeEntities.size()>0){
            for (ResourceTypeEntity resourceTypeEntity:typeEntities){
                ResourceTypeDTO typeDTO = getChildsTree(resourceTypeEntity.getResourceTypeId());
                List childs = resourceTypeDTO.getChilds();
                childs.add(typeDTO);
            }
            //resourceTypeDTOs=ConvertUtil.convertList(new ResourceTypeDTO(), childTypes);
        }

        return resourceTypeDTO;
    }

你可能感兴趣的:(java)