spring boot 接受一对多数据库数据 返回前端树状json格式

方法来源

方法来自以上 稍作修改

spring boot 接受一对多数据库数据 返回前端树状json格式_第1张图片

实体类

package org.yungu.modules.system.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

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

@Data
@TableName("natural_resources_data_layer")
public class QianXian {
    private String id;
    private String pid;
    private String title;
    private List<QianXian> childName = new ArrayList<>();
}

service

package org.yungu.modules.system.service;

import com.alibaba.fastjson.JSONObject;
import org.yungu.modules.system.entity.QianXian;

import java.util.List;

public interface QianXianService {
        public List<QianXian> getList();
//    JSONObject getInfo(JSONObject jsonObject);
}

mapper

package org.yungu.modules.system.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.yungu.modules.system.entity.QianXian;

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

@Mapper
public interface QianXianMapper extends BaseMapper<QianXian> {
    ArrayList<QianXian> getList();
}

xml文件(注意修改namespace与resultType路径)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.yungu.modules.system.mapper.QianXianMapper">

    <select id="getList" resultType="org.yungu.modules.system.entity.QianXian">
        select *
        from natural_resources_data_layer
        order by title asc
    </select>

</mapper>

service实现类

package org.yungu.modules.system.service.impl;


import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yungu.modules.system.mapper.QianXianMapper;
import org.yungu.modules.system.entity.QianXian;
import org.yungu.modules.system.service.QianXianService;

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

@Service
public class QianXianServiceImpl implements QianXianService {
    @Autowired
    private QianXianMapper qianXianMapper;

    @Override
    public List<QianXian> getList() {
        //从数据库读取数据
        List<QianXian> allList = qianXianMapper.getList();
        allList.forEach(System.out::println);
        allList.stream().sorted();

        allList.forEach(System.out::println);
        //父类容器
        List<QianXian> parentsList = new ArrayList<>();
        //返回值集合
        List<QianXian> resultList = new ArrayList<>();

        //对数据库拿到的数据进行遍历
        for (QianXian titleList : allList) {
            //判断是否为(父)根节点,如果是父节点,就加入父类容器和返回值集合
            if (titleList.getPid() == null) {
                parentsList.add(titleList);
                resultList.add(titleList);
            } else {
                //对不是根节点的数据进行遍历
                for (QianXian parent : allList) {
                    //把数据的pid与容器中的节点进行匹配,如果匹配成功,
                    //就把数据加入到父类容器节点的子节点集合中
                    if (parent.getId().equals(titleList.getPid())) {
                        //把该数据加入到父节点的子节点中
                        parent.getChildName().add(titleList);
                        //该数据可能是其他节点的父节点,所以加入到父类容器中
                        parentsList.add(titleList);
                        break;
                    }
                }
            }
        }
        return resultList;
    }
}

controller

	@Resource
    QianXianService qianXianService;
    @RequestMapping("/getList")
    @ResponseBody
    public List<QianXian> getList(){
        return qianXianService.getList();
    }

spring boot 接受一对多数据库数据 返回前端树状json格式_第2张图片

你可能感兴趣的:(Springboot,spring,boot,数据库,前端,json,java)