电商项目(六)----后台添加商品中的查询商品分类

青旅半醒 2023-07-03 06:10 154阅读 0赞

一、实现添加商品的接口

1. 在common_item服务中实现商品分类查询

1.1 创建controller

  1. package com.bjsxt.item.controller;
  2. import com.bjsxt.item.service.ItemCategoryService;
  3. import com.bjsxt.pojo.TbItemCat;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import java.util.List;
  9. @RestController
  10. @RequestMapping("/service/itemCategory")
  11. public class ItemCategoryController {
  12. @Autowired
  13. private ItemCategoryService itemCategoryService;
  14. /** * 根据父节点查询子节点 */
  15. @RequestMapping("/selectItemCategoryByParentId")
  16. public List<TbItemCat> selectItemCategoryByParentId(@RequestParam Long id){
  17. return itemCategoryService.selectItemCategoryByParentId(id);
  18. }
  19. }

1.2 创建service

  1. package com.bjsxt.item.service;
  2. import com.bjsxt.pojo.TbItemCat;
  3. import java.util.List;
  4. public interface ItemCategoryService {
  5. //根据父节点查询子节点 商品类目
  6. List<TbItemCat> selectItemCategoryByParentId(Long id);
  7. }

1.3 创建serviceImpl

  1. package com.bjsxt.item.service.impl;
  2. import com.bjsxt.item.service.ItemCategoryService;
  3. import com.bjsxt.mapper.TbItemCatMapper;
  4. import com.bjsxt.pojo.TbItemCat;
  5. import com.bjsxt.pojo.TbItemCatExample;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import java.util.List;
  9. @Service
  10. public class ItemCategoryServiceImpl implements ItemCategoryService {
  11. @Autowired
  12. private TbItemCatMapper tbItemCatMapper;
  13. /** * 根据父节点查询子节点 * @param id * @return */
  14. @Override
  15. public List<TbItemCat> selectItemCategoryByParentId(Long id) {
  16. TbItemCatExample example = new TbItemCatExample();
  17. example.createCriteria().andParentIdEqualTo(id).andStatusEqualTo(1);
  18. List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(example);
  19. return tbItemCats;
  20. }
  21. }

测试:父节点为1的所有的子节点数据。

在这里插入图片描述

2. 在backend_item服务中实现商品分类查询

2.1 创建controller

  1. package com.bjsxt.backenditem.controller;
  2. import com.bjsxt.backenditem.service.ItemCategoryService;
  3. import com.bjsxt.utils.Result;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. @RequestMapping("/backend/itemCategory")
  10. public class ItemCategoryController {
  11. @Autowired
  12. private ItemCategoryService itemCategoryService;
  13. /** * 查询商品类目:根据类目id,查询当前类目的子节点 */
  14. @RequestMapping("/selectItemCategoryByParentId")
  15. public Result selectItemCategoryByParentId(@RequestParam(value = "id",defaultValue = "0") Long id){
  16. try{
  17. Result result = itemCategoryService.selectItemCategoryByParentId(id);
  18. return result;
  19. }catch (Exception e){
  20. e.printStackTrace();
  21. }
  22. return Result.build(500,"error");
  23. }
  24. }

2.2 创建service

  1. package com.bjsxt.backenditem.service;
  2. import com.bjsxt.utils.Result;
  3. public interface ItemCategoryService {
  4. Result selectItemCategoryByParentId(Long id);
  5. }

2.3 创建serviceImpl

  1. package com.bjsxt.backenditem.service.impl;
  2. import com.bjsxt.backenditem.feign.CommonItemFeignClient;
  3. import com.bjsxt.backenditem.service.ItemCategoryService;
  4. import com.bjsxt.pojo.TbItemCat;
  5. import com.bjsxt.utils.Result;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import java.util.List;
  9. @Service
  10. public class ItemCategoryServiceImpl implements ItemCategoryService {
  11. @Autowired
  12. private CommonItemFeignClient commonItemFeignClient;
  13. @Override
  14. public Result selectItemCategoryByParentId(Long id) {
  15. List<TbItemCat> tbItemCats = commonItemFeignClient.selectTbItemAllByPage(id);
  16. System.out.println(tbItemCats);
  17. if(tbItemCats.size()>0 && tbItemCats!=null){
  18. return Result.ok(tbItemCats);
  19. }
  20. return Result.error("查无结果");
  21. }
  22. }

2.4 创建FeignClient

在这里插入图片描述

2.5 测试

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,154人围观)

还没有评论,来说两句吧...

相关阅读