SpringCloudAlibaba云商场-基础数据处理(四)

155 阅读3分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第10天,点击查看活动详情

1.商品发布加载功能

接上回,对应的Bean我这边已经提前创建好了。

1.1 分类加载

分类功能需要实现按照父ID查询,最开始初始化加载的是顶级父类,parent_id=0,后面每次点击的时候都根据传入的id查询子分类。(举个例子:一级->二级->三级)

1)Mapper 创建com.xz.mall.goods.mapper.CategoryMapper,代码如下:

public interface CategoryMapper extends BaseMapper<Category> {
}

2)Service 创建接口com.xz.mall.goods.service.CategoryService,代码如下:

public interface CategoryService extends IService<Category> {

    /**
     * 根据分类父ID查询所有子类
     * @param pid
     * @return
     */
    List<Category> findByParentId(Integer pid);

}

接口实现类com.xz.mall.goods.service.impl.CategoryServiceImpl,代码如下:

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService  {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 根据分类父ID查询所有子类
     * @param pid
     * @return
     */
    @Override
    public List<Category> findByParentId(Integer pid) {
        //条件封装对象
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", pid);
        return categoryMapper.selectList(queryWrapper);
    }
}

3)Controller 创建com.xz.mall.goods.controller.CategoryController,代码如下:

@RestController
@RequestMapping(value = "/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据分类父ID查询所有子类
     * @param id
     * @return
     */
    @GetMapping(value = "/parent/{id}")
    public RespResult<List<Category>> findByParentId(@PathVariable("id") Integer id) {
        List<Category> categories = categoryService.findByParentId(id);
        return RespResult.ok(categories);
    }

}

用postman测试一下:

根据分类父ID查询所有子类Category.jpg

1.2品牌加载

品牌需要根据分类进行加载,当用户选择第3级分类的时候,加载品牌,品牌数据需要经过category_brand表关联查询。

可以按照如下步骤实现:

1、查询category_brand中指定分类对应的品牌ID集合
2、从brand查出品牌集合

1)Mapper 在com.xz.mall.goods.mapper.BrandMapper中添加根据分类ID查询品牌ID集合:

/**
 * 根据分类ID查询品牌ID集合
 * @param id
 * @return
 */
@Select("select brand_id from category_brand where category_id=#{id}")
List<Integer> queryBrandIds(Integer id);

2)Service 接口com.xz.mall.goods.service.BrandService中添加根据分类ID查询品牌ID集合:

/**
 * 根据分类ID查询品牌ID集合
 * @param id
 * @return
 */
List<Brand> queryByCategoryId(Integer id);

实现类com.xz.mall.goods.service.impl.BrandServiceImpl中,代码如下:

/**
 * 根据分类ID查询品牌集合
 * @param id
 * @return
 */
@Override
public List<Brand> queryByCategoryId(Integer id) {
    //根据分类ID查询品牌集合ID
    List<Integer> brandIds = brandMapper.queryBrandIds(id);
    //根据品牌ID集合查询品牌集合
    if (!CollectionUtils.isEmpty(brandIds)) {
        return brandMapper.selectList(new QueryWrapper<Brand>().in("id", brandIds));
    }
    return null;
}

3)Controller 在com.xz.mall.goods.controller.BrandController中添加根据分类ID查询品牌ID集合:

/**
 * 根据分类ID查询品牌集合
 * @param pid
 * @return
 */
@GetMapping(value = "/category/{pid}")
public RespResult<List<Brand>> categoryBrands(@PathVariable("pid") Integer pid){
    List<Brand> brands = brandService.queryByCategoryId(pid);
    return RespResult.ok(brands);
}

用postman测试结果如下:

根据分类ID查询品牌集合.jpg

1.3属性加载

属性也称为规格,属性也需要根据分类查询,我们可以按照如下思路实现:

1、先从category_attr根据分类ID查询出当前分类拥有的属性ID集合
2、从sku_attribute中查询属性集合

1)Mapper 创建com.xz.mall.goods.mapper.SkuAttributeMapper,实现根据分类ID查询属性信息,代码如下:

public interface SkuAttributeMapper extends BaseMapper<SkuAttribute> {

    /****
     * 1、根据分类ID查询属性ID集合
     * 2、根据属性ID集合查询属性集合
     */
    @Select("select * from sku_attribute where id IN(SELECT attr_id FROM category_attr WHERE category_id=#{id})")
    List<SkuAttribute> queryByCategoryId(Integer id);

}

2)Service 创建接口com.xz.mall.goods.service.SkuAttributeService,添加根据分类ID查询属性集合方法,代码如下:

public interface SkuAttributeService extends IService<SkuAttribute> {

    /***
     * 根据分类ID查询属性加集合
     */
    List<SkuAttribute> queryList(Integer id);

}

创建接口实现类com.xz.mall.goods.service.impl.SkuAttributeServiceImpl,代码如下:

@Service
public class SkuAttributeServiceImpl extends ServiceImpl<SkuAttributeMapper, SkuAttribute> implements SkuAttributeService {

    @Autowired
    private SkuAttributeMapper skuAttributeMapper;

    /*****
     * 根据分类ID查询属性集合
     * @param id
     * @return
     */
    @Override
    public List<SkuAttribute> queryList(Integer id) {
        return skuAttributeMapper.queryByCategoryId(id);
    }
}

3)Controller 创建com.xz.mall.goods.controller.SkuAttributeController,添加根据分类ID查询属性集合,代码如下:

@RestController
@RequestMapping(value = "/skuAttribute")
public class SkuAttributeController {

    @Autowired
    private SkuAttributeService skuAttributeService;

    /*****
     * 根据分类ID查询属性集合
     */
    @GetMapping(value = "/category/{id}")
    public RespResult<List<SkuAttributeController>> categorySkuAttributeList(@PathVariable(value = "id")Integer id){
        List<SkuAttribute> skuAttributes = skuAttributeService.queryList(id);
        return RespResult.ok(skuAttributes);
    }

}

用postman测试如下:

属性加载.jpg

总结:本篇主要讲述了商品发布加载功能中的分类加载、品牌加载、属性加载的代码具体实现。