日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

TreeNode实现Java列表转树形结构列表

發(fā)布時(shí)間:2024/5/24 综合教程 27 生活家
生活随笔 收集整理的這篇文章主要介紹了 TreeNode实现Java列表转树形结构列表 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

表結(jié)構(gòu)

CREATE TABLE `test2` (
  `id` varchar(32) DEFAULT NULL,
  `prarentid` varchar(32) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

實(shí)體類

@Data
public class Test2 {
  private String id;
  private String parentid;
  private String name;
}

訪問(wèn)數(shù)據(jù)接口

@Mapper
public interface Test2Mapper {
  List<Test2> selectTreeList();
}

業(yè)務(wù)實(shí)現(xiàn)類

@Service
@Slf4j
public class Test2Service {
 
  @Autowired
  private Test2Mapper test2Mapper;
 
  public List<Test2> getTreeList() {
    return test2Mapper.selectTreeList();
  }
}

控制層類

@Api(value = "TEST2管理", tags = {"TEST2管理"})
@RestController
@Slf4j
@RequestMapping("/test")
public class Test2Controller {
  @Autowired
  private Test2Service test2Service;
 
  @ApiOperation(value = "樹形結(jié)構(gòu)列表")
  @GetMapping("/list")
  public ResponseEntity listUser() {
    TreeNode node = new TreeNode();
    List<Test2> treeList = test2Service.getTreeList();
    if (treeList.size() > 0) {
      for (Test2 test : treeList) {
        // 初始化
        TreeNode tn = new TreeNode(test.getId(), test.getParentid(), test.getId(), test.getName());
        node.add(tn);
      }
    }
    return new ResponseEntity(PublicConstant.SUCCESS_CODE, PublicConstant.SUCCESS_MSG,
        node.getChildren());
  }
}

接口公共返回實(shí)體

@Data
public class ResponseEntity {
    //返回編碼
    private String msgCode;
 
    //返回信息
    private String message;
 
    //返回的數(shù)據(jù)
    private Object data;
}

TreeNode工具類

package com.sb.util;
 
import java.util.ArrayList;
 
/**
 * TreeNode 工具類
 */
public class TreeNode {
  private String uuid;
  private String parentUuid;
  private String tagUuid;
  private String poiName;
  private ArrayList<TreeNode> children = new ArrayList<TreeNode>();
  public TreeNode() {}
  /**
   * 遞歸添加節(jié)點(diǎn)
   */
  public void add(TreeNode node) {
    if (node.parentUuid == null || "".equals(node.parentUuid)) {
      // 父節(jié)點(diǎn)
      this.children.add(node);
    } else if (node.parentUuid.equals(this.uuid)) {
      // 子節(jié)點(diǎn)
      this.children.add(node);
    } else {
      for (TreeNode tmp_node : children) {
        tmp_node.add(node);
      }
    }
  }
 
  public String getUuid() {
    return uuid;
  }
 
  public void setUuid(String uuid) {
    this.uuid = uuid;
  }
 
  public String getParentUuid() {
    return parentUuid;
  }
 
  public void setParentUuid(String parentUuid) {
    this.parentUuid = parentUuid;
  }
 
  public String getTagUuid() {
    return tagUuid;
  }
 
  public void setTagUuid(String tagUuid) {
    this.tagUuid = tagUuid;
  }
 
  public String getPoiName() {
    return poiName;
  }
 
  public void setPoiName(String poiName) {
    this.poiName = poiName;
  }
 
  public ArrayList<TreeNode> getChildren() {
    return children;
  }
 
  public void setChildren(ArrayList<TreeNode> children) {
    this.children = children;
  }
 
  public TreeNode(String uuid, String parentUuid, String tagUuid, String poiName) {
    this.uuid = uuid;
    this.parentUuid = parentUuid;
    this.tagUuid = tagUuid;
    this.poiName = poiName;
  }
}

接口請(qǐng)求結(jié)果

{
  "msgCode": "1000",
  "message": "操作成功",
  "data": [
    {
      "uuid": "1",
      "parentUuid": null,
      "tagUuid": "1",
      "poiName": "老三",
      "children": [
        {
          "uuid": "2",
          "parentUuid": "1",
          "tagUuid": "2",
          "poiName": "老四",
          "children": [
            {
              "uuid": "3",
              "parentUuid": "2",
              "tagUuid": "3",
              "poiName": "老五",
              "children": []
            }
          ]
        }
      ]
    }
  ]
}

總結(jié)

以上是生活随笔為你收集整理的TreeNode实现Java列表转树形结构列表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。