springboot 返回多层级关系为json格式给前端(树形层级数据)

后台表是一个类似菜单表,存在自关联的父子关系. 现在想把表数据加工成分级之后的json数据返回给前端.供展示 

 springboot 返回多层级关系为json格式给前端(树形层级数据)_第1张图片

1:实体

/**
 * 

* 课程科目 *

* * @author zxz * @since 2023-03-14 */ @TableName("edu_subject") @JsonInclude(JsonInclude.Include.NON_NULL) //springboo空字段不做json显示 @ApiModel(value = "EduSubject对象", description = "课程科目") @Data @ToString public class EduSubject implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty("课程类别ID") private String id; @ApiModelProperty("类别名称") private String title; @ApiModelProperty("父ID") private String parentId; @ApiModelProperty("排序字段") private Integer sort; @ApiModelProperty("创建时间") private Date gmtCreate; @ApiModelProperty("更新时间") private Date gmtModified; @TableField(exist=false) //排除字段关系 private ArrayList List; }

2:Controller

@Controller
@RequestMapping("/eduSubject")
@CrossOrigin
public class EduSubjectController {

  @Autowired
  private IEduSubjectService iEduSubjectService;
  @GetMapping("getSubject")
  @ResponseBody
  public ResultMessge getSubject(){
    return iEduSubjectService.getSubject();
  }
}

3:Service


/**
 * 

* 课程科目 服务实现类 *

* * @author zxz * @since 2023-03-14 */ @Service public class EduSubjectServiceImpl extends ServiceImpl implements IEduSubjectService { public ResultMessge getSubject (){ //所有学科列表 mybatisplus提供的list List list = list(); //将学科按照父id分类, 放在 <父id,<对应子集合>>的map中 HashMap> hashMap = new HashMap<>(); list.stream().forEach(a -> { hashMap.computeIfAbsent(a.getParentId(), key -> new ArrayList()).add(a); }); //从父节点进入开始递归往下找 listToJson(hashMap,"0"); //递归完成之后,将父节点的List返回就行了 return ResultMessge.success().addMap("json",hashMap.get("0")); } /** * * @param hashMap 源map * @param level levelid */ public void listToJson(HashMap> hashMap, String level){ //获得level等级的子集合 ArrayList list = hashMap.get(level); list.stream().forEach(e -> { ArrayList eduSubjectlist = hashMap.get(e.getId()); //获得level层级的孙集合 e.setList(eduSubjectlist); //子集合设置到父 if(eduSubjectlist!=null&&eduSubjectlist.size()!=0){ eduSubjectlist.stream().forEach(es -> { //子集合在去找孙集合然后递归 listToJson(hashMap,es.getParentId()); }); } }); } }

4:表数据

springboot 返回多层级关系为json格式给前端(树形层级数据)_第2张图片

 别的都是2个层级, 新增了一条记录是第3个层级. 前端开发--html/css--测试

 5:ResultMessge

package com.zxz.entiry;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @auther nnzxz
 * @create 2023-03-12
 * @used do something
 */
@Data
public class ResultMessge {
  final static int SUCESSCODE=20000 ;
  final static int DEFEATCODE=20001 ;

  public Boolean flag;
  public int code;
  private String message;
  private Map data;

  public ResultMessge addMap(String key,Object value){
    if(this.data==null){
      this.data=new HashMap<>();
    }
    this.data.put(key,value);
    return this;
  }
  public ResultMessge() {
  }

  public ResultMessge(Boolean flag, int code, String message, Map  items) {
    this.flag = flag;
    this.code = code;
    this.message = message;
    this.data = items;
  }

  public static ResultMessge success(){
    return new ResultMessge(true,SUCESSCODE,"成功",null);
  }

  public static ResultMessge defeat(){
    return new ResultMessge(false,DEFEATCODE,"失败",null);
  }
}

6: 效果

springboot 返回多层级关系为json格式给前端(树形层级数据)_第3张图片

你可能感兴趣的:(spring,boot,json,java)