数据库设计
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;
}
}
结果