Java reads the menu from the database, recursively builds the menu tree

Source: Internet
Author: User

First look at the menu.

According to this, we define the menu class

public Span class= "Hljs-keyword" >class menu {//menu ID private String    Id //menu name private String name; //parent menu ID private String parentid; //menu URL private String URL; //menu icon private String icon; //menu order private int order; //submenu private list<menu> Childmenus; //... Omit getter and setter methods and ToString Method}             

We define the database based on this class and insert the menu data

DROPTABLEIFEXISTS' Jrbac_menu ';CREATETABLE' Jrbac_menu ' (' ID 'varchar32)NotNULL COMMENT' PRIMARY key id,uuid32 bit ',' Name 'varchar64)NotNULL COMMENT' Login user name ',' parent_id 'varchar32)DEFAULTNULL COMMENT' Parent menu ID ',' URL 'varchar64)DEFAULTNULL COMMENT' Access address ',' Icon 'varchar32)DEFAULTNULL COMMENT' Menu Icon ',' Order ' tinyint (4)DEFAULT' 0 ' COMMENT' Menu Order ',PRIMARYKEY (' ID ')) engine=innodbDEFAULT Charset=utf8 comment=' Menu table ';-- ------------------------------Records of Jrbac_menu-- ----------------------------INSERTInto' Jrbac_menu 'VALUES (' 1 ',' Forms ',Null' Forms.html ',' FA Fa-edit ',' 0 ');INSERTInto' Jrbac_menu 'VALUES (' 2 ',' UI Elements ',Null‘‘,' FA Fa-wrench ',' 1 ');INSERTInto' Jrbac_menu 'VALUES (' 3 ',' Buttons ',' 2 ',' Buttons.html ',‘‘,' 0 ');INSERTInto' Jrbac_menu 'VALUES (' 4 ',' Icons ',' 2 ',' Icons.html ',Null' 1 ');INSERTInto' Jrbac_menu 'VALUES (' 5 ',' Multi-level Dropdown ',‘‘,‘‘,' FA Fa-sitemap ',' 2 ');INSERTInto' Jrbac_menu 'VALUES (' 6 ',' Second level Item ',' 5 ',' Second.html ',null,  ' 0 '); insert INTO  Jrbac_menu ' values ( ' 7 ',  ' third level ',  ' 5 ', null, ",  ' 1 '); insert INTO  Jrbac_menu ' values ( ' 8 ',  ' third level Item ',  ' 7 ',  ' third.html ', Span class= "Hljs-keyword" >null,  ' 0 ');         

In order to demonstrate, we have not finished the expandable, just insert a few data can be effective.

Test method and Recursive method

PrivateFinal Gson Gson =New Gsonbuilder (). disablehtmlescaping (). Create ();@TestPublicvoidTestquerymenulist () {Raw data list<menu> Rootmenu = Menudao.querymenulist (NULL);View Resultsfor (menu menu:rootmenu) {System.out.println (menu);}The final result list<menu> Menulist =New Arraylist<menu> ();Find all the first level menusfor (int i =0; I < rootmenu.size (); i++) {The first level menu has no parentidif (Stringutils.isblank (Rootmenu.get (i). Getparentid ())) {Menulist.add (Rootmenu.get (i));}}To set a submenu for a one-level menu, Getchild is called recursivelyfor (Menu menu:menulist) {Menu.setchildmenus (Getchild (Menu.getid (), Rootmenu));} Map<string,object> Jsonmap =New Hashmap<> (); Jsonmap.put ("Menu", menulist); System.out.println (Gson.tojson (Jsonmap));}/** * Recursive Lookup submenu * *@param ID * Current Menu ID *@param rootmenu * List to find *@return */Private list<menu>Getchild (String ID, list<menu> rootmenu) {Sub-menu List<menu> childlist =new arraylist<> (); for (menu menu:rootmenu) {//traverse all nodes, comparing the parent menu ID to the passed-in ID if (Stringutils.isnotblank (Menu.getparentid ())) {if ( Menu.getparentid (). Equals (ID)) {childlist.add (menu);}} } //the submenu of the handle menu cycle again for (menu menu:childlist) {//no URL submenu also has submenu if (Stringutils.isblank (Menu.geturl ())) {//Recursive Menu.setchildmenus (Getchild (Menu.getid (), rootmenu)); }} //recursive exit condition if (childlist.size () = = 0) {return null;} return childlist;}             

Menudao.querymenulist (null); The result of a lookup is one piece of data

Meudao

package com.jrbac.dao;import java.util.List;import com.jrbac.entity.LoginUser;import com.jrbac.entity.Menu;public interface MenuDao { /** * 查找用户的菜单 * @param loginUser * @return */ public List<Menu> queryMenuList(LoginUser loginUser);}

MyBatis

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.jrbac.dao.MenuDao"> <select id="queryMenuList" resultType="Menu"> SELECT id,`name`,parent_id,url,icon,`order` FROM jrbac_menu ORDER BY `order` ASC </select></mapper>

Test the running result of the program and compare the output JSON to the formatted one

Java reads the menu from the database, recursively builds the menu tree

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.