关于java:java之树结构得实现

2次阅读

共计 3806 个字符,预计需要花费 10 分钟才能阅读完成。

实体类:

package com.gsl.node.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**

  • @Author:Guo Shi Lin
  • @date:2020/8/18 13:57
  • @description:菜单树
  • @modified By:
  • @version: 1.0

*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(“tree”)
public class Tree {

@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private Integer parentId;
@TableField(exist = false)
private List<Tree> children;

}
数据拜访层接口 dao:

package com.gsl.node.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gsl.node.entity.Tree;
import org.apache.ibatis.annotations.Mapper;

/**

  • @Author:Guo Shi Lin
  • @date:2020/8/18 13:58
  • @description:
  • @modified By:
  • @version: 1.0

*/
@Mapper
public interface TreeDao extends BaseMapper<Tree> {

}

业务逻辑层接口 service:

package com.gsl.node.service;

import com.gsl.node.entity.Tree;

import java.util.List;

/**

  • @Author:Guo Shi Lin
  • @date:2020/8/18 13:58
  • @description:
  • @modified By:
  • @version: 1.0

*/
public interface TreeService {

/**
 * 查找树
 * @return
 */
List<Tree> findList();

}
业务逻辑层实现类 impl:
外汇经纪商比照 https://www.fx61.com/brokerlist

package com.gsl.node.service.impl;

import com.gsl.node.dao.TreeDao;
import com.gsl.node.entity.Tree;
import com.gsl.node.service.TreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**

  • @Author:Guo Shi Lin
  • @date:2020/8/18 14:00
  • @description:用的递归实现
  • @modified By:
  • @version: 1.0

*/
@Service
public class TreeServiceImpl implements TreeService {

@Autowired
private TreeDao treeDao;

@Override
public List<Tree> findList() {return treeDao.selectList(null);
}

public List<Tree> handleTree() {
    // 根节点 list
    List<Tree> treeList = new ArrayList<>();
    // 从数据库获取所有得数据
    List<Tree> allTree = treeDao.selectList(null);
    if (null != allTree && allTree.size() > 0) {
        allTree.forEach(tree -> {if (tree.getParentId() == 0) {treeList.add(tree);
            }
        });

        if (null != treeList && treeList.size() > 0) {
            treeList.forEach(tree -> {
                // 应用递归获取孩子
                List<Tree> childrenList = getChildren(tree.getId(), allTree);
                tree.setChildren(childrenList);
            });
        }
    }
    return treeList;
}

/**
 * 应用递归获取根节点上面得孩子
 * @param id
 * @param allTree
 * @return
 */
public List<Tree> getChildren(Integer id, List<Tree> allTree) {List<Tree> childrenList = new ArrayList<>();
    allTree.forEach(tree -> {if (tree.getParentId().equals(id)) {childrenList.add(tree);
        }
    });

    if (null != childrenList && childrenList.size() > 0) {
        childrenList.forEach(tree -> {tree.setChildren(getChildren(tree.getId(), allTree));
        });

    } else {return null;}
    return childrenList;
}

}
管制层 controller:

package com.gsl.node.controller;

import com.gsl.node.entity.Tree;
import com.gsl.node.service.TreeService;
import com.gsl.node.service.impl.TreeServiceImpl;
import com.gsl.result.ResponseResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**

  • @Author:Guo Shi Lin
  • @date:2020/8/19 9:37
  • @description:
  • @modified By:
  • @version: 1.0

*/
@RestController
@Api(tags = “ 树 ”)
public class TreeController {

@Autowired
private TreeServiceImpl treeService;

@GetMapping("tree")
public ResponseResult<Tree> tree() {return new ResponseResult(200, "胜利", treeService.handleTree());
}

}
3. 测试后果
{

"code": 200,
"msg": "胜利",
"data": [
    {
        "id": 1,
        "name": "根节点",
        "parentId": 0,
        "children": [
            {
                "id": 2,
                "name": "父节点",
                "parentId": 1,
                "children": [
                    {
                        "id": 3,
                        "name": "子节点",
                        "parentId": 2,
                        "children": null
                    },
                    {
                        "id": 4,
                        "name": "子节点 -1",
                        "parentId": 2,
                        "children": null
                    }
                ]
            },
            {
                "id": 5,
                "name": "父节点 -1",
                "parentId": 1,
                "children": null
            }
        ]
    },
    {
        "id": 6,
        "name": "根节点 -1",
        "parentId": 0,
        "children": null
    }
]

}

正文完
 0