100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Java查询返回值为树形结构

Java查询返回值为树形结构

时间:2023-10-05 14:40:27

相关推荐

Java查询返回值为树形结构

数据库结构

返回值DTO

package cn.yibiao163.unit.api.dto;import mon.web.dto.AbstractResultDTO;import lombok.Data;import lombok.EqualsAndHashCode;import java.util.List;/*** 国民经济行业分类与代码表** @author yyh* @date -07-30 15:06*/@Data@EqualsAndHashCode(callSuper = true)public class ApiUnitIndustryCategoryResponseDTO extends AbstractResultDTO {private static final long serialVersionUID = 1L;/*** 国民经济行业分类与代码表主键*/private String id;/*** 级别*/private Integer level;/*** 行业代码*/private String industryCode;/*** 行业名称*/private String industryName;/*** 父ID*/private String parentId;/*** 子节点*/private List<ApiUnitIndustryCategoryResponseDTO> children;}

controller层

package cn.yibiao163.unit.api.controller;import mon.web.base.AbstractBaseController;import mon.web.dto.ResultDTO;import cn.yibiao163.unit.api.dto.ApiUnitIndustryCategoryResponseDTO;import cn.yibiao163.unit.service.UnitIndustryCategoryService;import lombok.AllArgsConstructor;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** 国民经济行业分类与代码表*/@RestController@RequestMapping("/api/unit/v1/unitIndustryCategory")@AllArgsConstructorpublic class ApiUnitIndustryCategoryController extends AbstractBaseController {private final UnitIndustryCategoryService unitIndustryCategoryService;/*** 查询所有行业分类列表** @return 行业分类树形结构*/@GetMapping("/dep")public ResultDTO<List<ApiUnitIndustryCategoryResponseDTO>> getUnitIndustryCategoryTree() {return ResultDTO.success(unitIndustryCategoryService.getUnitIndustryCategoryTree());}}

service层

/*** 查询所有行业分类列表**/public List<ApiUnitIndustryCategoryResponseDTO> getUnitIndustryCategoryTree() {List<UnitIndustryCategory> entities = list();//把UnitIndustryCategory的数组entities包装成ApiUnitIndustryCategoryResponseDTO数组List<ApiUnitIndustryCategoryResponseDTO> responseDTOList = entities.stream().map(unitIndustryCategory -> {ApiUnitIndustryCategoryResponseDTO responseVO = new ApiUnitIndustryCategoryResponseDTO();BeanUtils.copyProperties(unitIndustryCategory, responseVO);return responseVO;}).collect(Collectors.toList());//filter是java8的写法,取得所有parentId为0的数据,也就是一级目录//map也是java8的写法,用于封装数据,取得他的孩子(也就是下级目录)的数据List<ApiUnitIndustryCategoryResponseDTO> list = responseDTOList.stream().filter(responseVO ->"0".equals(responseVO.getParentId())).map((menu) -> {menu.setChildren(getChildrenData(menu, responseDTOList));return menu;}).collect(Collectors.toList());return list;}/*** 获取孩子(下级目录)的方法,递归实现*/private List<ApiUnitIndustryCategoryResponseDTO> getChildrenData(ApiUnitIndustryCategoryResponseDTO root, List<ApiUnitIndustryCategoryResponseDTO> all) {List<ApiUnitIndustryCategoryResponseDTO> children = all.stream().filter(responseVO ->responseVO.getParentId().equals(root.getId())).map(responseVO -> {responseVO.setChildren(getChildrenData(responseVO, all));return responseVO;}).collect(Collectors.toList());return children;}

返回结果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。