@[TOC](List Stream(),map(),filter(),sorted(),Collectors()方法封装成Tree树型结构)
话不多说,直接上代码。
@Data //需要在maven仓库引用lombok依赖
public class ShopProductCategory extends Model<ShopProductCategory> {
private static final long serialVersionUID = 1L;
@TableId
private Long id;
/**
* 商品名称
*/
private String categoryName;
/**
* 商品编号
*/
private String categoryCode;
/**
* 父分类id
*/
private Long parentId;
/**
* 商品分类层级
*/
private Integer categoryLevel;
/**
* 商品分类状态 0 显示 1 不显示
*/
private Integer categoryStatus;
/**
* 创建时间
*/
private LocalDateTime createTime;
/**
* 创建人
*/
private Long createBy;
/**
* 修改时间
*/
private LocalDateTime updateTime;
/**
* 修改人
*/
private Long updateBy;
/**
* 分类图片
*/
private String mallPicturePath;
/**
* 业务删除(0不删除1删除)
*/
@TableLogic
private Integer delFlag;
/**
* 备注信息
*/
private String remarks;
/**
* 排序
*/
private Integer sort;
@TableField(exist = false) //标识在数据库中不存在
private List<ShopProductCategory> children;
}
public List<ShopProductCategory> treeAll() {
//使用mybatis-plus查询商品数据
List<ShopProductCategory> list = baseMapper.selectList(null);
//找出父类节点
List<ShopProductCategory> level1 = list.stream().filter((productCategory) -> {
//通过filter过滤当前父类节点是0的数据
return productCategory.getParentId() == 0;
}
).map((menu) -> {
//使用map方法对children进行业务封装。map主要是用于处理数据
menu.setChildren(getchildrens(menu, list));
return menu;
}).sorted((menu1, menu2) -> {
//使用sorted将获得父类节点是0的数据经行排序
return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
}
).collect(Collectors.toList());//使用Collectors.toList()封装成一个List集合
return level1;
}
private List<ShopProductCategory> getchildrens(ShopProductCategory shopProductCategory, List<ShopProductCategory> all) {
List<ShopProductCategory> childrenList = all.stream().filter((productCategory) -> {
return productCategory.getParentId() == Long.parseLong(shopProductCategory.getId().toString() );
}).sorted((menu1, menu2) -> {
return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
}
).collect(Collectors.toList());
return childrenList;
}
{
"id": 40,
"categoryName": "口红",
"categoryCode": "kouhong",
"parentId": 0,
"categoryLevel": 1,
"categoryStatus": 0,
"createTime": "2020-06-29 23:08:42",
"createBy": 2,
"updateTime": "2020-07-01 14:42:22",
"updateBy": 4,
"mallPicturePath": "ee4_diao.jpg",
"delFlag": 0,
"remarks": "口红",
"sort": 1,
"children": [
{
"id": 42,
"categoryName": "迪奥",
"categoryCode": "dior",
"parentId": 40,
"categoryLevel": 2,
"categoryStatus": 0,
"createTime": "2020-06-29 23:09:38",
"createBy": 2,
"updateTime": "2020-07-02 10:36:53",
"updateBy": 4,
"mallPicturePath": "a37f9f2fcf_diao.jpg",
"delFlag": 0,
"remarks": "dior品牌redis测试",
"sort": 1,
"children": null
},
{
"id": 43,
"categoryName": "YSL",
"categoryCode": "YSL",
"parentId": 40,
"categoryLevel": 2,
"categoryStatus": 0,
"createTime": "2020-06-29 23:09:58",
"createBy": 2,
"updateTime": "2020-07-04 16:27:12",
"updateBy": 4,
"mallPicturePath": "9d17d7183bd2_YSL.jpg",
"delFlag": 0,
"remarks": "QQQ",
"sort": 2,
"children": null
}
]
}