CGB2105-Day15
闪耀太阳 2021-08-09 11:25:04 2730 收藏 11 分类专栏: 2105 版权 2105 专栏收录该内容 17 篇文章 26 订阅 订阅专栏1. 全局异常处理/事务控制关系
2 商品分类业务实现
2.1 商品分类查询优化
2.1.1 现有代码存在的问题
问题说明: 如果经过2次for循环, 链接数据库的次数将会很多, 直接影响查询的效率.
优化: 尽可能的降低查询的次数,同时满足用户需求
2.1.2 优化策略
数据结构: Map<K,V> map
Key=“父级ID” Value=List<当前父级的子级>
用法: 如果想要获取任意父级的子级 map.get(父级的ID)
用法说明: Value 只有父级的子级信息,没有嵌套结构
Map<父级ID,List<ItemCat{id=xx,name=xx,children=null}>>
2.1.3 封装Map集合
原理图:
2.1.3 封装Map实现
/**
* 1.封装Map集合 Map<Key=父级ID,value=List<ItemCat对象>>
* 2.说明: 将所有的数据库的父子关系,进行封装.(没有嵌套!!!!)
* 3.优势: 只查询一次数据库,就可以完成父子关系的封装.
策略:
* 1. key不存在, 准备一个新List集合,将自己当作第一个元素追加
* 2. key存在, 获取原有list集合,将自己追加.
*
*/
public Map<Integer,List<ItemCat>> initMap(){
//Map中包含了所有的父子级关系.
Map<Integer,List<ItemCat>> map = new HashMap<>();
//1.查询item_cat表中的所有的记录(1/2/3级菜单)
List<ItemCat> itemCatList = itemCatMapper.selectList(null);
//2.实现数据的封装
for(ItemCat itemCat : itemCatList){
int key = itemCat.getParentId();
if(map.containsKey(key)){ //存在
List<ItemCat> list = map.get(key);
//将自己追加到其中
list.add(itemCat);
}else{ //不存在: 准备List集合,将自己作为第一个元素封装
List<ItemCat> list = new ArrayList<>();
list.add(itemCat);
map.put(key,list);
}
}
//将封装的数据进行返回.
return map;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
2.1.4 后台代码完整实现
/** * 1.封装Map集合 Map<Key=父级ID,value=List<ItemCat对象>> * 2.说明: 将所有的数据库的父子关系,进行封装.(没有嵌套!!!!) * 3.优势: 只查询一次数据库,就可以完成父子关系的封装. 策略: * 1. key不存在, 准备一个新List集合,将自己当作第一个元素追加 * 2. key存在, 获取原有list集合,将自己追加. * */ public Map<Integer,List<ItemCat>> initMap(){ //Map中包含了所有的父子级关系. Map<Integer,List<ItemCat>> map = new HashMap<>(); //1.查询item_cat表中的所有的记录(1/2/3级菜单) List<ItemCat> itemCatList = itemCatMapper.selectList(null); //2.实现数据的封装 for(ItemCat itemCat : itemCatList){ int key = itemCat.getParentId(); if(map.containsKey(key)){ //存在 List<ItemCat> list = map.get(key); //将自己追加到其中 list.add(itemCat); }else{ //不存在: 准备List集合,将自己作为第一个元素封装 List<ItemCat> list = new ArrayList<>(); list.add(itemCat); map.put(key,list); } } //将封装的数据进行返回. return map; }
<span class="token comment">/** * level 1 只查询一级商品分类 * 2 查询一级/二级 嵌套封装 * 3 查询一级/二级/三级 嵌套封装 * @param level * @return */</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">ItemCat</span><span class="token punctuation">></span></span> <span class="token function">findItemCatList</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> level<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">long</span> startTime <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//Map集合里边封装的是所有的父子级关系.</span> <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">,</span><span class="token class-name">List</span><span class="token punctuation"><</span><span class="token class-name">ItemCat</span><span class="token punctuation">></span><span class="token punctuation">></span></span> map <span class="token operator">=</span> <span class="token function">initMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>level <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//只获取1级菜单. parent_id = 0</span> <span class="token keyword">return</span> map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//用户查询1/2级商品分类信息</span> <span class="token keyword">if</span><span class="token punctuation">(</span>level <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token keyword">return</span> <span class="token function">getLevel2List</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//如果程序执行到这里,则说明用户查询的是1-2-3级菜单</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">ItemCat</span><span class="token punctuation">></span></span> list <span class="token operator">=</span> <span class="token function">getLevel3List</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">long</span> endTime <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"耗时:"</span><span class="token operator">+</span><span class="token punctuation">(</span>endTime<span class="token operator">-</span>startTime<span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"毫秒"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> list<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">ItemCat</span><span class="token punctuation">></span></span> <span class="token function">getLevel3List</span><span class="token punctuation">(</span><span class="token class-name">Map</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">,</span> <span class="token class-name">List</span><span class="token punctuation"><</span><span class="token class-name">ItemCat</span><span class="token punctuation">></span><span class="token punctuation">></span></span> map<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//1.先查询1-2级</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">ItemCat</span><span class="token punctuation">></span></span> oneList <span class="token operator">=</span> <span class="token function">getLevel2List</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2.遍历集合</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">ItemCat</span> oneItemCat <span class="token operator">:</span> oneList<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//获取二级集合信息</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">ItemCat</span><span class="token punctuation">></span></span> twoList <span class="token operator">=</span> oneItemCat<span class="token punctuation">.</span><span class="token function">getChildren</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>twoList <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> twoList<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//当前一级菜单没有二级元素.结束本次循环,开始下一次!!!</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//该元素有二级,应该查询三级.</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">ItemCat</span> twoItemCat <span class="token operator">:</span> twoList<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">ItemCat</span><span class="token punctuation">></span></span> threeList <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>twoItemCat<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> twoItemCat<span class="token punctuation">.</span><span class="token function">setChildren</span><span class="token punctuation">(</span>threeList<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> oneList<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//查询一级和二级信息</span> <span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">ItemCat</span><span class="token punctuation">></span></span> <span class="token function">getLevel2List</span><span class="token punctuation">(</span><span class="token class-name">Map</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">,</span> <span class="token class-name">List</span><span class="token punctuation"><</span><span class="token class-name">ItemCat</span><span class="token punctuation">></span><span class="token punctuation">></span></span> map<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//思路: 先查询一级,之后循环遍历,再次封装2级</span> <span class="token comment">//1.获取一级</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">ItemCat</span><span class="token punctuation">></span></span> oneList <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token class-name">ItemCat</span> oneItemCat <span class="token operator">:</span> oneList<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//2.如何根据一级查询二级? 通过Map集合获取</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">ItemCat</span><span class="token punctuation">></span></span> twoList <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>oneItemCat<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//3.实现了一级二级的封装</span> oneItemCat<span class="token punctuation">.</span><span class="token function">setChildren</span><span class="token punctuation">(</span>twoList<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> oneList<span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
2.2 商品分类状态修改
2.2.1 页面JS分析
2.2.2 业务接口文档说明
- 请求路径: /itemCat/status/{id}/{status}
- 请求类型: put
- 请求参数:
参数名称 | 参数说明 | 备注 |
---|---|---|
id | 用户ID值 | 不能为null |
status | 用户的状态信息 | 不能为null |
- 返回值: SysResult对象
参数名称 | 参数说明 | 备注 |
---|---|---|
status | 状态信息 | 200表示服务器请求成功 201表示服务器异常 |
msg | 服务器返回的提示信息 | 可以为null |
data | 服务器返回的业务数据 | 可以为null |
2.2.3 编辑ItemCatController
/** * 业务需求: 实现状态的修改 * 1.URL: /itemCat/status/{id}/{status} * 2.请求类型: put * 3.返回值: SysResult对象 */ @PutMapping("/status/{id}/{status}") public SysResult updateStatus(ItemCat itemCat){
itemCatService<span class="token punctuation">.</span><span class="token function">updateStatus</span><span class="token punctuation">(</span>itemCat<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token class-name">SysResult</span><span class="token punctuation">.</span><span class="token function">success</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
2.2.4 编辑ItemCatService
@Override public void updateStatus(ItemCat itemCat) {//id/status
itemCatMapper<span class="token punctuation">.</span><span class="token function">updateById</span><span class="token punctuation">(</span>itemCat<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
2.3 商品分类新增操作
2.3.1 业务说明
- 如果是一级菜单,则不需要勾选父级.
- 如果有父级,只能勾选1-2级.
2.3.2 页面JS说明
- 商品分类菜单信息 2级展现JS
async findParentItemCatList() { //动态获取商品分类信息 type=2表示获取2级商品分类信息 const { data: result } = await this.$http.get("/itemCat/findItemCatList/2") if (result.status !== 200) return this.$message.error("获取商品分类列表失败!!") this.parentItemCatList = result.data },
<span class="token comment">//2.发起Ajax请求,实现新增操作</span> async <span class="token function">addItemCatForm</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//先将整个表单进行校验</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">.</span>itemCatFormRef<span class="token punctuation">.</span><span class="token function">validate</span><span class="token punctuation">(</span>async validate <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">{<!-- --></span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>validate<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token keyword">const</span> <span class="token punctuation">{<!-- --></span> data<span class="token operator">:</span> result <span class="token punctuation">}</span> <span class="token operator">=</span> await <span class="token keyword">this</span><span class="token punctuation">.</span>$http<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">"/itemCat/saveItemCat"</span><span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>itemCatForm<span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>result<span class="token punctuation">.</span>status <span class="token operator">!=</span><span class="token operator">=</span> <span class="token number">200</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$message<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">"新增商品分类失败"</span><span class="token punctuation">)</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$message<span class="token punctuation">.</span><span class="token function">success</span><span class="token punctuation">(</span><span class="token string">"新增商品分类成功!!!"</span><span class="token punctuation">)</span> <span class="token comment">//新增成功,则刷新分类列表信息</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">findItemCatList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>addItemCatDialogVisible <span class="token operator">=</span> <span class="token boolean">false</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
2.3.3 业务接口文档说明
- 请求路径: /itemCat/saveItemCat
- 请求类型: post
- 请求参数: 表单数据
参数名称 | 参数说明 | 备注 |
---|---|---|
name | 商品分类名称 | 不能为null |
parentId | 用户父级ID | 不能为null |
level | 分类级别 | 1 2 3 商品分类级别 |
- 返回值: SysResult对象
参数名称 | 参数说明 | 备注 |
---|---|---|
status | 状态信息 | 200表示服务器请求成功 201表示服务器异常 |
msg | 服务器返回的提示信息 | 可以为null |
data | 服务器返回的业务数据 | 可以为null |
2.3.4 编辑ItemCatController
/** * 需求: 实现商品分类入库 * URL: /itemCat/saveItemCat * 类型: post类型 * 参数: itemCat对象~~~JSON * 返回值: SysResult */ @PostMapping("/saveItemCat") public SysResult saveItemCat(@RequestBody ItemCat itemCat){
itemCatService<span class="token punctuation">.</span><span class="token function">saveItemCat</span><span class="token punctuation">(</span>itemCat<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token class-name">SysResult</span><span class="token punctuation">.</span><span class="token function">success</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
2.3.5 编辑ItemCatServiceImpl
@Override @Transactional public void saveItemCat(ItemCat itemCat) {
itemCat<span class="token punctuation">.</span><span class="token function">setStatus</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> itemCatMapper<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span>itemCat<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2.4 商品分类删除操作
2.4.1 业务说明
1.如果被删除的标签是3级标签,则可以直接删除.
2.如果被删除的标签是2级标签,则需要先删除3级,再删除2级.
3.如果被删除的标签是1级标签,则需要先删除3级,在删除2级.最后删除1级.
2.4.2 页面JS分析
2.4.3 业务接口文档说明
- 请求路径: /itemCat/deleteItemCat
- 请求类型: delete
- 业务描述: 当删除节点为父级时,应该删除自身和所有的子节点
- 请求参数:
参数名称 | 参数说明 | 备注 |
---|---|---|
id | 用户id号 | 不能为null |
level | 商品分类级别 一级,二级,三级 |
- 返回值结果 SysResult对象
参数名称 | 参数说明 | 备注 |
---|---|---|
status | 状态信息 | 200表示服务器请求成功 201表示服务器异常 |
msg | 服务器返回的提示信息 | 可以为null |
data | 服务器返回的业务数据 | 可以为null |
2.4.4 编辑ItemCatController
/** * 商品分类删除操作 * URL: /itemCat/deleteItemCat?id=1&level=3 * 类型: delete * 参数: id/level * 返回值: SysResult对象 */ @DeleteMapping("/deleteItemCat") public SysResult deleteItemCat(Integer id,Integer level){
itemCatService<span class="token punctuation">.</span><span class="token function">deleteItemCat</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span>level<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token class-name">SysResult</span><span class="token punctuation">.</span><span class="token function">success</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
2.4.5 编辑ItemCatServiceImpl
/** * 1.易用性,性能高 * * @param id * @param level */ @Override @Transactional //添加事务的控制 public void deleteItemCat(Integer id, Integer level) { if(level == 3){ itemCatMapper.deleteById(id); }
<span class="token keyword">if</span><span class="token punctuation">(</span>level <span class="token operator">==</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//1.先删除3级菜单</span> <span class="token class-name">QueryWrapper</span> queryWrapper <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">QueryWrapper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> queryWrapper<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"parent_id"</span><span class="token punctuation">,</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span> itemCatMapper<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>queryWrapper<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2.再删除2级菜单</span> itemCatMapper<span class="token punctuation">.</span><span class="token function">deleteById</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//如果程序执行到这一行 则需要先删除3级/再删除2级/最后删除1级</span> <span class="token keyword">if</span><span class="token punctuation">(</span>level <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> ids <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//1.查询所有的二级菜单</span> <span class="token class-name">QueryWrapper</span> queryWrapper <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">QueryWrapper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> queryWrapper<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"parent_id"</span><span class="token punctuation">,</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">ItemCat</span><span class="token punctuation">></span></span> twoList <span class="token operator">=</span> itemCatMapper<span class="token punctuation">.</span><span class="token function">selectList</span><span class="token punctuation">(</span>queryWrapper<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">ItemCat</span> twoItemCat <span class="token operator">:</span> twoList<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> <span class="token comment">//删除3级菜单</span> queryWrapper<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> queryWrapper<span class="token punctuation">.</span><span class="token function">eq</span><span class="token punctuation">(</span><span class="token string">"parent_id"</span><span class="token punctuation">,</span>twoItemCat<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//delete from item_cat where parent_id = "二级ID"</span> itemCatMapper<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>queryWrapper<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//获取需要删除的ID信息</span> ids<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>twoItemCat<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//将所有的二级/一级ID封装到List集合中</span> ids<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span> itemCatMapper<span class="token punctuation">.</span><span class="token function">deleteBatchIds</span><span class="token punctuation">(</span>ids<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
3. 商品管理实现
3.1 表设计
1.item表设计 商品表(商品的基本信息)
2.item_desc 表设计 商品详情表 item_desc 专门保存大字段(html/url/图片网址…)
设计要求: item表与itemDesc表应该一对一. item.id = itemDesc.id
3.2.Item POJO对象设计
/**
* @author 刘昱江
* 时间 2021/4/7
*/
@TableName("item")
@Data
@Accessors(chain = true)
public class Item extends BasePojo{
@TableId(type = IdType.AUTO)
private Integer id; //商品Id号
private String title; //商品标题信息
private String sellPoint; //卖点信息
private Integer price; //商品价格 精度问题!!! 将小数扩大100倍 页面中将数据缩小100倍
private Integer num; //商品数量
private String images; //商品图片 1.jpg,2.jpg,3.jpg
private Integer itemCatId; //商品分类ID号
private Boolean status; //状态信息 0 下架 1 上架
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
3.3 商品列表展现
3.3.1 需求说明
要求: 当用户打开商品列表页面时,应该采用分页的方式 展现商品信息.
3.3.2 页面JS分析
3.3.3 业务接口文档实现
- 请求路径: /item/getItemList?query=&pageNum=1&pageSize=10
- 请求类型: get
- 请求参数: 使用pageResult对象接收
参数名称 | 参数说明 | 备注信息 |
---|---|---|
query | 用户查询的数据 | 可以为null |
pageNum | 分页查询的页数 | 必须赋值不能为null |
pageSize | 分页查询的条数 | 必须赋值不能为null |
- 返回值结果:
参数名称 | 参数说明 | 备注 |
---|---|---|
status | 状态信息 | 200表示服务器请求成功 201表示服务器异常 |
msg | 服务器返回的提示信息 | 可以为null |
data | 服务器返回的业务数据 | 商品分页对象 |
3.3.4 编辑ItemController
@RestController @CrossOrigin @RequestMapping("/item") public class ItemController {
<span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">ItemService</span> itemService<span class="token punctuation">;</span> <span class="token comment">/** * 业务逻辑: 查询商品分页 * URL: /item/getItemList?query=&pageNum=1&pageSize=10 * 类型: GET * 接收参数: PageResult对象 * 返回值: SysResult */</span> <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/getItemList"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> <span class="token class-name">SysResult</span> <span class="token function">getItemList</span><span class="token punctuation">(</span><span class="token class-name">PageResult</span> pageResult<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span><span class="token comment">//3个参数</span> <span class="token comment">//返回5个参数,total/rows分页记录</span> pageResult <span class="token operator">=</span> itemService<span class="token punctuation">.</span><span class="token function">getItemList</span><span class="token punctuation">(</span>pageResult<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token class-name">SysResult</span><span class="token punctuation">.</span><span class="token function">success</span><span class="token punctuation">(</span>pageResult<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
3.3.5 编辑ItemServiceImpl
@Service public class ItemServiceImpl implements ItemService{
<span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> <span class="token class-name">ItemMapper</span> itemMapper<span class="token punctuation">;</span> <span class="token comment">/** * 实现商品的分页操作 * @param pageResult * @return */</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token class-name">PageResult</span> <span class="token function">getItemList</span><span class="token punctuation">(</span><span class="token class-name">PageResult</span> pageResult<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token comment">//参数1:页数 参数2: 条数</span> <span class="token class-name">Page</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Item</span><span class="token punctuation">></span></span> page <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Page</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span>pageResult<span class="token punctuation">.</span><span class="token function">getPageNum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>pageResult<span class="token punctuation">.</span><span class="token function">getPageSize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">QueryWrapper</span> queryWrapper <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">QueryWrapper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//如果参数有值true 如果query参数没有值 false</span> <span class="token keyword">boolean</span> flag <span class="token operator">=</span> <span class="token class-name">StringUtils</span><span class="token punctuation">.</span><span class="token function">hasLength</span><span class="token punctuation">(</span>pageResult<span class="token punctuation">.</span><span class="token function">getQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> queryWrapper<span class="token punctuation">.</span><span class="token function">like</span><span class="token punctuation">(</span>flag<span class="token punctuation">,</span><span class="token string">"title"</span><span class="token punctuation">,</span>pageResult<span class="token punctuation">.</span><span class="token function">getQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//利用MP的分页API 实现查询</span> page <span class="token operator">=</span> itemMapper<span class="token punctuation">.</span><span class="token function">selectPage</span><span class="token punctuation">(</span>page<span class="token punctuation">,</span>queryWrapper<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">long</span> total <span class="token operator">=</span> page<span class="token punctuation">.</span><span class="token function">getTotal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Item</span><span class="token punctuation">></span></span> list <span class="token operator">=</span> page<span class="token punctuation">.</span><span class="token function">getRecords</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//封装成功之后原来3个参数 封装之后变为5个 之后返回</span> <span class="token keyword">return</span> pageResult<span class="token punctuation">.</span><span class="token function">setTotal</span><span class="token punctuation">(</span>total<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setRows</span><span class="token punctuation">(</span>list<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27