How to use Java to realize a simple version of multi-level menu function
Editor to share with you how to use Java to achieve a simple version of the multi-level menu function, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Body 1, first of all, the design of the database DROP TABLE IF EXISTS `t _ roomy`; CREATE TABLE `t_ roomy` (`id` int (11) NOT NULL AUTO_INCREMENT COMMENT 'identifies the unique id', of the directory `parent_ id` int (11) DEFAULT NULL COMMENT' storage parent directory represents the first-level directory', `name` varchar (255) COLLATE utf8_bin DEFAULT NULL COMMENT 'directory name, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;2.java back-end code
① corresponds to the entity class of the database
Public class Category {private int id; private int parentId; private String name;}
② returns to the entity class at the front end / / write a constructor with parameters so that we can inject Category data into it.
Public class CategoryTwo extends Category {private List categorieTwos; public CategoryTwo () {} / / this parametric construction method makes it convenient for us to assign values to attributes, that is, to assign values of member variables of categoy to itself public CategoryTwo (Category category) {super (category.getId (), category.getParentId (), category.getName ());}}
The concrete realization of ③
/ / Multi-level directory public void findCategoryAll () {/ / query all directory data List categorys = categoryMapper.findCategoryAll (null); / / complete this by yourself / / store all first-level directories List categoryTows = new ArrayList () / / find the first-level directory, that is, the directory for (Category category: categorys) {if (category.getParentId () = = 0) {CategoryTwo categoryTwo = new CategoryTwo (category); categoryTows.add (categoryTwo) / / store it in the first-level directory collection}} / / use recursion to find out the List of all first-level directories. Here, I pack the specific implementation into the findSubCategory () method with categoryTows = findSubCategory (categorys,categoryTows); / / finally, it is done, and the data is returned to the front end as required. System.out.println (categoryTows);} / find the subdirectories of all directories in List public List findSubCategory (List categorys,List categoryTwos) {for (CategoryTwo categoryTwo:categoryTwos) {/ / find their own subdirectories and store them in List to List categoryTwos2 = new ArrayList () For (Category category:categorys) {/ / if its own id = is equal to the parentId of a directory, then the directory is its own child directory if (categoryTwo.getId () = = category.getParentId ()) {categoryTwos2.add (new CategoryTwo (category) }} / / after all child directories are found, call back the findSubCategory () method to let the child directory find its own child List categoryTwos3 = findSubCategory (categorys,categoryTwos2); / / categoryTwos3 the real List and add it to its own categoryTwo.setCategorieTwos (categoryTwos3);} return categoryTwos;// returns the injected object}
The core is the method of callback. As long as we figure this out, it will be very easy.
Another implementation method import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map; public class MenuTest {/ * * @ param args * / public static void main (String [] args) {MenuTest menuTest = new MenuTest (); List menuList = initMenu (); / / initialization menu Map menuMap = menuTest.rebuildMenus (menuList) is attached / / restructure the menu menuTest.showMenu (menuMap); / / print the menu} / * restructure the menu to construct the required structure * @ param menuList * @ return * / private Map rebuildMenus (List menuList) {List rootMenuList=new ArrayList (); / / this List is used to store the root menu for (int j = 0; j < menuList.size ()) Null==parentId +) {Map menu = (Map) menuList.get (j); String parentId = menu.get ("parentId"). ToString (); if (null==parentId | | ".equals (parentId)) {rootMenuList.add (menu);}} menuList.removeAll (rootMenuList); / / remove HashMap menuMap = new HashMap () from menuList For (int j = 0; j < rootMenuList.size (); jacks +) {/ / traverses the root menu, each traversing to the last submenu (leaf node) Map menu = (Map) rootMenuList.get (j); menu = this.pase (menu, menuList); menuMap.put (menu.get ("id"), menu);} return menuMap } / * Recursive construction menu Map * @ param menu * @ param menuList * @ return * / private Map pase (Map menu, List menuList) {for (int j = 0; j < menuList.size (); Jake +) {Map menu2 = (Map) menuList.get (j) If (menu2.get ("parentId"). ToString (). Equals (menu.get ("id"). ToString ()) {if (menu.get ("children") = = null) {menu.put ("children", new ArrayList ());} ((List) menu.get ("children")) .add (menu2) Pase (menu2, menuList);}} return menu;} / * * print menu * @ param menuMap * / private void showMenu (Map menuMap) {Iterator it = menuMap.keySet () .iterator (); while (it.hasNext ()) {Map menu = (Map) menuMap.get (it.next ()) System.out.println (menu.get ("id") + "-" + menu.get ("name")); this.buildChildMenu (menu);}} / * Construction submenu * @ param menu * / private void buildChildMenu (Map menu) {List childList = (List) menu.get ("children") If (nullable childhood childList) {for (int iTunes 0polii)