基于SpringBoot+Mybatis Plus实现的树型结构

数据库设计

POJO

package com.example.demo.pojo;

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;

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

/**
 * @author yjw
 * @date 2020/7/28
 */
@Data
public class Tree {

    private Long id;

    private String name;

    @TableField(value = "parent_id")
    private Long pid;

    private Integer level;

    @TableField(exist = false)
    private List<Tree> children = new ArrayList<>();
}

Dao

package com.example.demo.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.pojo.Tree;

/**
 * @author yjw
 * @date 2020/7/28
 */
public interface TreeDao extends BaseMapper<Tree> {
}

Service

package com.example.demo.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.pojo.Tree;

/**
 * @author yjw
 * @date 2020/7/28
 */
public interface TreeService extends IService<Tree> {
}

Service实现

package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dao.TreeDao;
import com.example.demo.pojo.Tree;
import com.example.demo.service.TreeService;
import org.springframework.stereotype.Service;

/**
 * @author yjw
 * @date 2020/7/28
 */
@Service
public class TreeServiceImpl extends ServiceImpl<TreeDao, Tree> implements TreeService {
}

工具类

package com.example.demo.util;

import com.example.demo.pojo.Tree;

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

/**
 * @author yjw
 * @date 2020/7/28
 */
public class NodeUtil {

    private static final Integer LEVEL_1 = 1;

    /**
     * 获取目录根节点
     *
     * @return
     */
    public static List<Tree> getRootNode(List<Tree> nodeList) {
        List<Tree> list = new ArrayList<>();
        for (Tree t : nodeList) {
            if (LEVEL_1.equals(t.getLevel())) {
                list.add(t);
            }
        }
        return list;
    }

    /**
     * 根据pid查询子节点
     *
     * @param nodeList
     * @return
     */
    public static List<Tree> getChilNode(List<Tree> nodeList, Tree t) {
        //  为每一个树节点都创建一个Children
        t.setChildren(new ArrayList<>());
        //  这里获取是为了放入数据
        List<Tree> list = t.getChildren();
        for (Tree tree : nodeList) {
            //  判断当前的父id是否等于父节点id
            if (t.getId().equals(tree.getPid())) {
                //  是的话传入父类的子集合中
                list.add(tree);
                //  表示节点为该父id
                getChilNode(nodeList, tree);
            }
        }
        //  这个返回其实只有返回总树结构有用
        return list;
    }
}

测试

package com.example.demo.util;

import com.example.demo.pojo.Tree;

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

/**
 * @author yjw
 * @date 2020/7/28
 */
public class NodeUtil {

    private static final Integer LEVEL_1 = 1;

    /**
     * 获取目录根节点
     *
     * @return
     */
    public static List<Tree> getRootNode(List<Tree> nodeList) {
        List<Tree> list = new ArrayList<>();
        for (Tree t : nodeList) {
            if (LEVEL_1.equals(t.getLevel())) {
                list.add(t);
            }
        }
        return list;
    }

    /**
     * 根据pid查询子节点
     *
     * @param nodeList
     * @return
     */
    public static List<Tree> getChilNode(List<Tree> nodeList, Tree t) {
        //  为每一个树节点都创建一个Children
        t.setChildren(new ArrayList<>());
        //  这里获取是为了放入数据
        List<Tree> list = t.getChildren();
        for (Tree tree : nodeList) {
            //  判断当前的父id是否等于父节点id
            if (t.getId().equals(tree.getPid())) {
                //  是的话传入父类的子集合中
                list.add(tree);
                //  表示节点为该父id
                getChilNode(nodeList, tree);
            }
        }
        //  这个返回其实只有返回总树结构有用
        return list;
    }
}

结果

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值