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)文章
Java使用ffmpeg和mencoder實(shí)現(xiàn)視頻轉(zhuǎn)碼
這篇文章主要為大家詳細(xì)介紹了Java使用ffmpeg和mencoder實(shí)現(xiàn)視頻轉(zhuǎn)碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
SpringBoot和MybatisPlus實(shí)現(xiàn)通用Controller示例
本文主要介紹了SpringBoot和MybatisPlus實(shí)現(xiàn)通用Controller示例,只需創(chuàng)建實(shí)體類和mapper接口,就可以實(shí)現(xiàn)單表的增刪改查操作,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03
SpringBoot項(xiàng)目中日志管理與調(diào)優(yōu)指南
在 Spring Boot 開發(fā)過程中,日志管理是開發(fā)者必須掌握的重要技能之一,合理的日志配置不僅能幫助開發(fā)者追蹤應(yīng)用程序的執(zhí)行流程、定位問題,還能提升應(yīng)用程序的可維護(hù)性,本文將詳細(xì)探討 Spring Boot 項(xiàng)目中日志管理的常見問題、解決方案以及最佳實(shí)踐2024-10-10
SpringBoot淺析緩存機(jī)制之Ehcache?2.x應(yīng)用
EhCache?是一個(gè)純Java的進(jìn)程內(nèi)緩存框架,具有快速、精干等特點(diǎn)。它是Hibernate中的默認(rèn)緩存框架。Ehcache已經(jīng)發(fā)布了3.1版本。但是本文的講解基于2.x版本2022-08-08
Java 線程池詳解及創(chuàng)建簡單實(shí)例
這篇文章主要介紹了Java 線程池詳解及創(chuàng)建簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
PostMan傳@RequestParam修飾的數(shù)組方式
這篇文章主要介紹了PostMan傳@RequestParam修飾的數(shù)組方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
MyBatis項(xiàng)目的創(chuàng)建和增刪查改操作詳解
這篇文章主要介紹了MyBatis項(xiàng)目的創(chuàng)建和增刪查改操作,文中通過代碼示例和圖文結(jié)合的方式給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-11-11

