递归创建目录树,实现方法的一种有其他实现可以评论出来
domain层
public class Category {
private Long id;
private String name;
private Long parentId;
public Category(Long id, String name) {
this.id = id;
this.name = name;
}
public Category(Long id, String name, Long parentId) {
this.id = id;
this.name = name;
this.parentId = parentId;
}
public Category() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getParentId() {
return parentId;
}
public void setParentId(Long parentId) {
this.parentId = parentId;
}
}
dto
public class CategoryDto {
private Long id;
private String name;
private Long parentId;
private Set chilen;
public CategoryDto(Long id, String name, Long parentId, Set chilen) {
this.id = id;
this.name = name;
this.parentId = parentId;
this.chilen = chilen;
}
public CategoryDto(Long id, String name, Long parentId) {
this.id = id;
this.name = name;
this.parentId = parentId;
}
public CategoryDto(Long id, Long parentId, Set chilen) {
this.id = id;
this.parentId = parentId;
this.chilen = chilen;
}
public CategoryDto(Category category){
this.id = category.getId();
this.parentId = category.getParentId();
this.name = category.getName();
}
public CategoryDto() {
}
public void addSubNode(CategoryDto subNode) {
if (this.chilen == null) {
this.chilen = new HashSet<>();
}
this.chilen.add(subNode);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getParentId() {
return parentId;
}
public void setParentId(Long parentId) {
this.parentId = parentId;
}
public Set getChilen() {
return chilen;
}
public void setChilen(Set chilen) {
this.chilen = chilen;
}
}
测试
public class GetTree {
public static void main(String[] args) {
//模拟 一次select查询出来的集合
List categories = new ArrayList<>();
categories.add(new Category(1L,"一级目录1"));
categories.add(new Category(2L,"一级目录2"));
categories.add(new Category(3L,"一级目录3"));
categories.add(new Category(4L,"一级目录4"));
categories.add(new Category(5L,"一级目录5"));
categories.add(new Category(7L,"二级目录2-1",2L));
categories.add(new Category(8L,"二级目录3-1",3L));
categories.add(new Category(6L,"二级级目录1-1",1L));
categories.add(new Category(9L,"二级目录3-2",3L));
categories.add(new Category(10L,"三级目录7-10",7L));
categories.add(new Category(11L,"三级目录9-11",9L));
categories.add(new Category(12L,"四级目录11-12",11L));
CategoryDto root = new CategoryDto();
//设置根节点 默认写入的一个根节点 看实际业务场景可以改变
root.setId(0L);
root.setName("root");
HashMap nodehMap = new HashMap<>();
nodehMap.put(0L,root);
//遍历 如果没有父id 将根节点id 设置为父id
for (Category category : categories) {
if (category.getParentId() == null){
category.setParentId(0L);
}
// 放入到map集合中
nodehMap.put(category.getId(),new CategoryDto(category));
}
// 遍历map集合
for (CategoryDto value : nodehMap.values()) {
// 取出当前的 value 的上级目录
CategoryDto parent = nodehMap.get(value.getParentId());
if (parent != null){
// 存在 将他加入进去
parent.addSubNode(value);
}
}
// 返回的root 就是一个树形结构
// return root
}
}
后期优化思想,目录过多时每次build一次目录很麻烦,如果其他地方用到这个目录的话可以设置为一个对象,先遍历该对象,没有再去调用目录树的方法。



