递归查询树形结构

实体类

import lombok.AllArgsConstructor;
import lombok.Data;


@Data
@AllArgsConstructor
public class TroubleshootingLabel {
    /**
     * 
     */
    private Integer id;

    /**
     * 名称
     */
    private String title;

    /**
     * 父级id
     */
    private Integer superId;

    /**
     * 几级分类
     */
    private Integer type;

    省略set   get .......
}

树形结构

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TroubleshootingLabelTree
 * @Author qiuyahui
 * @Data 2019/4/26 0026 17:10
 * @Version 1.0
 **/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TroubleshootingLabelTree {
    private Integer id;

    /**
     * 名称
     */
    private String title;

    /**
     * 父级id
     */
    private Integer superId;

    /**
     * 几级分类
     */
    private Integer type;


    private List<TroubleshootingLabelTree> child = new ArrayList<>();

    /**
     * 类转换,基本属性复制
     * @param
     * @return
     */
    public static TroubleshootingLabelTree covert(TroubleshootingLabel troubleshootingLabel) {
        TroubleshootingLabelTree treeTargetDto = new TroubleshootingLabelTree();
        treeTargetDto.id = troubleshootingLabel.getId();
        treeTargetDto.title = troubleshootingLabel.getTitle();
        treeTargetDto.type = troubleshootingLabel.getType();
        treeTargetDto.superId=troubleshootingLabel.getSuperId();
        return treeTargetDto;
    }

   省略set   get .......
}

ServiceImpl

import com.zzu.modules.security.dao.TroubleshootingLabelMapper;
import com.zzu.modules.security.entity.TroubleshootingLabel;
import com.zzu.modules.security.entity.TroubleshootingLabelTree;
import com.zzu.modules.security.service.TroubleshootingLabelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @ClassName TroubleshootingLabelServiceImpl
 * @Author qiuyahui
 * @Data 2019/4/23 0023 14:44
 * @Version 1.0
 **/
@Service
public class TroubleshootingLabelServiceImpl implements TroubleshootingLabelService {
    @Resource
    private TroubleshootingLabelMapper troubleshootingLabelMapper;

    @Override
    public TroubleshootingLabelTree selectByPrimaryKey() {
        ArrayList<TroubleshootingLabel> list=troubleshootingLabelMapper.selectByPrimaryKey();
        TroubleshootingLabelTree tree = TroubleshootingLabelServiceImpl.getTree(list);
        return tree;
    }

    public static TroubleshootingLabelTree getTree(ArrayList<TroubleshootingLabel> list){
        HashMap<Integer, TroubleshootingLabelTree> map = new HashMap<>();
        //新建根节点
        TroubleshootingLabelTree rootNode = new TroubleshootingLabelTree();
        map.put(0, rootNode);
        for (TroubleshootingLabel treeSource : list) {
            TroubleshootingLabelTree childNode = TroubleshootingLabelTree.covert(treeSource);
            map.put(treeSource.getId(), childNode);
            //父节点id
            Integer pId = treeSource.getSuperId();
            //父节点
            TroubleshootingLabelTree parentNode = map.get(pId);
            //给父节点的child属性赋当前节点
            parentNode.getChild().add(childNode);
        }
        return  map.get(0);
    }
}

自己开发的出门必备小程序可以扫码体验交流交流

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值