一、序言
在日常一線開發(fā)過程中所袁,總有列表轉(zhuǎn)樹
的需求盏档,幾乎是項(xiàng)目的標(biāo)配,比方說做多級菜單纲熏、多級目錄妆丘、多級分類等,有沒有一種通用且跨項(xiàng)目的解決方式呢局劲?幫助廣大技術(shù)朋友給業(yè)務(wù)瘦身勺拣,提高開發(fā)效率。
本文將基于Java8的Lambda 表達(dá)式和Stream等知識鱼填,使用TreeUtils
工具類實(shí)現(xiàn)一行代碼完成列表轉(zhuǎn)樹這一通用型需求药有。本文有配套視頻,傳送門直達(dá)苹丸。
需要說明的是愤惰,本TreeUtils工具類已進(jìn)行三版優(yōu)化,能夠勝任生產(chǎn)場景絕大多數(shù)列表轉(zhuǎn)樹的赘理。
二宦言、實(shí)戰(zhàn)編碼
1、引入坐標(biāo)
本坐標(biāo)地址包含TreeUtils
工具類方法商模,已經(jīng)發(fā)布到Maven中央倉庫奠旺,建議使用最新的版本號,本文發(fā)布時版本是1.5.8.3
施流,若有最新版本响疚,建議使用最新版本。
學(xué)習(xí)源碼的朋友瞪醋,源碼直通車
<dependency>
<groupId>xin.altitude.cms</groupId>
<artifactId>ucode-cms-common</artifactId>
<version>1.5.8.3</version>
</dependency>
2忿晕、編寫DO
DO是訪問數(shù)據(jù)庫實(shí)體類,屬于前期準(zhǔn)備素材银受。
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "bu_category3")
public class Category {
private static final long serialVersionUID = 1L;
@TableId(type = IdType.AUTO)
private String id;
private String name;
private String parentId;
private String remark;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
public Category(Category category) {
if (Objects.nonNull(category)) {
this.id = category.id;
this.name = category.name;
this.parentId = category.parentId;
this.remark = category.remark;
this.createTime = category.createTime;
this.updateTime = category.updateTime;
}
}
}
3践盼、創(chuàng)建BO
創(chuàng)建BO數(shù)據(jù)模型用于構(gòu)造屬性結(jié)點(diǎn)
@Data
@NoArgsConstructor
public class CategoryBo {
private String id;
private String name;
private String parentId;
private String remark;
/**
* 孩子結(jié)點(diǎn) 名字任意取
*/
private List<CategoryBo> childList;
/**
* 實(shí)現(xiàn)Category 轉(zhuǎn) CategoryBo
*
* @param category
*/
public CategoryBo(Category category) {
if (Objects.nonNull(category)) {
this.id = category.getId();
this.name = category.getName();
this.parentId = category.getParentId();
this.remark = category.getRemark();
}
}
}
3、調(diào)用TreeUtils工具類
在服務(wù)層調(diào)用TreeUtils工具類蚓土,一行代碼完成轉(zhuǎn)換宏侍。
/**
* 獲取DO中指定的屬性 并構(gòu)造成樹
* @return
*/
public List<CategoryBo> selectCategory4() {
List<Category> categoryList = this.list();
List<CategoryBo> categoryBoList = EntityUtils.toList(categoryList, CategoryBo::new);
return TreeUtils.createNodeDetail(categoryBoList, "000000", CategoryBo::getId, CategoryBo::getParentId, CategoryBo::getChildList);
}
4、效果展示
原始列表數(shù)據(jù)
{
"code": 200,
"msg": "操作成功",
"data": [
{
"id": "310000",
"name": "電子商務(wù)",
"parentId": "000000",
"remark": "EEEEE",
"createTime": null,
"updateTime": "2021-11-21 11:22:40"
},
{
"id": "310100",
"name": "大宗商品",
"parentId": "310000",
"remark": "JJJJJ",
"createTime": null,
"updateTime": "2021-11-21 11:22:52"
},
{
"id": "310101",
"name": "大宗商品綜合",
"parentId": "310100",
"remark": "IIIII",
"createTime": null,
"updateTime": "2021-11-21 11:22:58"
},
{
"id": "310102",
"name": "鋼鐵類電商",
"parentId": "310100",
"remark": "OOOOO",
"createTime": null,
"updateTime": "2021-11-21 11:23:02"
},
{
"id": "310200",
"name": "綜合電商",
"parentId": "310000",
"remark": "NNNNN",
"createTime": null,
"updateTime": "2021-11-21 11:23:07"
}
]
}
樹狀層次數(shù)據(jù)
{
"code": 200,
"msg": "操作成功",
"data": [
{
"id": "310000",
"name": "電子商務(wù)",
"parentId": "000000",
"remark": "EEEEE",
"childList": [
{
"id": "310100",
"name": "大宗商品",
"parentId": "310000",
"remark": "JJJJJ",
"childList": [
{
"id": "310101",
"name": "大宗商品綜合",
"parentId": "310100",
"remark": "IIIII",
"childList": []
},
{
"id": "310102",
"name": "鋼鐵類電商",
"parentId": "310100",
"remark": "OOOOO",
"childList": []
}
]
},
{
"id": "310200",
"name": "綜合電商",
"parentId": "310000",
"remark": "NNNNN",
"childList": []
}
]
}
]
}
三蜀漆、小結(jié)
通過一個小小的高頻業(yè)務(wù)場景,抽離業(yè)務(wù)共性咱旱,屏蔽業(yè)務(wù)細(xì)節(jié)确丢,實(shí)現(xiàn)了通用型業(yè)務(wù)邏輯的編碼绷耍。
希望廣大技術(shù)朋友在完成日常開發(fā)工作的前提下,能夠進(jìn)一步打磨代碼鲜侥,感受編程之美褂始。