Java如何使用遞歸查詢多級樹形結(jié)構(gòu)數(shù)據(jù)(多級菜單)
1:數(shù)據(jù)庫表
字段resource_id為自增主鍵,其中最頂級的菜單的父類ID是用-1表示的
下面我們就來查詢這張表

2:實(shí)體類
childMenu是用于裝子類數(shù)據(jù)的;
@TableField(exist = false)表示該屬性不為數(shù)據(jù)庫表字段,但是必須使用
package org.jeecg.modules.system.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
import java.util.List;
@Data
@TableName("sys_depart_Manage")
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="部門對象", description="部門對象")
public class SysDepartManage {
/**部門名稱*/
@ApiModelProperty(value = "部門名稱")
private String departName;
/**資源id*/
@ApiModelProperty(value = "資源id")
@TableId(type = IdType.AUTO)
private Integer resourceId;
/**父id*/
@ApiModelProperty(value = "父id")
private String parentId;
/**是否展示*/
@ApiModelProperty(value = "是否展示")
private Integer showInput;
/**創(chuàng)建時(shí)間*/
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "創(chuàng)建時(shí)間")
private Date createTime;
/**更新時(shí)間*/
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "更新時(shí)間")
private Date updateTime;
/** 子類菜單數(shù)據(jù) */
@TableField(exist = false)
private List<SysDepartManage> childMenu;
}3:controller層代碼
@Resource
private ISysDepartManageService sysDepartManageService;
/**
* 部門樹形查詢
* @param parentID
* @return
*/
@GetMapping("/menu")
@ApiOperation(value="部門樹形查詢", notes="部門樹形查詢")
public List<SysDepartManage> findMenu(@RequestParam String parentID) {
return sysDepartManageService.findMenu(parentID);
}4:service層代碼
package org.jeecg.modules.system.service;
import com.baomidou.mybatisplus.extension.service.IService;
import org.jeecg.modules.system.entity.SysDepartManage;
import java.util.List;
import java.util.Map;
public interface ISysDepartManageService extends IService<SysDepartManage> {
List<SysDepartManage> findMenu(String parentID );
}
5:方法
serviceimpl層代碼(多次查詢數(shù)據(jù)庫,耗性能)
package org.jeecg.modules.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.system.entity.SysDepartManage;
import org.jeecg.modules.system.mapper.SysDepartManageMapper;
import org.jeecg.modules.system.service.ISysDepartManageService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* @Description: 部門角色
* @Author: jeecg-boot
* @Date: 2020-02-12
* @Version: V1.0
*/
@Service
public class SysDepartManageServiceImpl extends ServiceImpl<SysDepartManageMapper, SysDepartManage> implements ISysDepartManageService {
@Override
public List<SysDepartManage> findMenu(String parentID) {
QueryWrapper queryWrapper = new QueryWrapper<>();
//查詢條件
queryWrapper.eq("parent_id", parentID);
List<SysDepartManage> list = baseMapper.selectList(queryWrapper);
for (SysDepartManage departManage : list) {
//遞歸子類數(shù)據(jù)
departManage.setChildMenu(findMenu(departManage.getResourceId().toString()));
}
return list;
}
}
6:方法2:serviceimpl層代碼(只查詢數(shù)據(jù)庫1次)
package org.jeecg.modules.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.system.entity.SysDepartManage;
import org.jeecg.modules.system.mapper.SysDepartManageMapper;
import org.jeecg.modules.system.service.ISysDepartManageService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class SysDepartManageServiceImpl extends ServiceImpl<SysDepartManageMapper, SysDepartManage> implements ISysDepartManageService {
@Override
public List<SysDepartManage> findMenu(String parentID) {
//數(shù)據(jù)查詢
List<SysDepartManage> list = baseMapper.selectList(new QueryWrapper());
//新建一個(gè)用于接收數(shù)據(jù)的list
List<SysDepartManage> resultList = new ArrayList<>();
for (SysDepartManage result : list) {
if (result.getParentId() == 0) {
//調(diào)用方法給子類添加數(shù)據(jù)
resultList.add(getMenuTree(result, list));
}
}
return resultList;
}
private SysDepartManage getMenuTree(SysDepartManage result, List<SysDepartManage> list) {
for (SysDepartManage sysDepartManage : list) {
//如果父類主鍵等于傳過來實(shí)體類的ID
if (sysDepartManage.getParentId().equals(result.getResourceId())) {
if (result.getChildMenu() == null) {
result.setChildMenu(new ArrayList<>());
}
// 遞歸調(diào)用
result.getChildMenu().add(getMenuTree(sysDepartManage, list));
}
}
return result;
}
}
7:mapper層代碼
package org.jeecg.modules.system.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.jeecg.modules.system.entity.SysDepartManage;
import java.util.List;
import java.util.Map;
public interface SysDepartManageMapper extends BaseMapper<SysDepartManage> {
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Apache?POI和SpringBoot實(shí)現(xiàn)Excel文件上傳和解析功能
在現(xiàn)代企業(yè)應(yīng)用開發(fā)中,數(shù)據(jù)的導(dǎo)入和導(dǎo)出是一項(xiàng)常見且重要的功能需求,Excel?作為一種廣泛使用的電子表格工具,常常被用來存儲(chǔ)和展示數(shù)據(jù),下面我們來看看如何使用Apache?POI和SpringBoot實(shí)現(xiàn)Excel文件上傳和解析功能吧2025-01-01
java微信掃碼支付模式一線下支付功能實(shí)現(xiàn)
本篇文章主要介紹了JAVA微信掃碼支付模式一線下支付功能實(shí)現(xiàn),具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11
SpringBoot結(jié)合Docker進(jìn)行容器化處理指南
在當(dāng)今快速發(fā)展的軟件工程領(lǐng)域,Spring Boot 和 Docker 已經(jīng)成為現(xiàn)代 Java 開發(fā)者的必備工具,本文將深入講解如何將一個(gè) Spring Boot 應(yīng)用進(jìn)行容器化處理,希望對大家有所幫助2025-07-07
SpringBoot整合Dubbo zookeeper過程解析
這篇文章主要介紹了SpringBoot整合Dubbo zookeeper過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
java使用Hex編碼解碼實(shí)現(xiàn)Aes加密解密功能示例
這篇文章主要介紹了java使用Hex編碼解碼實(shí)現(xiàn)Aes加密解密功能,結(jié)合完整實(shí)例形式分析了Aes加密解密功能的定義與使用方法,需要的朋友可以參考下2017-01-01
Java設(shè)置Map過期時(shí)間的的幾種方法舉例詳解
本文詳細(xì)介紹了Java中使用輕量級緩存組件ExpiringMap以及Guava的LoadingCache緩存機(jī)制,ExpiringMap提供了Map自動(dòng)過期、監(jiān)聽事件等功能,而LoadingCache提供了緩存回收、數(shù)據(jù)加載等高級功能,兩者為Java項(xiàng)目提供了有效的數(shù)據(jù)管理和緩存解決方案,需要的朋友可以參考下2024-10-10

