实现一次加载多级所有的菜单

/**
	 * 得到菜单的json串
	 * 构造多叉树来实现一次加载多级所有的菜单
	 */
	@SuppressWarnings({ "rawtypes" })
	@Override
	public String getJsonstr() throws Exception {
		BsMenuExample example = new BsMenuExample();
		example.setOrderByClause(" menulevel  desc ,menuorder  desc ");
		List<BsMenu> dataList= bsMenuDAO.selectByExample(example);
		// 节点列表(散列表,用于临时存储节点对象)
		HashMap<Integer,MenuNode> nodeList = new HashMap<Integer,MenuNode>();
		// 根节点
		MenuNode root = null;
		// 根据结果集构造节点列表(存入散列表)
		for (BsMenu dataRecord: dataList) {
			MenuNode node = new MenuNode();
			node.setId(dataRecord.getId());
			BeanUtils.copyProperties(dataRecord, node);  
			nodeList.put(node.getId().intValue(), node);
		}
		// 构造无序的多叉树
		Set entrySet = nodeList.entrySet();
		for (Iterator it = entrySet.iterator(); it.hasNext();) {
			MenuNode node = (MenuNode) ((Map.Entry) it.next()).getValue();
			if (node.getMenuparent() == null || node.getMenuparent().equals("")) {
				root = node;
			} else {
				((MenuNode) nodeList.get(node.getMenuparent())).getChildren().add(node);
			}
		}
		String json =JSON.toJSONString(root);
		return "["+json+"]";
	}
 
文章出自:http://www.oschina.net/code/snippet_142385_34771
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值