根据商品等级查询商品分类信息(三层嵌套)
- 1.实现商品分类
- 1.1设计数据库表
- 1.2项目改造
- 1.2.1导入mybatis-puls的jar包
- 1.2.2编辑项目中ItemCat 在包Pojo中
- 1.2.2编辑项目中ItemCatMapper
- 1.2.3编辑YML配置文件
- 模块的层级项目的创建
- 1.3前端商品模块的跳转
- 1.4前端分类列表的展现
- 1.4.1 前端页面的分析
- 1.4.2 业务接口文档分析
- 1.4.3 编辑ItemCatController
- 1.4.4 编辑ItemCatServiceImpl---常规利用mybatis-plus(效率较低)
- 1.4.5 利用Map集合进行逐级遍历查询
- 1.4.6 商品分类列表信息展示
1.实现商品分类
1.1设计数据库表
商品类表item_cat

1.2项目改造
1.2.1导入mybatis-puls的jar包

1.2.2编辑项目中ItemCat 在包Pojo中

1.2.2编辑项目中ItemCatMapper

1.2.3编辑YML配置文件

模块的层级项目的创建

1.3前端商品模块的跳转

1.4前端分类列表的展现
1.4.1 前端页面的分析
1.生命周期函数

2.页面跳转axio请求

1.4.2 业务接口文档分析

1.4.3 编辑ItemCatController
使用的get请求和restful数据请求,需要在接受参数前面加注解@PathVariable
//等级查询商品分类信息
@GetMapping("/findItemCatList/{level}")
public SysResult findItemCatList(@PathVariable Integer level) {
return SysResult.success(itemCatService.findItemCatList(level));
}
1.4.4 编辑ItemCatServiceImpl—常规利用mybatis-plus(效率较低)
@Service
public class ItemCatServiceImpl implements ItemCatService {
@Autowired
ItemCatMapper itemCatMapper;
public List<ItemCat> findItemCatList(Integer level) {
long startTime =System.currentTimeMillis();
//查询一级商品分类信息
QueryWrapper<ItemCat> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("parent_id", 0);
List<ItemCat> onelist=itemCatMapper.selectList(queryWrapper);
for(ItemCat oneItemCat:onelist){
//1.为了复用条件构造器 将之前的数据清空
queryWrapper.clear();
//查询二级数据 parent_id =一级ID
queryWrapper.eq("parent_id", oneItemCat.getId());
List<ItemCat> twolist=itemCatMapper.selectList(queryWrapper);
for (ItemCat twoItemCat:twolist){
queryWrapper.clear();
queryWrapper.eq("parent_id", twoItemCat.getId());
List<ItemCat> thirdlist=itemCatMapper.selectList(queryWrapper);
twoItemCat.setChildren(thirdlist);
}
oneItemCat.setChildren(twolist);
}
long endTime =System.currentTimeMillis();
System.out.println(endTime-startTime);
return onelist;
}
}
1.4.5 利用Map集合进行逐级遍历查询
1.获取一级分类列表数据数据
public Map<Integer,List<ItemCat>> getMap(){
Map<Integer,List<ItemCat>> map=new HashMap<>();
//查询所有数据库记录
List<ItemCat> list=itemCatMapper.selectList(null);
//1.遍历数据
for (ItemCat itemCat : list){
int parentId = itemCat.getParentId();
if(map.containsKey(parentId)){
map.get(parentId).add(itemCat);
}else{
//key不存在,定义list集合,将自己作为第一个元素追加
List<ItemCat> childrenList=new ArrayList<>();
childrenList.add(itemCat);
//将数据保存到map集合中
map.put(parentId, childrenList);
}
}
return map;
}
2.获取二级分类列表数据
(1)先查询一级分类列表数据
(2)再遍历一级级分类列表,封装二级数据
//该方法获取1-2级数据信息
public List<ItemCat> getTwoList(Map<Integer,List<ItemCat>> map){
//1.先查询一级菜单数据
List<ItemCat> oneList=map.get(0);
//2.遍历每个一级菜单去封装二级数据
for (ItemCat oneItemCat:oneList){
//查询二级数据
int parentId=oneItemCat.getId();
//查询二级数据
List<ItemCat> twoList = map.get(parentId);
oneItemCat.setChildren(twoList);
}
return oneList;
}
3.获取三级分类列表信息
(1)获取二级分类列表信息
(2)遍历一级菜单,获取二级数据
(3)根据二级菜单查询三级数据 防止二级数据为null的现象
(4)将三级数据封装到二级中
public List<ItemCat> getThreeList(Map<Integer,List<ItemCat>> map){
//1.获取1-2数据信息 包含了2级的children
List<ItemCat> oneList=getTwoList(map);
//2.编辑一级数据,获取二级数据
for (ItemCat oneitemCat:oneList){
List<ItemCat> twoList=oneitemCat.getChildren();
if(twoList==null || twoList.size()==0){
continue;
}
//3.遍历二级数据,查询三级信息
for(ItemCat twoItemCat:twoList){
//查询三级 parentId = 二级Id
int prentId=twoItemCat.getId();
List<ItemCat> threeList=map.get(prentId);
twoItemCat.setChildren(threeList);
}
}
return oneList;
}
4.最后判断接收的等级,来执行不同等级的查询并返回数据集合
@Override
public List<ItemCat> findItemCatList(Integer level) {
long startTime =System.currentTimeMillis();
//回传一级商品信息
Map<Integer,List<ItemCat>>map=getMap();
if(level==1){
return map.get(0);
}
if(level==2){
return getTwoList(map);
}
List<ItemCat>allList=getThreeList(map);
long endTime =System.currentTimeMillis();
System.out.println(endTime-startTime);
return allList;
}
1.4.6 商品分类列表信息展示

最后
以上就是动人茉莉最近收集整理的关于根据商品等级查询商品分类信息(三层嵌套)1.实现商品分类的全部内容,更多相关根据商品等级查询商品分类信息(三层嵌套)1内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复