document
API test

获取分类的属性

GET
http://localhost:88/api/product/attr/:attrType/list/:catelogId

API description

分类包括两种, 一种是规格参数, 一种是销售属性

Request Parameters

parameter
type
description
required
attrType
int
(base或sale)示例: base
required
catelogId
int
(分类ID)示例:0
required

Response Parameters

parameter
type
description
required
msg
string
示例:success
required
code
int
示例:0
required
page
object
数据字典
required
totalCount
int
示例:3
required
pageSize
int
示例:10
required
totalPage
int
示例:1
required
currPage
int
示例:1
required
list
array
数据列表
required
attrId
int
示例:9
required
attrName
string
示例:颜色
required
searchType
int
示例:0
required
valueType
int
示例:0
required
icon
string
示例:xxx
required
valueSelect
string
示例:黑色;白色;蓝色
required
attrType
int
示例:0
required
enable
int
示例:1
required
catelogId
int
示例:225
required
showDesc
int
示例:0
required
attrGroupId
object
示例:null
required
catelogName
string
示例:手机
required
groupName
object
示例:null
required
catelogPath
object
示例:null
required

Description or Example

# 核心代码 ```java @GetMapping("/{attrType}/list/{catelogId}") public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catalogId, @PathVariable("attrType") String attrType) { PageUtils page = attrService.queryAttrsByCategoryIdWithKeys(params, catalogId, attrType); return R.ok().put("page", page); } ``` ```java @Override public PageUtils queryAttrsByCategoryIdWithKeys(Map<String, Object> params, Long catalogId, String attrType) { // 获取检索关键字 String key = (String) params.get("key"); LambdaQueryWrapper<AttrEntity> queryWrapper = new LambdaQueryWrapper<>(); // 首先明确是规格参数还是销售属性 if (ProductConstant.Attr.ATTR_BASE.getMessage().equals(attrType)) queryWrapper.eq(AttrEntity::getAttrType, ProductConstant.Attr.ATTR_BASE.getCode()); // 规格参数 else if(ProductConstant.Attr.ATTR_SALE.getMessage().equals(attrType)) queryWrapper.eq(AttrEntity::getAttrType, ProductConstant.Attr.ATTR_SALE.getCode()); // 销售属性 else queryWrapper.eq(AttrEntity::getAttrType, ProductConstant.Attr.ATTR_SALE$BASE.getCode()); // 既是, 又是 // 判断是否有分类ID if (catalogId != 0L) queryWrapper.eq(AttrEntity::getCatelogId, catalogId); if (StringUtils.isNotBlank(key)) queryWrapper.and(wrapper -> wrapper.eq(AttrEntity::getAttrId, key) .or() .like(AttrEntity::getAttrName, key)); IPage<AttrEntity> iPage = this.page(new Query<AttrEntity>().getPage(params), queryWrapper); PageUtils pageUtils = new PageUtils(iPage); List<AttrRespVO> attrRespVOS = iPage.getRecords() .stream() .map(attrEntity -> { AttrRespVO attrRespVO = new AttrRespVO(); BeanUtils.copyProperties(attrEntity, attrRespVO); // 判断是否为规格参数(规格参数才有属性分组) if (ProductConstant.Attr.ATTR_BASE.getMessage().equals(attrType)) { LambdaQueryWrapper<AttrAttrgroupRelationEntity> wp = new LambdaQueryWrapper<>(); wp.eq(AttrAttrgroupRelationEntity::getAttrId, attrEntity.getAttrId()); AttrAttrgroupRelationEntity relation = relationService.getOne(wp); // 若该属性存在与属性分组的关系 if (relation != null) { // 获取属性分组的ID Long attrGroupId = relation.getAttrGroupId(); // 获取属性分组 AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId); // 获取属性分组的名字 attrRespVO.setGroupName(attrGroup.getAttrGroupName()); } } Long catelogId = attrEntity.getCatelogId(); CategoryEntity category = categoryService.getById(catelogId); // 若跳过前端校验, 分类可能为空 if (category != null) attrRespVO.setCatelogName(category.getName()); return attrRespVO; }).collect(Collectors.toList()); pageUtils.setList(attrRespVOS); return pageUtils; } ``` # 扩展知识 ## 为什么不使用多表联查, 而是多次发送查询请求? > 有人会说, 你个小垃圾, 发送这么多请求, 需要频繁建立和销毁TCP连接,效率太低啦! > 其实不然, 如果使用多表联查, 可能会发生一个非常严重的后果, 即笛卡尔积式查找, 假设数据量很大, 分别为1w和1w, 一旦笛卡尔积查找, 数据量会达到惊人的一亿, 效率比建立连接低得多得多 > **~~因此, 需要用多次查询规避多表联查~~** > **建议查看总结, 这里写的不好, 太片面了**