第二十二章第三节:商品服务:发布商品 获取分类关联的品牌信息
1、定义需要返回的数据vo
com.applesnt.onlinemall.product.vo.BrandVo
package com.applesnt.onlinemall.product.vo;
import lombok.Data;
@Data
public class BrandVo {
/*品牌id*/
private Long brandId;
/*品牌名字*/
private String brandName;
}
2、定义品牌查询接口
com.applesnt.onlinemall.product.service.CategoryBrandRelationService
List<BrandEntity> getBrandsByCatId(Long catId);
3、定义品牌查询接口实现
com.applesnt.onlinemall.product.service.impl.CategoryBrandRelationServiceImpl
/*注入分类品牌dao层*/
@Autowired
CategoryBrandRelationDao relationDao;
/*注入品牌service*/
@Autowired
BrandService brandService;
@Override
public List<BrandEntity> getBrandsByCatId(Long catId) {
/*先从关联表中查询所有的品牌id和名字*/
List<CategoryBrandRelationEntity> catelogids = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>()
.eq("catelog_id", catId));
/*根据查询出来的品牌id封装完整的品牌信息*/
List<BrandEntity> collect = catelogids.stream().map((obj) -> {
Long brandId = obj.getBrandId();
BrandEntity brandEntity = brandService.getById(brandId);
return brandEntity;
}).collect(Collectors.toList());
return collect;
}
4、controller调用
com.applesnt.onlinemall.product.controller.CategoryBrandRelationController
@GetMapping("/brands/list")
public R relationBrandsList(@RequestParam(value = "catId",required = true) Long catId){
/*返回品牌实体类数据,不直接返回自己定义的brandvo,以便其他业务调用*/
List<BrandEntity> brandVos = categoryBrandRelationService.getBrandsByCatId(catId);
/*封装成自己需要的数据*/
List<BrandVo> collect = brandVos.stream().map((brand) -> {
BrandVo brandVo = new BrandVo();
brandVo.setBrandId(brand.getBrandId());
brandVo.setBrandName(brand.getName());
return brandVo;
}).collect(Collectors.toList());
return R.ok().put("data",collect);
}

浙公网安备 33010602011771号