document
API test

获取分类下的品牌

POST

Description or Example

# 核心代码 ```java @RequestMapping("/brands/list") public R brandsByCategoryId(@RequestParam("catId") Long catId) { List<BrandEntity> brandEntities = relationService.getBrandsByCatId(catId); // 分类下可能没有品牌 if (brandEntities != null) { List<BrandRespVO> vos = brandEntities.stream().map(brandEntity -> new BrandRespVO() .setBrandId(brandEntity.getBrandId()) .setBrandName(brandEntity.getName()) ).collect(Collectors.toList()); return R.ok().put("data", vos); } return null; } ``` ```java @Override public List<BrandEntity> getBrandsByCatId(Long catId) { // 在关联表中查询该分类下的所有品牌ID List<Long> brandIds = getBrandIdsByCatId(catId); if (brandIds != null && !brandIds.isEmpty()) return brandService.getBaseMapper().selectBatchIds(brandIds); return null; } @Override public List<Long> getBrandIdsByCatId(Long catId) { LambdaQueryWrapper<CategoryBrandRelationEntity> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(CategoryBrandRelationEntity::getCatelogId, catId); return this.baseMapper .selectList(wrapper) .stream() .map(CategoryBrandRelationEntity::getBrandId) .collect(Collectors.toList()); } ``` # 扩展知识 ## 为什么不直接返回VO? > 如果返回的时一个整体的BrandEntity集合, 集合中的每一个元素所具有的属性更多, 利于复用, 如果直接返回VO, 别人根本复用不了 因此, **可以提高复用率** ## Controller的职责 1. 处理请求, 接受并校验数据 2. 将数据交予service进行业务处理 3. 将service返回的数据进行封装