010显示商品服务分类,实现数据储存在缓存中

该博客详细介绍了如何分析商品服务分类的需求,通过包装数据结构并利用Redis进行存储。文章涵盖了从需求分析到服务端接口、实现类,再到消费端接口、实现类和控制器的全过程,并讨论了发布与订阅的实现。
摘要由CSDN通过智能技术生成

1 需求分析

在这里插入图片描述
分析商品分类所需要的数据类型为
最外层{“data”:[{},{}]}确定整个返回结果是一个类(最外侧是{}),类中包含了一个属性。属性叫data,类型是List,泛型是Object类型(后面写递归,所以才设置成Object)
分析每个菜单可能是两种类型:
第一层和第二层菜单包含了String url,String name; List<?> list属性的类
第三层菜单是String字符串

2 包装数据

包装data对象

package com.cdsxt.ego.beans;

import java.util.List;

/*
 * 响应到前台的数据格式
 */
public class CatResult {
	private List<?> data;

	public List<?> getData() {
		return data;
	}

	public void setData(List<?> data) {
		this.data = data;
	}
	
}

包装节点

package com.cdsxt.ego.beans;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;

public class CatNode {
	//将指定的Java对象转为json格式的对象
	@JsonProperty(value="u")
	private String url;
	@JsonProperty(value="n")
	private String name;
	@JsonProperty(value="i")
	private List<?> list;
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<?> getList() {
		return list;
	}
	public void setList(List<?> list) {
		this.list = list;
	}
	
	

}

3 服务端接口

//实现门户首页分类查询
	public List<ItemCat> loadItemCatListService();

4 服务端实现类

	@Override
	public List<ItemCat> loadItemCatListService() {
		// TODO Auto-generated method stub
		ItemCatExample example = new ItemCatExample();
		return itemCatMapper.selectByExample(example);
	}

5 消费端接口

package com.cdsxt.ego.portal.service;

import com.cdsxt.ego.beans.CatResult;

public interface PortalItemCatService {
	
	public String loadItemService();
}

6 消费端实现类

package com.cdsxt.ego.portal.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.cdsxt.ego.beans.CatNode;
import com.cdsxt.ego.beans.CatResult;
import com.cdsxt.ego.portal.service.PortalItemCatService;
import com.cdsxt.ego.rpc.pojo.ItemCat;
import com.cdsxt.ego.rpc.service.ItemCatService;
import com.cdsxt.ego.utils.JsonUtils;

import redis.clients.jedis.JedisCluster;
@Service
public class PortalItemCatServiceImpl implements PortalItemCatService{
	@Autowired
	private ItemCatService itemCatServiceProxy;
	//注入redis配置信息ITEM_CAT
	@Value("${ITEM_CAT}")
	private String ItemKey;
	@Autowired
	private JedisCluster cluster;
	@Override
	public String loadItemService() {
		// TODO Auto-generated method stub
		//获取所有父节点
		//获取缓存数据中的数据
		String jsonStr = cluster.get(ItemKey);
		if(!StringUtils.isEmpty(jsonStr)) {
			System.out.println("进入缓存");
			return jsonStr;
		}
		List<ItemCat> list = itemCatServiceProxy.loadItemCatListService();
		CatResult result = new CatResult();
		List<?> data = getAllItemCat(0L,list);
		result.setData(data);
		String str = JsonUtils.objectToJson(result);
		//将缓存的数据存到redis数据库中
		cluster.set(ItemKey, str);
		return str;
	}
	//使用递归查询所有数据
	private List<?> getAllItemCat(Long parentId, List<ItemCat> list) {
		// TODO Auto-generated method stub
		List<Object> listResult = new ArrayList<>();
		//一个cat对应一个菜单项目,前两层类型都是CatNode类型,后一层为String
		for (ItemCat cat : list) {
			//说明是第一层或第二层
			System.out.println("cat.getParentId():"+cat.getParentId());
			System.out.println("ParentId:"+parentId);
			if(cat.getParentId().equals(parentId)) {
				if(cat.getIsParent()) {
					CatNode node = new CatNode();
					if(cat.getParentId().longValue()==0) {
						//如果是一级菜单
						node.setName("<a href='/products/"+cat.getId()+".html'>"+cat.getName()+"</a>");
					}else {
						//如果是二级菜单
						node.setName(cat.getName());
					}
					node.setUrl("/products/"+cat.getId()+".html");
					node.setList(getAllItemCat(cat.getId(), list));
					listResult.add(node);
				}else {
					listResult.add("/products/"+cat.getId()+".html|"+cat.getName());
				}
			}
			
		}
		return listResult;
	}

}

7 控制器

package com.cdsxt.ego.portal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cdsxt.ego.beans.CatResult;
import com.cdsxt.ego.portal.service.PortalItemCatService;

@Controller
public class PortalItemCatController {
	@Autowired
	private PortalItemCatService portalItemCatService;
	
	@ResponseBody
	@RequestMapping(value="/item/cat",produces=MediaType.TEXT_HTML_VALUE+";charset=UTF-8")
	public String loadItemCat() {
		return portalItemCatService.loadItemService();
	}
}

8 发布及订阅

发布

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	<!-- 添加服务提供者的标志 -->
	<dubbo:application name="0516-ego-rpc-provider" />

	<!-- 指定注册中心 -->
	<dubbo:registry address="192.168.32.132:2181,192.168.32.132:2182,192.168.32.132:2183" protocol="zookeeper" />

	<!-- 指定当前项目发布dubbo服务的方式 -->
	<!-- 指定服务发布的协议:dubbo协议 -->
	<!-- 指定服务发布的端口:10000 -->
	<dubbo:protocol name="dubbo" port="20000" />
	 
	 
	 <!--指定发布的具体的服务  -->
	 <!--指定当前项目发布dubbo服务的方式  -->
	 <!--指定服务发布的协议:dubbo协议  -->
	 <!--指定服务发布的端口:10000  -->
	 <!--发布dubbo服务  -->
	 <dubbo:service interface="com.cdsxt.ego.rpc.service.ItemService" ref="itemServiceImpl"></dubbo:service>
	 <dubbo:service interface="com.cdsxt.ego.rpc.service.ItemCatService" ref="itemCatServiceImpl"></dubbo:service>
	 <dubbo:service interface="com.cdsxt.ego.rpc.service.ItemDescService" ref="itemDescServiceImpl"></dubbo:service>
	 <dubbo:service interface="com.cdsxt.ego.rpc.service.UserService" ref="userServiceImpl"></dubbo:service>

	
	
</beans>

订阅

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<!-- 添加服务消费者的标志 -->
	<dubbo:application name="0516-ego-portal-web-consumer"></dubbo:application>
	<!-- 指定注册中心 -->
	<dubbo:registry
		address="192.168.32.132:2181,192.168.32.132:2182,192.168.32.132:2183"
		protocol="zookeeper" />
	<!--spring容器中存在一个远程服务的代理对象 -->
	<dubbo:reference interface="com.cdsxt.ego.rpc.service.ItemCatService" id="itemCatServiceProxy"></dubbo:reference>
	
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值