JAVA--コードセグメント(親ノードに基づいてツリーリストをクエリーする再帰メソッド)

1152 ワード

private List> getList(long parentId) {
		//      
		TbItemCatExample example = new TbItemCatExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List list = itemCatMapper.selectByExample(example);
		List resultList = new ArrayList<>();
		//    
		int count = 0;
		for (TbItemCat tbItemCat : list) {
			//      
			if (tbItemCat.getIsParent()) {
				ItemCatNode node = new ItemCatNode();
				node.setUrl("/products/" + tbItemCat.getId() + ".html");
				//          
				if (parentId == 0) {
					node.setName(""+tbItemCat.getName()+"");
				} else {
					node.setName(tbItemCat.getName());
				}
				node.setItems(getList(tbItemCat.getId()));
				resultList.add(node);
			} else {
				String node = "/products/"+tbItemCat.getId()+".html|" + tbItemCat.getName();
				resultList.add(node);
			}
			count++;
			//      ,  14   
			if (parentId == 0 && count >= 14) {
				break;
			}
		}
		return resultList;
	}