目录
3.【主要代码进行封装】感觉上面代码不太活,复用性太差,于是我进行了简单的封装,首先要把进行封装树形结构的数据读取出来用以下实体类接受,实体类可以根据自身需求进行更改
5.因为方法我用静态关键字修饰了,可以直接用类名调,把查询的数据传进去就行了
JSON封装树形结构
我这边参考网上一些案例,分别写了两种方法,一种是直接编写业务代码,一种我是封装了一下,感觉封装后灵活度比较高,用起来也方便
一、为什么要封装数据结构?
示例:封装数据结构是为了前端更好的对接,如果所有数据都直接返回,那么前端是不是要做很多处理。
二、主要代码
1.代码编写
代码如下(示例):采用了lambda表达式操作集合,采用了递归算法去查询,我根据自己的理解对代码进行了注释
public List<CityData> threeJsonList() {
//创建空集合用于返回
List<CityData> dataList = new ArrayList<>();
//得到所有地区的信息
List<CityData> cityDataList = cityDataMapper.selectList(Wrappers.lambdaQuery(CityData.class));
cityDataList.forEach(v -> {
//循环得到父ID为0的例如 北京的父级ID=0
if (v.getParentId() == BigInteger.ZERO.longValue()) {
//把当前循环的父级ID为0的对象,以及所有地区信息的集合传进去
dataList.add(findChildren(v, cityDataList));
}
});
return dataList;
}
private CityData findChildren(CityData cityData, List<CityData> cityDataList) {
cityDataList.forEach(v -> {
//循环所有地区的信息,当前传来的对象是最外层的地区,例如北京
//循环判断哪些数据的父级ID等于传来的对象的ID
//匹配成功的数据就是传来的父级ID的子集
if (cityData.getId().equals(v.getParentId())) {
if (CollectionUtils.isEmpty(cityData.getChildList())) {
//这边判断传来对象为空的话初始化一下,不然会报错
cityData.setChildList(new ArrayList<>());
}
//匹配成功添加数据,重点在这里,再调一遍方法进行循环
//例如最开始进来的时候传的是北京的对象
//循环判断是北京下面的子集的话再重新调用这个方法一直查,直到查至最后一级
cityData.getChildList().add(findChildren(v, cityDataList));
}
});
return cityData;
}
2.实体类
package com.ztcloud.common.model.sf;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
/**
* ---------------------------
* 城市编码信息 (CityData)
* ---------------------------
* 作者:
* 时间: 2022-07-15 09:07:11
* ---------------------------
*/
@Data
@ApiModel("城市编码信息实体")
public class CityData {
/** */
@TableId(value = "id", type=IdType.AUTO)
@ApiModelProperty("")
private Long id;
/** 更新时间 */
@ApiModelProperty("更新时间")
private java.util.Date updateTime;
/** 名称 */
@ApiModelProperty("名称")
private String name;
/** 编码 */
@ApiModelProperty("编码")
private String code;
/** 层级 */
@ApiModelProperty("层级")
private Integer level;
/** 父级id */
@ApiModelProperty("父级id")
private Long parentId;
//子集
@TableField(exist = false)
List<CityData> childList;
}
3.【主要代码进行封装】感觉上面代码不太活,复用性太差,于是我进行了简单的封装,首先要把进行封装树形结构的数据读取出来用以下实体类接受,实体类可以根据自身需求进行更改
package com.ztcloud.admin.utils;
import lombok.Data;
import java.util.List;
/**
* @Author zyq
* @Date 2022-07-15 14:07
*/
@Data
public class ThreeJson {
private Long id;
/** 父ID */
private Long parentId;
/** 名称 */
private String name;
/** 子集 */
private List<ThreeJson> children;
}
4.封装的工具类
package com.ztcloud.admin.utils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.formula.functions.T;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
/**
* @Author zyq
* @Date 2022-07-15 13:30
*/
public class ThreeJsonUtils {
public static List<ThreeJson> threeJson(List<ThreeJson> addressList) {
List<ThreeJson> resultJsonList = new ArrayList<>();
addressList.forEach(v -> {
if (v.getParentId() == BigInteger.ZERO.longValue()) {
resultJsonList.add(findChildren(v, addressList));
}
});
return resultJsonList;
}
private static ThreeJson findChildren(ThreeJson threeJson, List<ThreeJson> addressList) {
addressList.forEach(v -> {
if (threeJson.getId().equals(v.getParentId())) {
if (CollectionUtils.isEmpty(threeJson.getChildren())) {
threeJson.setChildren(new ArrayList<>());
}
threeJson.getChildren().add(findChildren(v, addressList));
}
});
return threeJson;
}
}
5.因为方法我用静态关键字修饰了,可以直接用类名调,把查询的数据传进去就行了
public List<ThreeJson> threeJsonList() {
List<ThreeJson> threeJsons = cityDataMapper.selectAddressList();
List<ThreeJson> threeJsons1 = ThreeJsonUtils.threeJson(threeJsons);
return threeJsons1;
}
6.最终的数据展示
返回格式(示例):我这边只展示了以北京市及下面所有区域的数据结构展示
[
{
"id": 1,
"updateTime": "2021-05-25T16:29:25.000+0800",
"name": "北京市",
"code": null,
"level": 1,
"parentId": 0,
"childList": [
{
"id": 2,
"updateTime": "2021-05-25T16:29:25.000+0800",
"name": "市辖区",
"code": "110100000000",
"level": 2,
"parentId": 1,
"childList": [
{
"id": 3,
"updateTime": "2021-05-25T16:29:25.000+0800",
"name": "东城区",
"code": "110101000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 4,
"updateTime": "2021-05-25T16:29:25.000+0800",
"name": "西城区",
"code": "110102000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 5,
"updateTime": "2021-05-25T16:29:26.000+0800",
"name": "朝阳区",
"code": "110105000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 6,
"updateTime": "2021-05-25T16:29:26.000+0800",
"name": "丰台区",
"code": "110106000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 7,
"updateTime": "2021-05-25T16:29:26.000+0800",
"name": "石景山区",
"code": "110107000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 8,
"updateTime": "2021-05-25T16:29:26.000+0800",
"name": "海淀区",
"code": "110108000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 9,
"updateTime": "2021-05-25T16:29:26.000+0800",
"name": "门头沟区",
"code": "110109000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 10,
"updateTime": "2021-05-25T16:29:26.000+0800",
"name": "房山区",
"code": "110111000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 11,
"updateTime": "2021-05-25T16:29:26.000+0800",
"name": "通州区",
"code": "110112000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 12,
"updateTime": "2021-05-25T16:29:26.000+0800",
"name": "顺义区",
"code": "110113000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 13,
"updateTime": "2021-05-25T16:29:27.000+0800",
"name": "昌平区",
"code": "110114000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 14,
"updateTime": "2021-05-25T16:29:27.000+0800",
"name": "大兴区",
"code": "110115000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 15,
"updateTime": "2021-05-25T16:29:27.000+0800",
"name": "怀柔区",
"code": "110116000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 16,
"updateTime": "2021-05-25T16:29:27.000+0800",
"name": "平谷区",
"code": "110117000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 17,
"updateTime": "2021-05-25T16:29:27.000+0800",
"name": "密云区",
"code": "110118000000",
"level": 3,
"parentId": 2,
"childList": null
},
{
"id": 18,
"updateTime": "2021-05-25T16:29:27.000+0800",
"name": "延庆区",
"code": "110119000000",
"level": 3,
"parentId": 2,
"childList": null
}
]
}
]
}]
该处使用的url网络请求的数据。
总结
以上我觉得封装后的更友好一点,代码复用性也比较强,本文简单介绍了一下如何封装树形结构的JSON,如果数据量较多的话这样写效率不太好,如果有更好的方案希望大家一起参考。