获取省市区三级列表

3 篇文章 0 订阅
这篇博客记录了在CRM项目中实现省市区三级列表的获取过程,包括相关代码和DAO层表结构的简单介绍。提供了一个使用外连接的SQL数据示例链接。
摘要由CSDN通过智能技术生成

1. 背景

一个基本的crm项目,都有获取省市区三级列表的需求,简单记录一下。

2. 代码

  public List<TreeNode> findTree() {
        List<AreaEntity> list = areaDao.findAll();

        // 按父节点分组
        ImmutableListMultimap<String, AreaEntity> multiMap = Multimaps.index(list, AreaEntity::getParentCode);
        // 选取省份作为根节点
        List<AreaEntity> provinces = list.stream().filter(input -> input.getType() == 1).collect(Collectors.toList());
        // 递归构建省市树
        return transferAndBuildTree(provinces, multiMap);
    }
private List<TreeNode> transferAndBuildTree(List<AreaEntity> provinceCityEntities, ImmutableListMultimap<String, AreaEntity> multiMap) {
        List<TreeNode> treeNodes = provinceCityEntities.stream().map(input -> {
            TreeNode treeNode = new TreeNode();
            treeNode.setValue(input.getCode());
            treeNode.setLabel(input.getName());
            // 子节点
            ImmutableList<AreaEntity> list = multiMap.get(input.getCode());
            // 没有子节点,构建完毕
            if (list == null || list.isEmpty()) {
                return treeNode;
            }

            // 递归构建子树
            List<TreeNode> children = transferAndBuildTree(list, multiMap);
            treeNode.setChildren(children);
            return treeNode;
        }).collect(Collectors.toList());

        return treeNodes;
    }

3. dao层表结构

CREATE TABLE `area` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `code` varchar(6) CHARACTER SET utf8 NOT NULL DEFAULT '',
  `name` varchar(20) CHARACTER SET utf8 NOT NULL DEFAULT '',
  `type` tinyint(20) NOT NULL DEFAULT '0',
  `parent_code` varchar(6) CHARACTER SET utf8 NOT NULL DEFAULT '',
  `root_code` varchar(6) CHARACTER SET utf8 DEFAULT NULL,
  `deleted` tinyint(4) NOT NULL DEFAULT '0',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `gmt_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3248 DEFAULT CHARSET=utf8mb4;

sql数据:

因为文章篇幅有限,所以引入了一个外连接,大家自取。

http://note.youdao.com/s/ZELDYra4

INSERT INTO hat_province VALUES (1,'110000','北京市'); INSERT INTO hat_province VALUES (2,'120000','天津市'); INSERT INTO hat_province VALUES (3,'130000','河北省'); INSERT INTO hat_province VALUES (4,'140000','山西省'); INSERT INTO hat_province VALUES (5,'150000','内蒙古自治区'); INSERT INTO hat_province VALUES (6,'210000','辽宁省'); INSERT INTO hat_province VALUES (7,'220000','吉林省'); INSERT INTO hat_province VALUES (8,'230000','黑龙江省'); INSERT INTO hat_province VALUES (9,'310000','上海市'); INSERT INTO hat_province VALUES (10,'320000','江苏省'); INSERT INTO hat_province VALUES (11,'330000','浙江省'); INSERT INTO hat_province VALUES (12,'340000','安徽省'); INSERT INTO hat_province VALUES (13,'350000','福建省'); INSERT INTO hat_province VALUES (14,'360000','江西省'); INSERT INTO hat_province VALUES (15,'370000','山东省'); INSERT INTO hat_province VALUES (16,'410000','河南省'); INSERT INTO hat_province VALUES (17,'420000','湖北省'); INSERT INTO hat_province VALUES (18,'430000','湖南省'); ........ INSERT INTO hat_city VALUES (1,'110100','市辖区','110000'); INSERT INTO hat_city VALUES (2,'110200','县','110000'); INSERT INTO hat_city VALUES (3,'120100','市辖区','120000'); INSERT INTO hat_city VALUES (4,'120200','县','120000'); INSERT INTO hat_city VALUES (5,'130100','石家庄市','130000'); INSERT INTO hat_city VALUES (6,'130200','唐山市','130000'); INSERT INTO hat_city VALUES (7,'130300','秦皇岛市','130000'); INSERT INTO hat_city VALUES (8,'130400','邯郸市','130000'); INSERT INTO hat_city VALUES (9,'130500','邢台市','130000'); INSERT INTO hat_city VALUES (10,'130600','保定市','130000'); INSERT INTO hat_city VALUES (11,'130700','张家口市','130000'); INSERT INTO hat_city VALUES (12,'130800','承德市','130000'); INSERT INTO hat_city VALUES (13,'130900','沧州市','130000'); INSERT INTO hat_city VALUES (14,'131000','廊坊市','130000'); INSERT INTO hat_city VALUES (15,'131100','衡水市','130000');
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值