java实现省市区转换成树形结构
作者:super小苏打 时间:2022-11-27 03:43:37
本文实例为大家分享了java实现省市区转换成树形结构的具体代码,供大家参考,具体内容如下
前言:为什我想写这篇博客呢?第一方面是记录,另一方面是分享。
1.创建数据库表
1.建表语句
CREATE TABLE `area` (
`AREA_CODE` varchar(16) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '区域ID',
`AREA_NAME` varchar(512) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '区域名称',
`SUPERIOR_CODE` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '父级代码',
`AREA_LEVEL` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '级别',
`AREA_SORT` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '排序',
`REMARK` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注',
`DELETE_FLAG` varchar(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '删除标识',
`CREATED_BY` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '创建人',
`CREATION_TIME` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '创建时间',
`UPDATED_BY` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '更新人',
`UPDATE_TIME` timestamp(0) NOT NULL DEFAULT '2020-01-01 00:00:00' COMMENT '更新时间',
PRIMARY KEY (`AREA_CODE`) USING BTREE,
UNIQUE INDEX `UK_T_PUB_AREA_AREA_CODE`(`AREA_CODE`) USING BTREE,
INDEX `IDX_T_PUB_AREA_AREA_LEVEL`(`AREA_LEVEL`) USING BTREE,
INDEX `IDX_T_PUB_AREA_SUPERIOR_CODE`(`SUPERIOR_CODE`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '区域表' ROW_FORMAT = DYNAMIC;
SET FOREIGN_KEY_CHECKS = 1;
建表完成的table样式:
2.创建java实体
package org.labib.pojo;
import java.util.Date;
public class Area {
//区域ID
private String areaCode;
//区域名称
private String areaName;
//父级代码
private String superiorCode;
//级别
private String areaLevel;
//排序
private String areaSort;
//备注
private String remark;
//删除标识
private String deleteFlag;
//创建人
private String createdBy;
//创建时间
private Date creationTime;
//更新人
private String updatedBy;
//更新时间
private Date updateTime;
public String getAreaCode() {
return areaCode;
}
public void setAreaCode(String areaCode) {
this.areaCode = areaCode == null ? null : areaCode.trim();
}
public String getAreaName() {
return areaName;
}
public void setAreaName(String areaName) {
this.areaName = areaName == null ? null : areaName.trim();
}
public String getSuperiorCode() {
return superiorCode;
}
public void setSuperiorCode(String superiorCode) {
this.superiorCode = superiorCode == null ? null : superiorCode.trim();
}
public String getAreaLevel() {
return areaLevel;
}
public void setAreaLevel(String areaLevel) {
this.areaLevel = areaLevel == null ? null : areaLevel.trim();
}
public String getAreaSort() {
return areaSort;
}
public void setAreaSort(String areaSort) {
this.areaSort = areaSort == null ? null : areaSort.trim();
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark == null ? null : remark.trim();
}
public String getDeleteFlag() {
return deleteFlag;
}
public void setDeleteFlag(String deleteFlag) {
this.deleteFlag = deleteFlag == null ? null : deleteFlag.trim();
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy == null ? null : createdBy.trim();
}
public Date getCreationTime() {
return creationTime;
}
public void setCreationTime(Date creationTime) {
this.creationTime = creationTime;
}
public String getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(String updatedBy) {
this.updatedBy = updatedBy == null ? null : updatedBy.trim();
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
3.创建mapper
package org.labib.mapper;
import org.labib.pojo.Area;
import java.util.List;
import java.util.Map;
public interface AreaMapper {
int deleteByPrimaryKey(String areaCode);
int insert(Area record);
int insertSelective(Area record);
Area selectByPrimaryKey(String areaCode);
int updateByPrimaryKeySelective(Area record);
int updateByPrimaryKey(Area record);
List<Area> queryAreaAll();
List<Map<String, String>> queryMap();
}
4.创建service
@Service("AreaService")
public class AreaService {
@Autowired
private AreaMapper areaMapper;
//修建
public String queryArea(){
List<Area> mapList = areaMapper.queryAreaAll();
JSONArray result = ListToTree.buildTree(JSONArray.parseArray(JSON.toJSONString(mapList)),"areaCode","superiorCode","children");
return result.toJSONString();
} //修建
public String queryAreaCode(){
List<Map<String, String>> mapList = areaMapper.queryMap();
JSONArray result = ListToTree.buildTree(JSONArray.parseArray(JSON.toJSONString(mapList)),"value","superiorCode","children");
return result.toJSONString();
}
}
并附上转换成树形结构的工具类:
public class ListToTree {
public static JSONArray buildTree(JSONArray arr, String id, String pid, String Chirden) {
//新建一个JSONArray来接收组装成树形结构的返回值
JSONArray jsonArray = new JSONArray();
//新建一个JSONObject对象
JSONObject hash = new JSONObject();
//将数组转换为object格式
for (int i = 0; i < arr.size(); i++) {
//获取当前的JSON对象
JSONObject json = (JSONObject) arr.get(i);
//把当前id作为键,当前JSON对象作为值 put回hash这个Object对象中
//这里的put方法类似于map的put方法
hash.put(json.getString(id), json);
}
//遍历结果集
for (int j = 0; j < arr.size(); j++) {
//单条记录
JSONObject aVal = (JSONObject) arr.get(j);
//在hash中取出key为单条记录中pid的值
String pidStr = "";
//如果父级id不等于null
if (aVal.get(pid) != null) {
pidStr = aVal.get(pid).toString();
}
//从hash这个对象中获取父级对象 parent
JSONObject hashParent = (JSONObject) hash.get(pidStr);
//如果记录的pid存在,则说明它有父节点,将她添加到孩子节点的集合中
if (hashParent != null) {
//检查是否有child属性
if (hashParent.get(Chirden) != null) {
//有子节点 则先将子节点取出
JSONArray children = (JSONArray) hashParent.get(Chirden);
//然后把当前这个对象放进子节点之中
children.add(aVal);
//最后把子节点在放回父节点之中
hashParent.put(Chirden, children);
} else {
//无子节点 则新建一个子节点
JSONArray children = new JSONArray();
//然后再把当前对象放进去
children.add(aVal);
//最后在放回父节点之中
hashParent.put(Chirden, children);
}
} else {
jsonArray.add(aVal);
}
}
return jsonArray;
}
}
5.创建controller
@RestController
public class AreaController {
@Resource
private AreaService areaService;
@RequestMapping(value = "/area",method = RequestMethod.POST)
public String queryArea() {
return areaService.queryArea();
}
@RequestMapping(value = "/area1",method = RequestMethod.POST)
public String queryAreaDemo() {
return areaService.queryAreaCode();
}
}
6.接口测试
测试结果:
来源:https://blog.csdn.net/qq_45256805/article/details/119565617
标签:java,省市区,树形结构
0
投稿
猜你喜欢
C#调用微信接口的相关代码
2022-01-23 03:01:02
Java通过HttpClient进行HTTP请求的代码详解
2022-06-04 09:50:20
java中request常用方法小结
2022-09-13 06:19:45
VSCode 搭建 Arm 远程调试环境的步骤详解
2023-06-27 08:54:36
C#中POST接口formdata传参模板的记录
2022-03-30 01:58:50
C# SendMail发送邮件功能实现
2022-05-02 19:40:43
SpringBoot 读取yml文件的多种方式汇总
2023-08-03 13:10:43
Flutter集成高德地图并添加自定义Maker的实践
2022-07-11 21:32:49
Spring boot事件监听实现过程解析
2022-08-29 13:46:02
Spring的Aware接口你知道多少
2023-01-19 12:44:20
深入解析C#中的泛型类与泛型接口
2023-04-30 02:35:39
SpringBoot+Elasticsearch实现数据搜索的方法详解
2023-04-17 02:45:46
Kotlin挂起函数原理示例剖析
2023-11-10 21:52:13
一文给你通俗易懂的讲解Java异常
2021-12-20 14:40:56
深入讲解spring boot中servlet的启动过程与原理
2022-08-19 00:18:40
mybatis中<if>标签bool值类型为false判断方法
2023-11-20 11:28:33
SVN报错:Error Updating changes:svn:E155037的解决方案
2023-06-11 07:27:11
tcp、udp、ip协议分析_动力节点Java学院整理
2023-05-17 18:00:17
SpringCloud之Feign示例详解
2023-03-16 06:14:32
Java中JMM与volatile关键字的学习
2022-03-24 00:12:00