document
API test

新增品牌&分类的关系

POST
http://localhost:88/api/product/categorybrandrelation/save

Request Parameters

parameter
type
description
required
brandId
long
示例:9
required
catelogId
long
示例:1019
required

Response Parameters

parameter
type
description
required
msg
string
示例:success
required
code
int
示例:0
required

Description or Example

# Error说明 ## Q1. 前端发送请求问题 ``` vue vue.esm.js:591 [Vue warn]: Error in callback for watcher "paths": "TypeError: Cannot read properties of undefined (reading 'publish')" ``` ## A1. 解决方案 ```bash npm install --save pubsub-js # 在前端项目下载对应的依赖 ``` ```js import PubSub from 'pubsub-js' Vue.prototype.PubSub = PubSub // 在main.js添加以上代码 ``` **** # 核心代码 ```java @RequestMapping("/save") // @RequiresPermissions("product:categorybrandrelation:save") public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){ categoryBrandRelationService.addDetail(categoryBrandRelation); return R.ok(); } ``` ```java @Override public void addDetail(CategoryBrandRelationEntity categoryBrandRelation) { Long brandId = categoryBrandRelation.getBrandId(); Long catelogId = categoryBrandRelation.getCatelogId(); BrandEntity brandEntity = brandService.getBaseMapper().selectById(brandId); CategoryEntity categoryEntity = categoryService.getBaseMapper().selectById(catelogId); categoryBrandRelation.setBrandName(brandEntity.getName()) .setCatelogName(categoryEntity.getName()); this.save(categoryBrandRelation); } ``` # 知识拓展 ## 为什么分类与品牌的关系表存储两个冗余字段? > **总结: 牺牲空间, 换取时间** ![image.png](https://cos.easydoc.net/13568421/files/ljy4j7pz.png) 因为这两个字段会被经常查询, 如上所示, 如果不存储这两个冗余字段, 就需要我们每次显示都去查找三个表,**查询效率极低**, 如果有了冗余字段, 只需要查询一次, 效率大大提高