In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to implement the menu tree with Java recursion. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Pom file
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE com.example demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework .boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.1 mysql mysql-connector-java runtime com.github.pagehelper pagehelper-spring-boot-starter 1.3.0 org.projectlombok lombok true Com.baomidou mybatis-plus-boot-starter 3.4.2 org.springframework.boot spring-boot-maven-plugin
Application.yaml file
Spring: datasource: url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&serverTimezone=CTT username: root password: 2020 driver-class-name: com.mysql.cj.jdbc.Driverpagehelper: helperDialect: mysql reasonable: true # modify the default value # mybatis-plus configuration mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl typeAliasesPackage: com.qcby.entity mapperLocations: classpath:mapper/*.xml # global configuration Set id self-increment = > global-config: db-config: id-type: auto
The database table is designed as follows
SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0Mutual-Table structure for menu-- DROP TABLE IF EXISTS `menu` CREATE TABLE `menu` (`id` bigint (20) NOT NULL AUTO_INCREMENT COMMENT 'primary key id', `name` varchar (255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT' name', `pid` bigint (20) DEFAULT NULL COMMENT 'parent id', PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact -Records of menu-- INSERT INTO `menu` VALUES (1, 'main menu 1, 0); INSERT INTO `menu` VALUES (2,' main menu 2, 0); INSERT INTO `menu` VALUES (3, 'main menu 3, 0) INSERT INTO `menu` VALUES (4, 'menu 1.1, 1); INSERT INTO `menu` VALUES (5,' menu 1.2, 1); INSERT INTO `menu` VALUES (6, 'menu 1.1.1, 4); INSERT INTO `menu` VALUES (7,' menu 2.1, 2); INSERT INTO `menu` VALUES (8, 'menu 2.2, 2); INSERT INTO `menu` VALUES (9,' menu 1.1.2, 4); SET FOREIGN_KEY_CHECKS = 1
Menu class
Package com.qcby.entity;import lombok.Data;import java.util.List;@Data//lombok implements simplified get, set, tostring methods public class Menu {/ / menu id private String id; / / menu name private String name; / / parent menu id private String pid; / / child menu private List menuChildren;}
Xml file
SELECT * FROM menu WHERE pid=# {pid} SELECT * FROM menu SELECT * FROM menu where Piddance = 0
Mapper layer
Package com.qcby.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.qcby.entity.Menu;import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapperpublic interface MenuMapper extends BaseMapper {List selectByPid (Integer pid); List selectAll (); List selectAllNotBase ();}
Service layer
Package com.qcby.service;import com.baomidou.mybatisplus.extension.service.IService;import com.qcby.entity.Menu;import java.util.List;public interface MenuService extends IService {List selectByPid (Integer pid); List selectAll (); List selectAllNotBase ();}
ServiceImpl
Package com.qcby.service.Impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import com.qcby.entity.Menu;import com.qcby.mapper.MenuMapper;import com.qcby.service.MenuService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class MenuServiceImpl extends ServiceImpl implements MenuService {@ Autowired private MenuMapper menuMapper; @ Override public List selectByPid (Integer pid) {return menuMapper.selectByPid (pid) } @ Override public List selectAll () {return menuMapper.selectAll ();} @ Override public List selectAllNotBase () {return menuMapper.selectAllNotBase ();}}
Controller layer
Package com.qcby.controller;import com.baomidou.mybatisplus.core.toolkit.StringUtils;import com.qcby.entity.Menu;import com.qcby.mapper.MenuMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;import java.util.List;@RestController@RequestMapping ("menu") public class MenuController {@ Autowired private MenuMapper menuMapper @ RequestMapping ("/ getMenuTree") public List getMenuTree () {List menusBase = menuMapper.selectByPid (0); List menuLNotBase = menuMapper.selectAllNotBase (); for (Menu menu: menusBase) {List menus = iterateMenus (menuLNotBase, menu.getId ()); menu.setMenuChildren (menus);} return menusBase } / * Multi-level menu query method * @ param menuVoList does not contain the menu collection of the highest-level menu * @ param pid parent class id * @ return * / public List iterateMenus (List menuVoList,String pid) {List result = new ArrayList (); for (Menu menu: menuVoList) {/ / get the menu id String menuid = menu.getId () / / get the parent id String parentid of the menu = menu.getPid (); if (StringUtils.isNotBlank (parentid)) {if (parentid.equals (pid)) {/ / Recursive query the submenu of the current submenu List iterateMenu = iterateMenus (menuVoList,menuid); menu.setMenuChildren (iterateMenu) Result.add (menu);} return result;}}
Result display
This is the end of the article on "how to realize menu tree with Java recursion". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.