电商项目(七)----实现商品规格参数模板的查询

红太狼 2023-07-03 06:11 137阅读 0赞

一、实现商品规格参数模板的查询

1. 在common-item中实现商品规格参数模板的查询

1.1 创建controller

  1. package com.bjsxt.item.controller;
  2. import com.bjsxt.item.service.ItemParamService;
  3. import com.bjsxt.pojo.TbItemParam;
  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("/service/itemParam")
  10. public class ItemParamController {
  11. @Autowired
  12. private ItemParamService itemParamService;
  13. /**
  14. * 查询商品规格参数模板
  15. */
  16. @RequestMapping("/selectItemParamByItemCatId")
  17. public TbItemParam selectItemParamByItemCatId(@RequestParam Long itemCatId){
  18. TbItemParam tbItemParam = itemParamService.selectItemParamByItemCatId(itemCatId);
  19. return tbItemParam;
  20. }
  21. }

1.2 创建service

  1. package com.bjsxt.item.service;
  2. import com.bjsxt.pojo.TbItemParam;
  3. import org.springframework.web.bind.annotation.RequestParam;
  4. public interface ItemParamService {
  5. TbItemParam selectItemParamByItemCatId( Long itemCatId);
  6. }

1.3 创建serviceImpl

  1. package com.bjsxt.item.service.impl;
  2. import com.bjsxt.item.service.ItemParamService;
  3. import com.bjsxt.mapper.TbItemParamMapper;
  4. import com.bjsxt.pojo.TbItemParam;
  5. import com.bjsxt.pojo.TbItemParamExample;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import java.util.List;
  9. @Service
  10. public class ItemParamServiceImpl implements ItemParamService {
  11. @Autowired
  12. private TbItemParamMapper tbItemParamMapper;
  13. /**
  14. * 查询商品规格参数的模板
  15. * @param itemCatId
  16. * @return
  17. */
  18. @Override
  19. public TbItemParam selectItemParamByItemCatId(Long itemCatId) {
  20. TbItemParamExample example = new TbItemParamExample();
  21. example.createCriteria().andItemCatIdEqualTo(itemCatId);
  22. List<TbItemParam> tbItemParams = tbItemParamMapper.selectByExampleWithBLOBs(example);
  23. if(tbItemParams.size()>0 && tbItemParams!=null ){
  24. return tbItemParams.get(0);
  25. }
  26. return null;
  27. }
  28. }

1.4 使用PostMan测试

在这里插入图片描述

2. 在backend_item服务中实现规格参数模板查询

2.1 创建controller

在传递参数的时候注意
在这里插入图片描述

  1. package com.bjsxt.backenditem.controller;
  2. import com.bjsxt.backenditem.service.ItemParamService;
  3. import com.bjsxt.utils.Result;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.PathVariable;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. @RestController
  9. @RequestMapping("/backend/itemParam")
  10. public class ItemParamController {
  11. @Autowired
  12. private ItemParamService itemParamService;
  13. /**
  14. * 根据商品id查询商品的规格参数
  15. * @param itemCatId
  16. * @return
  17. */
  18. @RequestMapping("/selectItemParamByItemCatId/{itemCatId}")
  19. public Result selectItemParamByItemCatId(@PathVariable Long itemCatId){
  20. try{
  21. Result result = itemParamService.selectItemParamByItemCatId(itemCatId);
  22. return result;
  23. }catch (Exception e){
  24. e.printStackTrace();
  25. }
  26. return Result.build(500,"error");
  27. }
  28. }

2.2 创建service

  1. package com.bjsxt.backenditem.service;
  2. import com.bjsxt.utils.Result;
  3. public interface ItemParamService {
  4. Result selectItemParamByItemCatId(Long itemCatId);
  5. }

2.3 创建serviceImpl

  1. package com.bjsxt.backenditem.service.impl;
  2. import com.bjsxt.backenditem.feign.CommonItemFeignClient;
  3. import com.bjsxt.backenditem.service.ItemParamService;
  4. import com.bjsxt.pojo.TbItemParam;
  5. import com.bjsxt.utils.Result;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. @Service
  9. public class ItemParamServiceImpl implements ItemParamService {
  10. @Autowired
  11. private CommonItemFeignClient commonItemFeignClient;
  12. @Override
  13. public Result selectItemParamByItemCatId(Long itemCatId) {
  14. TbItemParam tbItemParam = commonItemFeignClient.selectItemParamByItemCatId(itemCatId);
  15. System.out.println("++++++++++"+tbItemParam);
  16. if(tbItemParam!=null){
  17. return Result.ok(tbItemParam);
  18. }
  19. return Result.error("查无结果");
  20. }
  21. }

2.4 修改FeignClient

  1. package com.bjsxt.backenditem.feign;
  2. import com.bjsxt.pojo.TbItemCat;
  3. import com.bjsxt.pojo.TbItemParam;
  4. import com.bjsxt.utils.PageResult;
  5. import org.springframework.cloud.openfeign.FeignClient;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.RequestParam;
  9. import java.util.List;
  10. @FeignClient(value = "common-item")
  11. public interface CommonItemFeignClient {
  12. //-------------/service.Item
  13. @GetMapping("/service/item/selectTbItemAllByPage")
  14. PageResult selectTbItemAllByPage(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows);
  15. //-------------/service.ItemCategory
  16. @PostMapping("/service/itemCategory/selectItemCategoryByParentId")
  17. List<TbItemCat> selectTbItemAllByPage(@RequestParam("id") Long id);
  18. //-------------/service.ItemParam
  19. @PostMapping("/service/itemParam/selectItemParamByItemCatId")
  20. TbItemParam selectItemParamByItemCatId(@RequestParam("itemCatId") Long itemCatId);
  21. }

发表评论

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

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

相关阅读