行政区划代码(使用国务院官方数据)形成树形结构数据表
2022年中华人民共和国行政区划代码
https://www.mca.gov.cn/article/sj/xzqh/1980/202304/20230400047341.shtml
下拉复制
粘贴到excel,导入数据库
表结构为:
CREATE TABLE `citynew` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`pre_id` int(11) DEFAULT NULL,
`code` varchar(255) DEFAULT NULL,
`city` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3212 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
生成父级preId代码:
public void fun (boolean flag){
Map map = new HashMap();
List<Citynew> list = citynewMapper.selectAll(map);
int i = list.size();
for (int j = 0; j < i; j++) {
Citynew item = list.get(j);
if(flag){
if(item.getPreId()==null) {
if (item.getCode() != null && item.getCode().endsWith("0000")) {
item.setPreId(0);
citynewMapper.updateByPrimaryKey(item);
}
if (!item.getCode().endsWith("00") && (!item.getCode().endsWith("0000"))) {
//判断是否省直辖
String midFix = item.getCode().substring(2, 4);
if(midFix.equals("90")){
String preFix = item.getCode().substring(0, 2);
preFix = preFix + "0000";
Map params = new HashMap();
params.put("code", preFix);
Citynew privance = citynewMapper.selectAll(params).get(0);
int preId = privance.getId();
item.setPreId(preId);
citynewMapper.updateByPrimaryKey(item);
}else {
String preFix = item.getCode().substring(0, 4);
preFix = preFix + "00";
Map params = new HashMap();
params.put("code", preFix);
Citynew city = citynewMapper.selectAll(params).get(0);
int preId = city.getId();
item.setPreId(preId);
citynewMapper.updateByPrimaryKey(item);
}
}
if (item.getCode().endsWith("00") && (!item.getCode().endsWith("0000"))) {
String preFix = item.getCode().substring(0, 2);
preFix = preFix + "0000";
Map params = new HashMap();
params.put("code", preFix);
Citynew privance = citynewMapper.selectAll(params).get(0);
int preId = privance.getId();
item.setPreId(preId);
citynewMapper.updateByPrimaryKey(item);
}
}
}else {
item.setId(item.getId()+1);
citynewMapper.updateByPrimaryKey(item);
}
}
}
Map map = new HashMap();
List<Citynew> list = citynewMapper.selectAll(map);
int i = list.size();
for (int j = 0; j < i; j++) {
Citynew item = list.get(j);
if(flag){
if(item.getPreId()==null) {
if (item.getCode() != null && item.getCode().endsWith("0000")) {
item.setPreId(0);
citynewMapper.updateByPrimaryKey(item);
}
if (!item.getCode().endsWith("00") && (!item.getCode().endsWith("0000"))) {
//判断是否省直辖
String midFix = item.getCode().substring(2, 4);
if(midFix.equals("90")){
String preFix = item.getCode().substring(0, 2);
preFix = preFix + "0000";
Map params = new HashMap();
params.put("code", preFix);
Citynew privance = citynewMapper.selectAll(params).get(0);
int preId = privance.getId();
item.setPreId(preId);
citynewMapper.updateByPrimaryKey(item);
}else {
String preFix = item.getCode().substring(0, 4);
preFix = preFix + "00";
Map params = new HashMap();
params.put("code", preFix);
Citynew city = citynewMapper.selectAll(params).get(0);
int preId = city.getId();
item.setPreId(preId);
citynewMapper.updateByPrimaryKey(item);
}
}
if (item.getCode().endsWith("00") && (!item.getCode().endsWith("0000"))) {
String preFix = item.getCode().substring(0, 2);
preFix = preFix + "0000";
Map params = new HashMap();
params.put("code", preFix);
Citynew privance = citynewMapper.selectAll(params).get(0);
int preId = privance.getId();
item.setPreId(preId);
citynewMapper.updateByPrimaryKey(item);
}
}
}else {
item.setId(item.getId()+1);
citynewMapper.updateByPrimaryKey(item);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通