SpringBoot+Mybatis将数据库中的数据以JSON树形格式返回给前端
·
前言:
想着从数据库读取数据,以json树形格式返回到前端。中间遇到些小插曲,idea中有些依赖包莫名其妙就不见了,搞了很久后面重新导入依赖包才运行成功。记录一下今天的所学吧~
话不多说,直接开干!
目录
1.数据库建表: (id:自身节点编号 pid:父节点编号 name:节点名称)
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.运行结果:
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐


所有评论(0)