SpringBoot树获取方法总结

最近项目中有需要获取全国行政区划省-市-区县-乡镇、街道办的树状结构数据,现将自己获取树的方法总结如下,有不到之处,敬请批评指正!

一、全国行政区划数据的整理以及获取

  1. 获取地址:https://pan.baidu.com/s/19JxdWH1R4z_HrrodFJUfjw?pwd=7bey    提取码:7bey 
  2. 创建表

SpringBoot树获取方法总结_第1张图片

 

 SpringBoot树获取方法总结_第2张图片

 二、java代码示例

  • AreaTreeMapper.xml的编写 

    

  • CategoryTreeNode实体类的编写
    • /**
      * @Project:
      * @Description:
      * @Auther: songwp
      * @Date: 2022/2/14 19:41
      **/
      @Data
      public class CategoryTreeNode {
      private BigInteger id;
      private String title;
      private BigInteger parentId;
      private List children = new ArrayList<>();
      }

  • AreaTreeMapper接口编写

    /**
    * @Project:
    * @Description:
    * @Auther: songwp
    * @Date: 2022/2/14 19:44
    **/
    @Mapper
    public interface AreaTreeMapper {
      List selectSymptomTreeNodeJson();
    }

 

  • AreaTreeService业务接口编写

    /**
    * @Project:
    * @Description:
    * @Auther: songwp
    * @Date: 2022/2/14 19:52
    **/
    public interface AreaTreeService {

      List getSymptomTreeJson();
    }

 

  • AreaTreeServiceImpl的业务实现类编写
    • /**
      * @Project:
      * @Description:
      * @Auther: songwp
      * @Date: 2022/2/14 19:53
      **/
      @Service
      public class AreaTreeServiceImpl implements AreaTreeService {

      @Resource
      AreaTreeMapper areaTreeMapper;

      @Override
      public List getSymptomTreeJson() {
      //从数据库获取数据的list(一次性获取的所有数据)
      List list= areaTreeMapper.selectSymptomTreeNodeJson();
      List resultList = new ArrayList<>();
      TreeUtil treeUtilNew = new TreeUtil();
      resultList = treeUtilNew.build(list); //进行封装
      return resultList; //返回树形结构json
      }
      }

  • AreaTreeController的编写  
    • /**
      * @Project:
      * @Description:
      * @Auther: songwp
      * @Date: 2022/2/14 19:57
      **/
      @RestController
      public class AreaTreeController {

      @Autowired
      AreaTreeService areaTreeService;

      @GetMapping("/getTreeList")
      public List getTreeList(){
      return areaTreeService.getSymptomTreeJson();
      }

      }

  • TreeUtil的编写

    /**
    * @Project:
    * @Description: 构架树结构树形json--好用
    * @Auther: songwp
    * @Date: 2022/2/14 19:39
    **/
    public class TreeUtil {

    /**
    * 根据pid,构建树节点
    */
    public static List build(List treeNodes, Integer pid) {

      List treeList = new ArrayList<>();
      for(T treeNode : treeNodes) {
        if (pid.equals(treeNode.getParentId())) {
          treeList.add(findChildren(treeNodes, treeNode));
        }
      }
    return treeList;
    }

    /**
    * 查找子节点
    */
    private static T findChildren(List treeNodes, T rootNode) {
      for(T treeNode : treeNodes) {
        if(rootNode.getId().equals(treeNode.getParentId())) {
          rootNode.getChildren().add(findChildren(treeNodes, treeNode));
        }
      }
      return rootNode;
    }

    /**
    * 构建树节点
    */
    public static List build(List treeNodes) {
      List result = new ArrayList<>();
      //list转map
      Map nodeMap = new LinkedHashMap<>(treeNodes.size());
      for(T treeNode : treeNodes){
        nodeMap.put(treeNode.getId(), treeNode);
      }
      for(T node : nodeMap.values()) {
        T parent = nodeMap.get(node.getParentId());
          if(parent != null && !(node.getId().equals(parent.getId()))){
            parent.getChildren().add(node);
            continue;
            }

          result.add(node);
          }
        return result;
        }
    }

三、调用接口获取数据如下:

SpringBoot树获取方法总结_第3张图片

 

 SpringBoot树获取方法总结_第4张图片

 

 

 

 

你可能感兴趣的:(SpringBoot树获取方法总结)