Java如何使用遞歸查詢多級樹形結(jié)構(gòu)數(shù)據(jù)(多級菜單)
更新時間:2024年07月17日 09:40:16 作者:小徐敲java
這篇文章主要介紹了Java如何使用遞歸查詢多級樹形結(jié)構(gòu)數(shù)據(jù)(多級菜單),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
1:數(shù)據(jù)庫表
字段resource_id為自增主鍵,其中最頂級的菜單的父類ID是用-1表示的
下面我們就來查詢這張表
2:實體類
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)建時間*/ @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") @ApiModelProperty(value = "創(chuàng)建時間") private Date createTime; /**更新時間*/ @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") @ApiModelProperty(value = "更新時間") 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()); //新建一個用于接收數(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) { //如果父類主鍵等于傳過來實體類的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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用ffmpeg和mencoder實現(xiàn)視頻轉(zhuǎn)碼
這篇文章主要為大家詳細(xì)介紹了Java使用ffmpeg和mencoder實現(xiàn)視頻轉(zhuǎn)碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12SpringBoot和MybatisPlus實現(xiàn)通用Controller示例
本文主要介紹了SpringBoot和MybatisPlus實現(xiàn)通用Controller示例,只需創(chuàng)建實體類和mapper接口,就可以實現(xiàn)單表的增刪改查操作,具有一定的參考價值,感興趣的可以了解一下2025-03-03SpringBoot項目中日志管理與調(diào)優(yōu)指南
在 Spring Boot 開發(fā)過程中,日志管理是開發(fā)者必須掌握的重要技能之一,合理的日志配置不僅能幫助開發(fā)者追蹤應(yīng)用程序的執(zhí)行流程、定位問題,還能提升應(yīng)用程序的可維護(hù)性,本文將詳細(xì)探討 Spring Boot 項目中日志管理的常見問題、解決方案以及最佳實踐2024-10-10SpringBoot淺析緩存機制之Ehcache?2.x應(yīng)用
EhCache?是一個純Java的進(jìn)程內(nèi)緩存框架,具有快速、精干等特點。它是Hibernate中的默認(rèn)緩存框架。Ehcache已經(jīng)發(fā)布了3.1版本。但是本文的講解基于2.x版本2022-08-08PostMan傳@RequestParam修飾的數(shù)組方式
這篇文章主要介紹了PostMan傳@RequestParam修飾的數(shù)組方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08