欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java之树结构的实现

程序员文章站 2022-07-10 18:21:15
1.建数据库名为tree2.写代码包的层次:实体类: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.mybatisp....

1.建数据库名为tree

java之树结构的实现

2.写代码

包的层次:

java之树结构的实现

实体类:

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:

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
        }
    ]
}

本文地址:https://blog.csdn.net/demo_gsl/article/details/108252238

相关标签: Java