Java (省市区三级联动),可扩展到4级或者5级(目前全网最新)

发布时间:2024年01月14日

项目场景:

在项目编写的过程中 需要实现客户选择的具体大区信息

三级联动表结构

DROP TABLE IF EXISTS `tb_ipr_item_region`;
CREATE TABLE `tb_ipr_item_region`  (
  `id` int(11) NOT NULL COMMENT '主键ID',
  `pid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '父级Id',
  `deep` int(11) NULL DEFAULT NULL COMMENT '级别 0 省/直辖市  1 :市级别  2:区级别/县级别',
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '地区简称',
  `pinyin_prefix` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '地区首拼',
  `pinyin` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '地区拼音',
  `ext_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '地区ID',
  `ext_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '地区全称',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin COMMENT = '三级联动地区表' ROW_FORMAT = Compact;


具体思路:

  • 遍历所有地区数据,根据地区的深度(deep)将节点分为省级、市级和区县级别。
  • 使用三个映射(provinceMap、cityMap、districtMap)分别存储省、市和区县的节点,以便在构建树结构时能够方便地找到父节点。
  • 根据地区的深度,将节点添加到相应的映射和最终的树结构中。
  • 确保每个父节点都有一个子节点列表,并将当前节点添加到该列表中,以构建完整的树形结构。
  • 返回构建好的树结构作为响应结果。

实现方式:

节点类

@Data
public class TreeNodeRegion {
    private Integer id;
    private String value;
    private String label;
    private String name;
    private List<TreeNodeRegion> children;

    public TreeNodeRegion(Integer id, String name,String value,String label) {
        this.id = id;
        this.name = name;
        this.value = value;
        this.label = label;
        this.children = null; // 初始化为 null,而不是空列表
    }
}

方法实现类

	@PostMapping("area")
    @ApiOperation(value = "地区接口")
    public ResponseResult<List<TreeNodeRegion>> area() {
        //存放最终树的架构
        List<TreeNodeRegion> tree = new ArrayList<>();
        //省级节点
        Map<Integer, TreeNodeRegion> provinceMap = new HashMap<>();
        //市级节点
        Map<Integer, TreeNodeRegion> cityMap = new HashMap<>();
        //县级别结点  如果需要存放到 镇级别的 可以使用此集合扩展
        Map<Integer, TreeNodeRegion> districtMap = new HashMap<>();
        //获取所有地区的数据
        List<IprItemRegion> allRegions = iprItemRegionService.list();
        //构建树架构
        for (IprItemRegion region : allRegions) {
            // 创建地区节点对象
            TreeNodeRegion regionNode = new TreeNodeRegion(region.getId(), region.getName(), region.getPinyin(), region.getExtName());
            // 根据地区的深度进行分类处理
            if (region.getDeep() == 0) {
                // 省级节点
                provinceMap.put(region.getId(), regionNode);
                tree.add(regionNode);
            } else if (region.getDeep() == 1) {
                // 市级节点
                cityMap.put(region.getId(), regionNode);
                // 获取市级节点的父节点(省级节点)
                TreeNodeRegion parent = provinceMap.get(Integer.parseInt(region.getPid()));
                if (parent != null) {
                    // 确保父节点有子节点列表
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    // 将市级节点添加到父节点的子节点列表中
                    parent.getChildren().add(regionNode);
                }
            } else if (region.getDeep() == 2) {
                // 区县节点
                districtMap.put(region.getId(), regionNode);
                // 获取区县节点的父节点(市级节点)
                TreeNodeRegion parent = cityMap.get(Integer.parseInt(region.getPid()));
                if (parent != null) {
                    // 确保父节点有子节点列表
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    // 将区县节点添加到父节点的子节点列表中
                    parent.getChildren().add(regionNode);
                }
            }
        }
        // 返回构建好的树结构
        return new ResponseResult<>(tree);
    }

返回数据结构

[
    {
        "id":11,
        "value":"bei jing",
        "label":"北京市",
        "name":"北京",
        "children":[
            {
                "id":1101,
                "value":"bei jing",
                "label":"北京市",
                "name":"北京",
                "children":[
                    {
                        "id":110101,
                        "value":"dong cheng",
                        "label":"东城区",
                        "name":"东城",
                        "children":null
                    },
                    {
                        "id":110102,
                        "value":"xi cheng",
                        "label":"西城区",
                        "name":"西城",
                        "children":null
                    },
                    {
                        "id":110105,
                        "value":"chao yang",
                        "label":"朝阳区",
                        "name":"朝阳",
                        "children":null
                    },
                    {
                        "id":110106,
                        "value":"feng tai",
                        "label":"丰台区",
                        "name":"丰台",
                        "children":null
                    },
                    {
                        "id":110107,
                        "value":"shi jing shan",
                        "label":"石景山区",
                        "name":"石景山",
                        "children":null
                    },
                    {
                        "id":110108,
                        "value":"hai dian",
                        "label":"海淀区",
                        "name":"海淀",
                        "children":null
                    },
                    {
                        "id":110109,
                        "value":"men tou gou",
                        "label":"门头沟区",
                        "name":"门头沟",
                        "children":null
                    },
                    {
                        "id":110111,
                        "value":"fang shan",
                        "label":"房山区",
                        "name":"房山",
                        "children":null
                    },
                    {
                        "id":110112,
                        "value":"tong zhou",
                        "label":"通州区",
                        "name":"通州",
                        "children":null
                    },
                    {
                        "id":110113,
                        "value":"shun yi",
                        "label":"顺义区",
                        "name":"顺义",
                        "children":null
                    },
                    {
                        "id":110114,
                        "value":"chang ping",
                        "label":"昌平区",
                        "name":"昌平",
                        "children":null
                    },
                    {
                        "id":110115,
                        "value":"da xing",
                        "label":"大兴区",
                        "name":"大兴",
                        "children":null
                    },
                    {
                        "id":110116,
                        "value":"huai rou",
                        "label":"怀柔区",
                        "name":"怀柔",
                        "children":null
                    },
                    {
                        "id":110117,
                        "value":"ping gu",
                        "label":"平谷区",
                        "name":"平谷",
                        "children":null
                    },
                    {
                        "id":110118,
                        "value":"mi yun",
                        "label":"密云区",
                        "name":"密云",
                        "children":null
                    },
                    {
                        "id":110119,
                        "value":"yan qing",
                        "label":"延庆区",
                        "name":"延庆",
                        "children":null
                    }
                ]
            }
        ]
    }
]
文章来源:https://blog.csdn.net/qq_49354230/article/details/135566494
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。