Java一次返回中国所有省市区三级树形级联+前端vue展示【200ms内】

一、前言中国省市区还是不少的 , 省有34个 , 市有391个,区有1101个 , 这是以小编的库里的,可能不是最新的,但是个数也差不了多少 。
当一次返回所有的数据,并且还要组装成一个三级树,一般的for , 会循环34*391*1101次 。这样就是千万级的 , 加上与数据库交互 , 你跑半天也跑不完 。
最后的处理是组长提供的思路,果然很快 。
二、思路首先:小编的省市区是三张表
第一:我们通过三次IO从数据库中查询出所有省市区的数据,下面在进行for循环组装树形效率就很快了!
第二:为了减少IO交互 , 我们把刚刚取出来的市和区构建为map<provinceCode,List<City>>map<districtCode,List<District>>,这样效率就上来了!
三、数据库表设计1. 省CREATE TABLE `address_province`(`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',`code` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`short_name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`new_code` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`is_latest` tinyint(3) NULL DEFAULT 1 COMMENT '是否是最新地址,0否1是',`remarks` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`created_at` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',`created_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`updated_at` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',`updated_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`is_deleted` tinyint(3) UNSIGNED NOT NULL DEFAULT 0 COMMENT '删除标记,0:正常;1:删除',PRIMARY KEY (`id`) USING BTREE,UNIQUE INDEX `uk_code`(`code`) USING BTREE,INDEX `idx_address_province_name`(`name`) USING BTREE,INDEX `idx_address_province_short_name`(`short_name`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 35 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '地址省' ROW_FORMAT = Dynamic;2. 市:CREATE TABLE `address_city`(`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',`code` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`new_code` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`province_code` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`is_latest` tinyint(3) NULL DEFAULT 1 COMMENT '是否是最新地址,0否1是',`remarks` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`created_at` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',`created_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`updated_at` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',`updated_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`is_deleted` tinyint(3) NULL DEFAULT 0 COMMENT '删除标记,0正常1删除',PRIMARY KEY (`id`) USING BTREE,UNIQUE INDEX `uk_code`(`code`) USING BTREE,INDEX `idx_address_city_name`(`name`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 392 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '地址市' ROW_FORMAT = Dynamic;3. 区CREATE TABLE `address_district`(`id` int(11) NOT NULL AUTO_INCREMENT,`code` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`new_code` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`city_code` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`province_code` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`is_latest` tinyint(3) NULL DEFAULT 1 COMMENT '是否是最新地址,0否1是',`remarks` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`created_at` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',`created_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`updated_at` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',`updated_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`is_deleted` tinyint(3) NULL DEFAULT 0 COMMENT '删除标记,0正常1删除',PRIMARY KEY (`id`) USING BTREE,UNIQUE INDEX `uk_code`(`code`) USING BTREE,INDEX `idx_address_district_name`(`name`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 3110 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '地址县/区' ROW_FORMAT = Dynamic;数据量太大了,就不给大家了!
四、具体实现省市区的实体类就不展示了,大家根据自己公司的设计稍微改动即可使用!

推荐阅读