在日常一線開發過程中,總有列表轉樹
的需求,幾乎是專案的標配,比方說做多級選單、多級目錄、多級分類等,有沒有一種通用且跨專案的解決方式呢?幫助廣大技術朋友給業務瘦身,提高開發效率。
本文將基於Java8的Lambda 表示式和Stream等知識,使用TreeUtils
工具類實現一行程式碼完成列表轉樹這一通用型需求。本文有配套視訊,傳送門直達。
需要說明的是,本TreeUtils工具類已進行三版優化,能夠勝任生產場景絕大多數列表轉樹的。
本座標地址包含TreeUtils
工具類方法,已經發布到Maven中央倉庫,建議使用最新的版本號,本文釋出時版本是1.5.8.3
,若有最新版本,建議使用最新版本。
學習原始碼的朋友,原始碼直通車
<dependency>
<groupId>xin.altitude.cms</groupId>
<artifactId>ucode-cms-common</artifactId>
<version>1.5.8.3</version>
</dependency>
DO是存取資料庫實體類,屬於前期準備素材。
@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;
}
}
}
建立BO資料模型用於構造屬性結點
@Data
@NoArgsConstructor
public class CategoryBo {
private String id;
private String name;
private String parentId;
private String remark;
/**
* 孩子結點 名字任意取
*/
private List<CategoryBo> childList;
/**
* 實現Category 轉 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();
}
}
}
在服務層呼叫TreeUtils工具類,一行程式碼完成轉換。
/**
* 獲取DO中指定的屬性 並構造成樹
* @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);
}
原始列表資料
{
"code": 200,
"msg": "操作成功",
"data": [
{
"id": "310000",
"name": "電子商務",
"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"
}
]
}
樹狀層次資料
{
"code": 200,
"msg": "操作成功",
"data": [
{
"id": "310000",
"name": "電子商務",
"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": []
}
]
}
]
}
通過一個小小的高頻業務場景,抽離業務共性,遮蔽業務細節,實現了通用型業務邏輯的編碼。
希望廣大技術朋友在完成日常開發工作的前提下,能夠進一步打磨程式碼,感受程式設計之美。