Wechat public number: xiao Lei when the efforts to a certain extent, lucky from and you accidentally meet
1, an overview of the
Background management system is a necessary project for Web development, so it is necessary to make a comb of some basic functions of the system. Today’s content is how to recursively generate the tree structure menu. The renderings are as follows:
The menu is divided into the first level menu, the second level menu, the complex menu and even contains the third level menu and the fourth level menu. These data in the table are all in a menu table.
The core design of the table is as follows:
field | The name of the | note |
---|---|---|
id | int | The primary key id |
parent_id | int | The first-level menu is 0, and the other menus point to the id of the superclass |
menu_path | varchar | Resource path |
sort | int | The sorting |
menu_button | tinyint | Whether to display or hide as a directory |
2. Code implementation
The thinking is relatively unified:
1, first find all the data of the menu data
2. Then find all the first-level menus and splice their subtrees together
3. Subtrees also have submenus, which need to be recursively spliced.
To start with, we need to add a field to the list of submenus in the original entity class, annotating it to indicate that the field is not in the database.
2.1 Basic edition implementation
@override public List<CategoryEntity> categoryEntities = listWithTree() {// Override public List<CategoryEntity> categoryEntities = categoryDao.selectList(null); List<ChildMenuVO> childMenuVOS =treeMenu(categoryEntities); return childMenuVOS; } private List<ChildMenuVO> treeMenu(List<CategoryEntity> categoryEntities) { List<ChildMenuVO> childMenuVOS=new ArrayList<>(); for (int i = 0; i < categoryEntities.size(); i++) { CategoryEntity categoryEntity=categoryEntities.get(i); // If the parent id is 0, then the subsequent method if(0! =categoryEntity.getParentCid().intValue()){ continue; } ChildMenuVO childMenuVO = new ChildMenuVO(); BeanUtils.copyProperties(categoryEntity,childMenuVO); / / recursive judgment If the current menu has child nodes, if have, set childMenuVO. SetChild (subMenu (categoryEntity categoryEntities)); childMenuVOS.add(childMenuVO); } return childMenuVOS; } private List<ChildMenuVO> subMenu(CategoryEntity parentMenu, List<CategoryEntity> categoryEntities) { List<ChildMenuVO> childMenuVOS =new ArrayList<>(); For (int I = 0; i < categoryEntities.size(); i++) { CategoryEntity subCategory=categoryEntities.get(i); if(subCategory.getParentCid().equals(parentMenu.getCatId())){ ChildMenuVO childMenuVO = new ChildMenuVO(); BeanUtils.copyProperties(subCategory,childMenuVO); childMenuVO.setChild(subMenu(subCategory,categoryEntities)); childMenuVOS.add(childMenuVO); } } return childMenuVOS; }Copy the code
2.2 Advanced version implementation
Public List<CategoryEntity> listWithTree() {public List<CategoryEntity> categoryEntities = categoryDao.selectList(null); List<CategoryEntity> level1Menus = categoryentities.stream ().filter(CategoryEntity -> {CategoryEntity -> {CategoryEntity = categoryentities.stream ().filter(CategoryEntity -> {CategoryEntity -> { return categoryEntity.getParentCid() == 0; }).map(menu->{ menu.setChildren(getChildrens(menu,categoryEntities)); return menu; }).sorted((menu1,menu2)->{ return (menu1.getSort()==null? 0:menu1.getSort())-(menu2.getSort()==null? 0:menu2.getSort()); }) .collect(Collectors.toList()); return level1Menus; Private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){ List<CategoryEntity> children = all.stream().filter(categoryEntity -> { return categoryEntity.getParentCid().equals(root.getCatId()); }).map(categoryEntity -> { categoryEntity.setChildren(getChildrens(categoryEntity,all)); return categoryEntity; }).sorted((menu1,menu2)->{ return (menu1.getSort()==null? 0:menu1.getSort())-(menu2.getSort()==null? 0:menu2.getSort()); }).collect(Collectors.toList()); return children; }Copy the code
I have to say that Java8 stream is extremely concise code, and I will record java8 articles later, the effect is also achieved, a stream stream is a bit like SQL, you can do sorting, filtering, judging, and other concise operations.
Today’s share here, interested can pay attention, the follow-up more content dry goods please look forward to.