java根据商品编号查找商品_查询商品分类下的所有商品(分类级别不限)
正版python深度学习逻辑算法与编程
80.9元
(需用券)
去购买 >

查询所有商品分类
遍历每个分类
如果能查到父级,就将子类放入父级中
根据分类Id查询商品列表
最后将查询到的商品放入分类中
controller代码
@GetMapping("/list")
@ApiOperation(value = "分类下的商品", notes = "所有分类包含商品", produces = "application/json")
public ResultPoJo> getCategoryproducts(Pagination pagination,String keyword) {
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper().orderByAsc(YxJpProductCategory::getSort).orderByDesc(YxJpProductCategory::getCreateDate);
CommonUtil.emptyStr(keyword).ifPresent(key -> queryWrapper.and(andQw ->
andQw.or(orQw -> orQw.like(YxJpProductCategory::getName, key))
));
// 查询所有分类
List categoryList = categoryService.list(queryWrapper);//查询所有分类
List rsList = categoryList;
if (StrUtil.isBlank(keyword)) {//关键字为空(用于搜索)
categoryList.forEach(category -> {//遍历每个分类
if (!"0".equals(category.getParentId())) {//父id不等于0 (0为顶级分类)
//查找父级分类(包含子类)
categoryList.stream().filter(ca -> ca.getId().equals(category.getParentId())).findFirst()
.ifPresent(parentCategory -> {//如果存在父子关系
List children = parentCategory.getChildren();
if (ObjectUtil.isNull(children)) {
children = Lists.newArrayList();
}
//根据商品分类查询商品
IPage page = productService.getJpProductByCategory(pagination.page(),keyword,category.getId());
category.setProducts(page);
//将子级放到父级的children中
children.add(category);
parentCategory.setChildren(children);
});
}
});
rsList = categoryList.stream().filter(category -> "0".equals(category.getParentId())).collect(Collectors.toList());
}
return ResultPoJo.ok(rsList);
}
在实体类中需要加入children的列表(类型是自己本身)
需要加入商品列表的属性
实体类代码
public class YxJpProductCategory extends BaseEntity {
private static final long serialVersionUID=1L;
private String id;
@ApiModelProperty(value = "上级分类的编号:0表示一级分类")
private String parentId;
@ApiModelProperty(value = "所有父级编号")
private String parentIds;
@ApiModelProperty(value = "分类名称")
private String name;
@ApiModelProperty(value = "分类级别:0->1级;1->2级")
private String level;
@ApiModelProperty(value = "产品数量")
private Integer productCount;
@ApiModelProperty(value = "排序")
private Integer sort;
@ApiModelProperty(value = "图标")
private String icon;
@ApiModelProperty(value = "关键词")
private String keywords;
@ApiModelProperty(value = "描述")
private String description;
@ApiModelProperty(value = "创建者",hidden = true)
private String createBy;
@ApiModelProperty(value = "创建时间",example = "2020-05-19 00:00:00",hidden = true)
private Date createDate;
@ApiModelProperty(value = "更新者",hidden = true)
private String updateBy;
@ApiModelProperty(value = "更新时间",example = "2020-05-19 00:00:00",hidden = true)
private Date updateDate;
private String remarks;
@TableLogic
@ApiModelProperty(value = "删除标记",example = "0",hidden = true)
private String delFlag;
private String openid;
@TableField(select = false,exist = false)
@ApiModelProperty(hidden = true)
private List children;
@TableField(select = false,exist = false)
@ApiModelProperty(hidden = true)
private IPage products;
@Override
protected Serializable pkVal() {
return this.id;
}
}
java 11官方入门(第8版)教材
79.84元
包邮
(需用券)
去购买 >

