java 改造树_java之树结构的实现

1.建数据库名为tree

d780ccba1cd1930cc98b60f05802f82d.png

2.写代码

包的层次:

01a065ae9695b77123c9e57ad9b0141a.png

实体类:

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 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 {

}

业务逻辑层接口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 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 findList() {

return treeDao.selectList(null);

}

public List handleTree() {

//根节点list

List treeList = new ArrayList<>();

//从数据库获取所有得数据

List 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 childrenList = getChildren(tree.getId(), allTree);

tree.setChildren(childrenList);

});

}

}

return treeList;

}

/**

* 使用递归获取根节点下面得孩子

* @param id

* @param allTree

* @return

*/

public List getChildren(Integer id, List allTree) {

List 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() {

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

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值