solr搜尋_商品分類_品牌列表_規格列表的實現思路和步驟
第一步: 事先我們已經將itemCat表中的資料放入了solr中,所以這裡可以直接使用solr的分組查詢,查詢商品分類資料
第二步: 將商品分類表全部快取進redis資料庫中
快取的形式是map型別 大key=itemcat 小key=name value=type_id 思路還是一樣先從mysql資料庫查出來,然後存入redis資料庫中
List<TbItemCat> list = findAll(); //mysql查出來
for(TbItemCat itemCat:list){ //redis存進去
redisTemplate.boundHashOps("itemCat").put(itemCat.getName(), itemCat.getTypeId());
}
@Autowired
private RedisTemplate redisTemplate;
/**
* 根據上級ID查詢列表
*/
@Override
public List<TbItemCat> findByParentId(Long parentId) {
TbItemCatExample example1=new TbItemCatExample();
Criteria criteria1 = example1.createCriteria();
criteria1.andParentIdEqualTo(parentId);
//每次執行查詢的時候,一次性讀取快取進行儲存 (因為每次增刪改都要執行此方法)
List<TbItemCat> list = findAll();
for(TbItemCat itemCat:list){
redisTemplate.boundHashOps("itemCat").put(itemCat.getName(), itemCat.getTypeId());
}
System.out.println("更新快取:商品分類表");
return itemCatMapper.selectByExample(example1);
}
第三步: 快取品牌資料和規格資料(第三列規格,第四列品牌)
第三列,第四列都是陣列,需要先將陣列轉換成List<Map>型別,這樣便於放入redis資料庫中
儲存形式是 大key =brandList 小key=id(type_id) value=brandIds是一個List<Map> 型別的
redisTemplate.boundHashOps("brandList").put(typeTemplate.getId(), brandList);
List<Map> specList = findSpecList(typeTemplate.getId()); 這裡面封裝了規格和規格選項的內容,跟brandList一樣,也需要封裝讓如redis快取中
@Autowired
private RedisTemplate redisTemplate;
/**
* 將資料存入快取
*/
private void saveToRedis(){
//獲取模板資料
List<TbTypeTemplate> typeTemplateList = findAll();
//迴圈模板
for(TbTypeTemplate typeTemplate :typeTemplateList){
//儲存品牌列表
List<Map> brandList = JSON.parseArray(typeTemplate.getBrandIds(), Map.class);
redisTemplate.boundHashOps("brandList").put(typeTemplate.getId(), brandList);
//儲存規格列表
List<Map> specList = findSpecList(typeTemplate.getId());//根據模板ID查詢規格列表
redisTemplate.boundHashOps("specList").put(typeTemplate.getId(), specList);
}
}
然後只要運營商頁面執行後,就可以將相關資料放入redis資料庫中.
第四步:顯示品牌和規格資料
itemcat brandList和 speList 資料現在已經全部都在redis中了
現在整個思路就是根據 關鍵字Keywords------->查詢到商品分類名稱----------->查詢到模板id-------->查詢到品牌列表 和 規格列表
@Autowired
private RedisTemplate redisTemplate;
/**
* 查詢品牌和規格列表
* @param category 分類名稱
* @return
*/
private Map searchBrandAndSpecList(String category){
Map map=new HashMap();
Long typeId = (Long) redisTemplate.boundHashOps("itemCat").get(category);//獲取模板ID
if(typeId!=null){
//根據模板ID查詢品牌列表
List brandList = (List) redisTemplate.boundHashOps("brandList").get(typeId);
map.put("brandList", brandList);//返回值新增品牌列表
//根據模板ID查詢規格列表
List specList = (List) redisTemplate.boundHashOps("specList").get(typeId);
map.put("specList", specList);
}
return map;
}