SpringBoot+Mybatis将数据库中的数据以JSON树形格式返回给前端

前言:

        想着从数据库读取数据,以json树形格式返回到前端。中间遇到些小插曲,idea中有些依赖包莫名其妙就不见了,搞了很久后面重新导入依赖包才运行成功。记录一下今天的所学吧~

话不多说,直接开干! 


目录

1.数据库建表: (id:自身节点编号   pid:父节点编号   name:节点名称)

 2.实体类pojo:

 3.  dao层:

4. *.xml文件:

5.Service接口:

6.Service实现类:

7.Controller层:

8.运行结果:


1.数据库建表: (id:自身节点编号   pid:父节点编号   name:节点名称)

 2.实体类pojo:

package com.hnucm.woc.medicinehome.pojo;

import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
public class TitleList implements Serializable {

    private Integer id;
    private Integer pid;
    private String name;
    private List<TitleList> childName=new ArrayList<>();

    public String toString(){
        return "Information{"+
                "id="+id+
                ",pid="+pid+
                ",name="+name+
                ",childName="+childName+
                "}";
    }


}

 3.  dao层:

package com.hnucm.woc.medicinehome.dao;

import com.hnucm.woc.medicinehome.pojo.TitleList;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface TitleListMapper {

    public List<TitleList> getList();
}

4. *.xml文件:

<?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="com.hnucm.woc.medicinehome.dao.TitleListMapper">

    <select id="getList" resultType="com.hnucm.woc.medicinehome.pojo.TitleList">
        select * from titlelist
    </select>

</mapper>

5.Service接口:

package com.hnucm.woc.medicinehome.service;

import com.hnucm.woc.medicinehome.pojo.TitleList;

import java.util.List;

public interface TitleListService {

    public List<TitleList> getList();

}

6.Service实现类:

package com.hnucm.woc.medicinehome.service;

import com.hnucm.woc.medicinehome.dao.TitleListMapper;
import com.hnucm.woc.medicinehome.pojo.TitleList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class TitleListServiceImpl implements TitleListService{

    @Autowired
    private TitleListMapper titleListMapper;

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

                }

            }

        }

        return resultList;
    }
}

7.Controller层:

package com.hnucm.woc.medicinehome.controller;

import com.hnucm.woc.medicinehome.dao.JsonMapper;
import com.hnucm.woc.medicinehome.pojo.TitleList;
import com.hnucm.woc.medicinehome.pojo.User;
import com.hnucm.woc.medicinehome.service.TitleListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class JsonController {

    @Autowired
    TitleListService titleListService;


    @RequestMapping("getlist")
    @ResponseBody
    public List<TitleList> getlist(){


        return titleListService.getList();
    }

}

8.运行结果:

 

 

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值