springboot構(gòu)造樹(shù)形結(jié)構(gòu)數(shù)據(jù)并查詢的方法
因?yàn)轫?xiàng)目需要,頁(yè)面上需要樹(shù)形結(jié)構(gòu)的數(shù)據(jù)進(jìn)行展示(類似下圖這樣),因此需要后端返回相應(yīng)格式的數(shù)據(jù)。

不說(shuō)廢話,直接開(kāi)干!??!
我這里用的是springboot+mybatis-plus+mysql,示例的接口是查詢一級(jí)權(quán)限以及二級(jí)權(quán)限、三級(jí)權(quán)限整個(gè)權(quán)限樹(shù)…
下面是導(dǎo)入的maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--數(shù)據(jù)庫(kù)連接-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis增強(qiáng)工具-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.0.6</version>
</dependency>
下面是實(shí)體類Permission
@Data
public class Permission implements Serializable {
@TableId
private String permissionId;
@NotNull(message = "權(quán)限名稱不能為空")
private String permissionName;
/**
* 權(quán)限標(biāo)識(shí)
*/
@NotNull(message = "權(quán)限標(biāo)識(shí)不能為空")
private String permissionCode;
/**
* 父菜單ID,如果是-1就表示是一級(jí)權(quán)限菜單。
*/
@NotBlank(message = "父菜單ID不能為空")
private String parentId;
/**
* 前端URL訪問(wèn)接口路徑
*/
private String path;
/**
* 排序值
*/
private Integer sort;
/**
* 創(chuàng)建時(shí)間
*/
private LocalDateTime createTime;
/**
* 更新時(shí)間
*/
private LocalDateTime updateTime;
/**
* 0--正常 1--刪除
*/
private String delFlag;
public Permission() {
this.permissionId = IdUtil.simpleUUID();
}
樹(shù)形結(jié)點(diǎn)類
@Data
public class TreeNode {
protected String id;
protected String parentId;
protected List<TreeNode> children = new ArrayList<TreeNode>();
protected boolean hasChildren;
public void addTreeNode(TreeNode node){
children.add(node);
}
}
樹(shù)形結(jié)點(diǎn)詳細(xì)信息類
@Data
@EqualsAndHashCode(callSuper = true)
public class PermissionTree extends TreeNode implements Serializable {
private String permissionName;
private String permissionCode;
private String path;
private Integer sort;
private String label;
private boolean hasChildren;
public PermissionTree() {
}
}
構(gòu)建樹(shù)形結(jié)點(diǎn)工具類(關(guān)鍵),在這里我用@UtilityClass注解就表示這個(gè)類中的方法都是靜態(tài)方法:
@UtilityClass
public class TreeUtil {
public <T extends TreeNode> List<T> build(List<T> treeNodes, String root) {
List<T> trees = new ArrayList<>();
for (T treeNode : treeNodes) {
if (root.equals(treeNode.getParentId())) {
trees.add(treeNode);
}
for (T node : treeNodes) {
if (node.getParentId().equals(treeNode.getId())) {
treeNode.addTreeNode(node);
treeNode.setHasChildren(true);
}
}
}
return trees;
}
/**
* 通過(guò)permission創(chuàng)建樹(shù)形節(jié)點(diǎn)
*
* @param permissionList
* @param root
* @return
*/
public List<PermissionTree> buildTree(List<Permission> permissionList, String root) {
System.out.println(Arrays.toString(permissionList.toArray()));
List<PermissionTree> treeNodeList = new ArrayList<>();
PermissionTree treeNode = null;
for (Permission permission : permissionList) {
treeNode = new PermissionTree();
treeNode.setId(permission.getPermissionId());
treeNode.setPermissionName(permission.getPermissionName());
treeNode.setPath(permission.getPath());
treeNode.setSort(permission.getSort());
treeNode.setParentId(permission.getParentId());
treeNode.setLabel(permission.getPermissionName());
treeNode.setHasChildren(false);
treeNodeList.add(treeNode);
}
return TreeUtil.build(treeNodeList, root);
}
}
響應(yīng)消息主體類
/**
* 響應(yīng)信息主體
*
* @param <T>
*/
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {
private static final long serialVersionUID = 1L;
private int code;
private String msg;
private T data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public static <T> R<T> ok() {
return restResult(null, CommonConstants.SUCCESS, CommonConstants.MSG_SUCCESS);
}
public static <T> R<T> ok(T data) {
return restResult(data, CommonConstants.SUCCESS, CommonConstants.MSG_SUCCESS);
}
public static <T> R<T> ok(T data, String msg) {
return restResult(data, CommonConstants.SUCCESS, msg);
}
public static <T> R<T> failed() {
return restResult(null, CommonConstants.FAIL, null);
}
public static <T> R<T> failed(String msg) {
return restResult(null, CommonConstants.FAIL, msg);
}
public static <T> R<T> failed(T data) {
return restResult(data, CommonConstants.FAIL, null);
}
public static <T> R<T> failed(T data, String msg) {
return restResult(data, CommonConstants.FAIL, msg);
}
private static <T> R<T> restResult(T data, int code, String msg) {
R<T> apiResult = new R<>();
apiResult.setCode(code);
apiResult.setData(data);
apiResult.setMsg(msg);
return apiResult;
}
}
數(shù)據(jù)查詢接口mapper類
@Mapper
public interface PermissionMapper extends BaseMapper<Permission>{
}
數(shù)據(jù)邏輯處理業(yè)務(wù)接口
public interface PermissionService extends IService<Permission> {
/**
* 構(gòu)建權(quán)限樹(shù)
*
* @param lazy
* @param parentId
* @return
*/
List<PermissionTree> treePermission(boolean lazy, String parentId);
}
數(shù)據(jù)邏輯處理業(yè)務(wù)接口實(shí)現(xiàn)類
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
/**
* 構(gòu)建權(quán)限樹(shù):1、不是懶加載情況,查詢?nèi)?
* 2、是懶加載,根據(jù)parentId查詢
*
* @param lazy
* @param parentId
* @return
*/
@Override
public List<PermissionTree> treePermission(boolean lazy, String parentId) {
if (!lazy) {
return TreeUtil.buildTree(
baseMapper.selectList(Wrappers.<Permission>lambdaQuery().orderByAsc(Permission::getSort)),
CommonConstants.PERMISSION_ROOT_ID);
}
String parent = parentId == null ? CommonConstants.PERMISSION_ROOT_ID : parentId;
return TreeUtil.buildTree(
baseMapper.selectList(Wrappers.<Permission>lambdaQuery().eq(Permission::getParentId, parent).orderByAsc(Permission::getSort)), parent
);
}
}
查詢權(quán)限樹(shù)請(qǐng)求接口類
@RestController
@RequestMapping("/permission")
public class PermissionController {
@Autowire
private PermissionService permissionService;
/**
* 查詢權(quán)限列表,并以樹(shù)狀結(jié)構(gòu)顯示
*
* @param lazy 懶加載: false時(shí), parentId這個(gè)參數(shù)失效, 加載所有的權(quán)限; true時(shí), 根據(jù)parentId加載
* @param parentId
* @return
*/
@RequestMapping(value = "/getTree", method = RequestMethod.GET)
public R getTree(boolean lazy, String parentId) {
return R.ok(permissionService.treePermission(lazy, parentId));
}
}
表中測(cè)試數(shù)據(jù)如下(注意它的parent_id)

測(cè)試一:不是懶加載,查詢整個(gè)權(quán)限樹(shù)。 結(jié)果如下。

{
"code": 0,
"msg": "SUCCESS",
"data": [
{
"id": "1",
"parentId": "-1",
"children": [
{
"id": "2",
"parentId": "1",
"children": [
{
"id": "3",
"parentId": "2",
"children": [],
"hasChildren": false,
"permissionName": "update",
"permissionCode": null,
"path": null,
"sort": 3,
"label": "update",
"owned": false
},
{
"id": "4",
"parentId": "2",
"children": [],
"hasChildren": false,
"permissionName": "insert_role",
"permissionCode": null,
"path": null,
"sort": 4,
"label": "insert_role",
"owned": false
}
],
"hasChildren": true,
"permissionName": "delete",
"permissionCode": null,
"path": null,
"sort": 2,
"label": "delete",
"owned": false
}
],
"hasChildren": true,
"permissionName": "add",
"permissionCode": null,
"path": null,
"sort": 1,
"label": "add",
"owned": false
},
{
"id": "5",
"parentId": "-1",
"children": [],
"hasChildren": false,
"permissionName": "role:saveRole",
"permissionCode": null,
"path": "/role/saveRole",
"sort": 5,
"label": "role:saveRole",
"owned": false
}
]
}
測(cè)試二:是懶加載,根據(jù)parent_id查詢當(dāng)前分支。 結(jié)果如下。

{
"code": 0,
"msg": "SUCCESS",
"data": [
{
"id": "3",
"parentId": "2",
"children": [],
"hasChildren": false,
"permissionName": "update",
"permissionCode": null,
"path": null,
"sort": 3,
"label": "update",
"owned": false
},
{
"id": "4",
"parentId": "2",
"children": [],
"hasChildren": false,
"permissionName": "insert_role",
"permissionCode": null,
"path": null,
"sort": 4,
"label": "insert_role",
"owned": false
}
]
}
到此這篇關(guān)于springboot構(gòu)造樹(shù)形結(jié)構(gòu)數(shù)據(jù)并查詢的方法的文章就介紹到這了,更多相關(guān)springboot 樹(shù)形結(jié)構(gòu)并查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud Eureka自我保護(hù)機(jī)制原理解析
這篇文章主要介紹了SpringCloud Eureka自我保護(hù)機(jī)制原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Java動(dòng)態(tài)線程池插件dynamic-tp集成過(guò)程淺析
這篇文章主要介紹了Java動(dòng)態(tài)線程池插件dynamic-tp集成過(guò)程,dynamic-tp是一個(gè)輕量級(jí)的動(dòng)態(tài)線程池插件,它是一個(gè)基于配置中心的動(dòng)態(tài)線程池,線程池的參數(shù)可以通過(guò)配置中心配置進(jìn)行動(dòng)態(tài)的修改2023-03-03
springboot項(xiàng)目main函數(shù)啟動(dòng)的操作
這篇文章主要介紹了springboot項(xiàng)目main函數(shù)啟動(dòng)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Java基礎(chǔ)之finally語(yǔ)句與return語(yǔ)句詳解
這篇文章主要介紹了Java基礎(chǔ)之finally語(yǔ)句與return語(yǔ)句詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
MyBatis-Plus自定義通用的方法實(shí)現(xiàn)
MP自帶的條件構(gòu)造器雖然很強(qiáng)大,有時(shí)候也避免不了寫稍微復(fù)雜一點(diǎn)業(yè)務(wù)的sql,本文主要介紹了MyBatis-Plus自定義通用的方法實(shí)現(xiàn),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
Springboot整合MongoDB的Docker開(kāi)發(fā)教程全解
這篇文章主要介紹了Springboot整合MongoDB的Docker開(kāi)發(fā),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2020-07-07

