获取全国所有省份,市区县的中文名和英文名
数据来源是一个在线的文本数据,里面包含了全国所有省份,
市区县的名字以及英文名(拼音),还有编码(应该是作者自己编的);
下载下来,放到本地,最好跟java文件放一起这样好找
在线的文本数据:
https://wuliu.taobao.com/user/output_address.do?range=county
一. 将文本解析
格式类似于JSON,将大括号两边的内容剔除掉,
再将单引号全部替换成双引号,就是JSON格式的字符串了
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* create by fzg
* 2022/11/21 10:41
*/
@Slf4j
public class ProvinceCityAndCounty {
public static void main(String[] args) {
// txt文本文件的位置
String txtUrl = "E:\\学习\\项目\\vue-vant-app\\myBlog\\myCaveolae_server\\src\\main\\java\\com\\fzg\\common\\demo\\country\\province.txt";
ArrayList<HashMap<String, Object>> chineseAllProvinces = getChineseAllProvincesAndCityCounty(txtUrl);
log.info(JSON.toJSONString(chineseAllProvinces));
}
/**
* 获取中国所有省份中文名和英文名
* @param txtUrl
* @return
*/
public static ArrayList<HashMap<String,Object>> getChineseAllProvincesAndCityCounty(String txtUrl){
// 根据文件url读取文件内容
File file = new File(txtUrl);
BufferedReader bf;
String data = "";
try {
bf = new BufferedReader(new FileReader(file));
data = bf.readLine();
} catch (FileNotFoundException e) {
e.printStackTrace();
log.info("文件未找到");
} catch (IOException e) {
e.printStackTrace();
log.info("文件读取失败");
}
if (data.equals("")){
log.info("文件内容为空");
return null;
}else {
log.info("文件读取成功");
log.info(data);
return convertContent(data);
}
}
/**
* 处理文件内容
* @param data
* @return
*/
public static ArrayList<HashMap<String,Object>> convertContent(String data){
// 文件内容,把大括号前面后面都剔除 var name = { }; 我们要的是大括号中间的部分
String res = data.substring(data.indexOf("{"), data.lastIndexOf("}") + 1);
// 将文件内容里的单引号全部替换成双引号
res = res.replace("'","\"");
log.info(res);
// 现在res 已经是一个标准的JSON字符串了,把他解析成对象就行了
JSONObject jsonObject = new JSONObject(JSON.parseObject(res));
// log.info(jsonObject.toString());
// 将JSONObject转为list
Map<String, Object> innerMap = jsonObject.getInnerMap();
Set<Map.Entry<String, Object>> entries = innerMap.entrySet();
ArrayList<HashMap<String, Object>> list = new ArrayList<>();
for (Map.Entry<String, Object> entry : entries) {
HashMap<String, Object> map = new HashMap<>();
JSONArray objects = JSON.parseArray(entry.getValue().toString());
map.put("code",entry.getKey());
map.put("parentCode",objects.get(1));
map.put("chineseName",objects.get(0));
map.put("englishName",objects.get(2));
list.add(map);
}
log.info(list.toString());
if (list.size() > 0){
log.info("解析成功");
}else {
log.info("解析失败");
}
return list;
}
}
二. 配置
解析成功之后返回了一个list
用springboot连接MySQL,配置文件(application.yaml)
server:
port: 端口号
spring:
datasource:
url: jdbc:mysql://localhost:3306/数据库名称?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: 用户名
password: 密码
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
#打印SQl日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
pom.xml里导入mybatis-plus依赖以及要用到的其他依赖
<!-- 连接MySQL -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<!-- springboot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 连接MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 实体类,打印日志等 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- JSON数据转换 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.4.3.4</version>
</dependency>
<!-- mybatis-plus代码生成 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<!-- mybatis-plus代码生成时,需要用到freemarker模板 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
然后代码生成器根据表生成对应的controller,service,mapper,entity等类
关于框架怎么生成请查看我的博客:
https://blog.csdn.net/weixin_50794535/article/details/127965546?spm=1001.2014.3001.5501
最后在service实现类里添加代码插入数据到MySQL
三. 实现
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzg.common.demo.country.ProvinceCityAndCounty;
import com.fzg.common.enums.ResponseWrapper;
import com.fzg.entity.ChinaProvinceCity;
import com.fzg.mapper.ChinaProvinceCityMapper;
import com.fzg.service.ChinaProvinceCityService;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
/**
* <p>
* 服务实现类
* </p>
*
* @author fzg
* @since 2022-11-21
*/
@Service
public class ChinaProvinceCityServiceImpl extends ServiceImpl<ChinaProvinceCityMapper, ChinaProvinceCity> implements ChinaProvinceCityService {
/**
* 将全国所有省份,市区,县数据导入到MySQL数据库中
*
* @return
*/
@Override
public ResponseWrapper importProvinceCityCounty() {
// 开始计时
Timestamp startTime = new Timestamp(System.currentTimeMillis());
// 调用这个类的静态方法把文本内容处理成 数据list
ArrayList<HashMap<String, Object>> list = ProvinceCityAndCounty.getChineseAllProvincesAndCityCounty("E:\\学习\\项目\\vue-vant-app\\myBlog\\myCaveolae_server\\src\\main\\java\\com\\fzg\\common\\demo\\country\\provinceCityAndCounty.txt");
String res = "";
if (list != null){
int count = 0;
for (HashMap<String, Object> map : list) {
boolean save = this.save(new ChinaProvinceCity() {{
setChineseName(map.get("chineseName").toString());
setEnglishName(map.get("englishName").toString());
setCode(map.get("code").toString());
setParentCode(map.get("parentCode").toString());
}});
if (save){
count++;
}
}
res = "新增了" + count + "数据";
}else {
res = "数据为空";
}
Timestamp endTime = new Timestamp(System.currentTimeMillis());
// 用时
long second = (endTime.getTime() - startTime.getTime()) / 1000;
String result = "用时" + second + "秒; ";
return ResponseWrapper.markCustomSuccess(result + res);
}
}
获取数据list,调用了另一个类的静态方法, 上面已经给出了
表结构:根据表结构(表名和字段)生成代码框架
最后在controller层调用service实现类方法去postman测试
或者直接controller里使用get请求,直接在浏览器输入网址就可
四. 结果
select count(1)
from china_province_city;
标签:map,java,log,com,list,中文名,英文名,import,省份 From: https://www.cnblogs.com/Fantasyfzg/p/16911562.html数据库表数据:4115条数据,我们国家竟然这么多个城市,区县。