SpringBoot树获取方法总结

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

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

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

SpringBoot树获取方法总结

 

 SpringBoot树获取方法总结

 二、java代码示例

  • AreaTreeMapper.xml的编写 

    <select id="selectSymptomTreeNodeJson" resultType="com.boot.mybatis.entity.CategoryTreeNode">
      SELECT dq_id as id,dq_name AS title,parent_id as parentId FROM ft_sys_dq
    </select>

  • 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<CategoryTreeNode> children = new ArrayList<>();
      }

  • AreaTreeMapper接口编写

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

 

  • AreaTreeService业务接口编写

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

      List<CategoryTreeNode> getSymptomTreeJson();
    }

 

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

      @Resource
      AreaTreeMapper areaTreeMapper;

      @Override
      public List<CategoryTreeNode> getSymptomTreeJson() {
      //从数据库获取数据的list(一次性获取的所有数据)
      List<CategoryTreeNode> list= areaTreeMapper.selectSymptomTreeNodeJson();
      List<CategoryTreeNode> 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<CategoryTreeNode> getTreeList(){
      return areaTreeService.getSymptomTreeJson();
      }

      }

  • TreeUtil的编写

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

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

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

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

    /**
    * 构建树节点
    */
    public static <T extends CategoryTreeNode> List<T> build(List<T> treeNodes) {
      List<T> result = new ArrayList<>();
      //list转map
      Map<BigInteger, T> 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树获取方法总结

 

 SpringBoot树获取方法总结

 

 

 

 

上一篇:剑指 Offer 68 - I. 二叉搜索树的最近公共祖先


下一篇:2022-2-17剑指offer day7