三级分类for循环(只能三级分类,逻辑看的有点晕)
//查询所有分类
List<SugClassificationParam> queryAllList = counterQueryAllclsMapper.queryAllList();
// 组装分类
List<SugClassificationParam> returnList = new ArrayList<>();
// 2.2 提供Map进行缓存,子分类可以快速找到父分类
Map<Integer,SugClassificationParam> cacheMap = new HashMap<>();
for (SugClassificationParam sugClassificationParam : queryAllList){
if(sugClassificationParam.getParentId() == 0){
returnList.add( sugClassificationParam );
}
//将自己添加到map缓存中,方便子分类查找自己
cacheMap.put(sugClassificationParam.getId(), sugClassificationParam);
//子分类从缓存map获取父分类
SugClassificationParam parentList = cacheMap.get(sugClassificationParam.getParentId());
if(parentList != null) {
parentList.getChildren().add(sugClassificationParam);
}
}
递归生成树形菜单 (创建公共类提取出来)
递归生成一个如图的菜单,编写两个类数据模型Menu、和创建树形的MenuTree。通过以下过程实现:
1.首先从菜单数据中获取所有根节点。
2.为根节点建立次级子树并拼接上。
3.递归为子节点建立次级子树并接上,直至为末端节点拼接上空的“树”。
首先,编写数据模型Menu。每条菜单有自己的id、父节点parentId、菜单名称text、菜单还拥有次级菜单children。(注意建库时的数据类型要统一)
-- 创建级联分类表
DROP TABLE IF EXISTS `t_counter_type`;
CREATE TABLE `t_counter_type` (
`id` int(32) NOT NULL AUTO_INCREMENT COMMENT '分类id',
`cls_name` varchar(32) DEFAULT NULL COMMENT '分类名称',
`cls_descrption` varchar(128) DEFAULT NULL COMMENT '分类说明',
`parent_id` int(32) DEFAULT NULL COMMENT '父级id',
`is_praent` int(32) DEFAULT NULL COMMENT '创建者',
`createTime` datetime DEFAULT NULL COMMENT '创建时间',
`updateUser` varchar(32) DEFAULT NULL COMMENT '修改人',
`updateTime` datetime DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='建议三级分类表';
1 import java.util.List; 2 3 public class Menu { 4 private String id; 5 private String parentId; 6 private String text; 7 private String url; 8 private String yxbz; 9 private List<Menu> children; 10 public Menu(String id,String parentId,String text,String url,String yxbz) { 11 this.id=id; 12 this.parentId=parentId; 13 this.text=text; 14 this.url=url; 15 this.yxbz=yxbz; 16 } 17 /*省略get\set*/ 18 }
创建树形结构的类MenuTree。方法getRootNode获取所有根节点,方法builTree将根节点汇总创建树形结构,buildChilTree为节点建立次级树并拼接上当前树,递归调用buildChilTree不断为当前树开枝散叶直至找不到新的子树。完成递归,获取树形结构。
1 import java.util.ArrayList; 2 import java.util.List; 3 4 public class MenuTree { 5 private List<Menu> menuList = new ArrayList<Menu>(); 6 public MenuTree(List<Menu> menuList) { 7 this.menuList=menuList; 8 } 9 10 //建立树形结构 11 public List<Menu> builTree(){ 12 List<Menu> treeMenus =new ArrayList<Menu>(); 13 for(Menu menuNode : getRootNode()) { 14 menuNode=buildChilTree(menuNode); 15 treeMenus.add(menuNode); 16 } 17 return treeMenus; 18 } 19 20 //递归,建立子树形结构 21 private Menu buildChilTree(Menu pNode){ 22 List<Menu> chilMenus =new ArrayList<Menu>(); 23 for(Menu menuNode : menuList) { 24 if(menuNode.getParentId().equals(pNode.getId())) { 25 chilMenus.add(buildChilTree(menuNode)); 26 } 27 } 28 pNode.setChildren(chilMenus); 29 return pNode; 30 } 31 32 //获取根节点 33 private List<Menu> getRootNode() { 34 List<Menu> rootMenuLists =new ArrayList<Menu>(); 35 for(Menu menuNode : menuList) { 36 if(menuNode.getParentId().equals("0")) { 37 rootMenuLists.add(menuNode); 38 } 39 } 40 return rootMenuLists; 41 } 42 }
最后,插入一些数据试试效果。得到的json就可以生成图一菜单了。
1 import java.util.ArrayList; 2 import java.util.List; 3 import com.alibaba.fastjson.JSON; 4 5 public class Hello { 6 public static void main(String []args) { 7 List<Menu> menuList= new ArrayList<Menu>(); 8 /*插入一些数据*/ 9 menuList.add(new Menu("GN001D000","0","系统管理","/admin","Y")); 10 menuList.add(new Menu("GN001D100","GN001D000","权限管理","/admin","Y")); 11 menuList.add(new Menu("GN001D110","GN001D100","密码修改","/admin","Y")); 12 menuList.add(new Menu("GN001D120","GN001D100","新加用户","/admin","Y")); 13 menuList.add(new Menu("GN001D200","GN001D000","系统监控","/admin","Y")); 14 menuList.add(new Menu("GN001D210","GN001D200","在线用户","/admin","Y")); 15 menuList.add(new Menu("GN002D000","0","订阅区","/admin","Y")); 16 menuList.add(new Menu("GN003D000","0","未知领域","/admin","Y")); 17 /*让我们创建树*/ 18 MenuTree menuTree =new MenuTree(menuList); 19 menuList=menuTree.builTree(); 20 /*转为json看看效果*/ 21 String jsonOutput= JSON.toJSONString(menuList); 22 System.out.println(jsonOutput); 23 } 24 }
数据库访问数据的形式
@Service
@Transactional
public class CounterQueryAllclsServiceImpl implements CounterQueryAllclsService {
private static final Logger logger = LoggerFactory.getLogger(CounterQueryAllclsServiceImpl.class);
@Autowired
CounterQueryAllclsMapper counterQueryAllclsMapper;
@Override
public List<SugClassificationParam> queryAllList() {
//第二种方式递归循环
List<SugClassificationParam> clsList = new ArrayList<>();
//查询所有分类
List<SugClassificationParam> queryAllList = counterQueryAllclsMapper.queryAllList();
//创建树
ClsTree clsTree = new ClsTree(queryAllList);
clsList=clsTree.builTree();
//打印json
logger.info("返回结果>>>>" + JSON.toJSONString(clsList));
return clsList;
}