当前位置:   article > 正文

从高德上同步省市区行政区划数据到本地数据库demo_将所有地区同步到数据库

将所有地区同步到数据库

  本地数据库表结构:

create table tbl_sys_area_bak
(
    id          int                                  not null
        primary key,
    parent_ids  varchar(255)                         null comment '所有上级(1,2,3)',
    parent_id   varchar(32)                          null comment '地区上级(3)',
    area_name   varchar(32)                          not null comment '地区名称(深圳市)',
    full_name   varchar(128)                         not null comment '地区全称(中国,广东省,深圳市)',
    level       int                                  not null comment '地区级别',
    code        varchar(100)                         null comment '区域编码',
    sort        int                                  not null comment '排序',
    remarks     varchar(255)                         null comment '备注',
    create_id   int                                  not null comment '创建者id',
    update_id   int                                  not null comment '更新者id',
    create_date datetime   default CURRENT_TIMESTAMP null comment '创建日期',
    update_date datetime   default CURRENT_TIMESTAMP null comment '更新日期',
    del_flag    tinyint(1) default 0                 not null comment '删除标记(Y删除,N未删除)',
    constraint index_area_code
        unique (code)
)
    comment '地区表;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

  注意想用高德开放平台的api,需要先去该平台注册一个应用,我这里用的java,申请的是web服务平台的key。
在这里插入图片描述
粘贴下面代码,替换上自己的key之后,就可以将高德的行政区划数据导入自己本地数据库中。

注意:在跑该demo的过程中,控制台上可能会提示有部分数据导入失败,原因是本地数据库的等级设为了3级(省、市、区/县),有一部分省直辖县级市等级分为了2级(例如:五指山市,仙桃市、天门市……),该县级市下辖乡镇所用的adcode是该县级市的adcode。数据库用adcode作为了表的id,所以当插入了县级市的信息之后,再插入该县级市下辖的乡镇的时候,发现id(即adcode)重复,所以插入失败。这个可以根据自己业务需要,调整表结构来避免这个问题

 @ResponseBody
  @GetMapping("/t1")
  public Object t1() {
    //获取所有得的省份
    List<String> tblSysAreas = List.of(
        "河北省",
        "山西省",
        "辽宁省",
        "吉林省",
        "黑龙江省",
        "江苏省",
        "浙江省",
        "安徽省",
        "福建省",
        "江西省",
        "山东省",
        "河南省",
        "湖北省",
        "湖南省",
        "广东省",
        "海南省",
        "四川省",
        "贵州省",
        "云南省",
        "陕西省",
        "甘肃省",
        "青海省",
        "台湾省",
        "北京市",
        "天津市",
        "上海市",
        "重庆市",
        "内蒙古自治区",
        "广西壮族自治区",
        "西藏自治区",
        "宁夏回族自治区",
        "新疆维吾尔自治区",
        "香港特别行政区",
        "澳门特别行政区"
    );
    for (int i = 0; i < tblSysAreas.size(); i++) {
      int a = i + 1;
      getAreaInfo(tblSysAreas.get(i), a);
    }
    return scsDataRv("插入成功");
  }

  private void getAreaInfo(String areaName, int sort) {
  //此key为高德开放平台上申请的应用
    String key = "10901ba132asflwo4cccde7b122bdd69";
    String url = "https://restapi.amap.com/v3/config/district?"
        + "keywords=" + areaName
        + "&subdistrict=3"
        + "&key=" + key;
    JSONObject jsonObject = null;
    try {
      String response = httpApiService.doGet(url);
      jsonObject = JSONUtil.parseObj(response);
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println(areaName + "没有获取成功");
    }
    JSONObject provinceInfo = null;
    try {
      //第一层,省
      JSONArray jsonArray = jsonObject.getJSONArray("districts");
      provinceInfo = JSONUtil.parseObj(jsonArray.get(0));
      //插入省信息
      insertProvince(provinceInfo, sort);
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("省份插入异常");
    }

    //第二层,市
    JSONArray cityInfos = provinceInfo.getJSONArray("districts");
    int citySort = 0;
    for (int i = 0; i < cityInfos.size(); i++) {
      JSONObject cityInfo = JSONUtil.parseObj(cityInfos.get(i));
      ++citySort;
      try {
        //插入市信息
        insertCityInfo(provinceInfo, cityInfo, citySort);
      } catch (Exception e) {
        e.printStackTrace();
        System.out.println("城市信息插入异常");
      }

      //区信息
      JSONArray areaList = JSONUtil.parseArray(cityInfo.get("districts"));
      int areaSort = 0;
      for (int i1 = 0; i1 < areaList.size(); i1++) {
        JSONObject areaInfo = JSONUtil.parseObj(areaList.get(i1));
        ++areaSort;
        try {
          //插入区信息
          insertAreaInfo(provinceInfo, cityInfo, areaInfo, areaSort);
        } catch (Exception e) {
          e.printStackTrace();
          System.out.println("区域信息插入异常");
        }

      }
    }

  }

  private void insertAreaInfo(JSONObject provinceInfo, JSONObject cityInfo, JSONObject areaInfo, int sort) {
    TblSysAreaBak build = TblSysAreaBak.builder()
        .id(Integer.valueOf(String.valueOf(areaInfo.get("adcode"))))
        .code((String) areaInfo.get("adcode"))
        .sort(sort)
        .updateId(1)
        .createId(1)
        .level(3)
        .parentId((String) cityInfo.get("adcode"))
        .parentIds("0,".concat((String) provinceInfo.get("adcode")).concat(",").concat((String) cityInfo.get("adcode")).concat(",").concat((String) areaInfo.get("adcode")))
        .fullName("中国,".concat((String) provinceInfo.get("name")).concat(",").concat((String) cityInfo.get("name")).concat(",").concat((String) areaInfo.get("name")))
        .delFlag(DelFlagEnum.N.getDelKey())
        .areaName((String) areaInfo.get("name")).build();
    tblSysAreaBakService.insert(build);
  }

  private void insertCityInfo(JSONObject provinceInfo, JSONObject cityInfo, int sort) {
    TblSysAreaBak build = TblSysAreaBak.builder()
        .id(Integer.valueOf(String.valueOf(cityInfo.get("adcode"))))
        .code((String) cityInfo.get("adcode"))
        .sort(sort)
        .updateId(1)
        .createId(1)
        .level(2)
        .parentId((String) provinceInfo.get("adcode"))
        .parentIds("0,".concat((String) provinceInfo.get("adcode")).concat(",").concat((String) cityInfo.get("adcode")))
        .fullName("中国,".concat((String) provinceInfo.get("name")).concat(",").concat((String) cityInfo.get("name")))
        .delFlag(DelFlagEnum.N.getDelKey())
        .areaName((String) cityInfo.get("name")).build();
    tblSysAreaBakService.insert(build);
  }

  private void insertProvince(JSONObject provinceInfo, int sort) {
    TblSysAreaBak build = TblSysAreaBak.builder()
        .id(Integer.valueOf(String.valueOf(provinceInfo.get("adcode"))))
        .code((String) provinceInfo.get("adcode"))
        .sort(sort)
        .updateId(1)
        .createId(1)
        .level(1)
        .parentId("0")
        .parentIds("0,".concat((String) provinceInfo.get("adcode")))
        .fullName("中国,".concat((String) provinceInfo.get("name")))
        .delFlag(DelFlagEnum.N.getDelKey())
        .areaName((String) provinceInfo.get("name")).build();
    tblSysAreaBakService.insert(build);
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号