商品无限级菜单设计总结

本文总结了商品无限级菜单的设计,首先介绍了商品类型表,接着详细讨论了使用Java如何查询并获取商品类别的所有子节点,包括递归方法和循环获取分类树的两种方式,并针对效率问题进行了优化分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、商品类型表


二、java实现查询出商品类别的所有子节点

1.1 递归(效率太低)

/**
	 * 通过递归获取分类树(数据量大时,效率低)
	 * 
	 * @param pid
	 * @return
	 */
	public List<ProductType> getAllProductTypesByRes(Long id) {
		List<ProductType> children = mapper.getChildren(id);//通过商品类型id查询商品类型的子节点
		for (ProductType productType : children) {
		List<ProductType> afterChildren = getAllProductTypesByRes(productType.getId());//递归查孙节点
			productType.setChildren(afterChildren);
		}
		return children;
	}
前台响应时间:
结果:3.48s响应出来我们都可以和几杯咖啡了。

1.2 通过循环获取分类树

private List<ProductType> getAllTreeByLoop(Long pid) {
		List<ProductType> orginals = null;
		if (pid == 0) {
			orginals = mapper.getAll();
		} else {
			orginals = mapper.getTypes(pid);
		}
		if (null == orginals)
			return null;
		Map<Long, ProductType> dtoMap = new HashMap<Long, ProductType>();
		for (ProductType node : orginals) {
			dtoMap.put(node.getId(), node);
		}

		List<ProductType> resultList = new ArrayList<ProductType>();
		for (Map.Entry<Long, ProductType> entry : dtoMap.entrySet()) {
			ProductType node = entry.getValue();
			if (node.getPid() == pid) {
				// 如果是顶层节点,直接添加到结果集合中
				resultList.add(node);
			} else {
				// 如果不是顶层节点,找的起父节点,然后添加到父节点的子节点中
				if (dtoMap.get(node.getPid()) != null) {
					dtoMap.get(node.getPid()).addChild(node);
				}
			}
		}
		return resultList;
	}

三、优化

无论是使用那种方法,都是会去数据库查询,又因为商品类型不是经常改变,我们可以尝试静态化到远程,比如使用七牛云云存储或者阿里云,然后在我们jsp页面中加载七牛云提供给我们的json数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值