一、实现添加商品的接口
1. 在common_item服务中实现商品分类查询
1.1 创建controller
package com.bjsxt.item.controller;
import com.bjsxt.item.service.ItemCategoryService;
import com.bjsxt.pojo.TbItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/service/itemCategory")
public class ItemCategoryController {
@Autowired
private ItemCategoryService itemCategoryService;
@RequestMapping("/selectItemCategoryByParentId")
public List<TbItemCat> selectItemCategoryByParentId(@RequestParam Long id){
return itemCategoryService.selectItemCategoryByParentId(id);
}
}
1.2 创建service
package com.bjsxt.item.service;
import com.bjsxt.pojo.TbItemCat;
import java.util.List;
public interface ItemCategoryService {
List<TbItemCat> selectItemCategoryByParentId(Long id);
}
1.3 创建serviceImpl
package com.bjsxt.item.service.impl;
import com.bjsxt.item.service.ItemCategoryService;
import com.bjsxt.mapper.TbItemCatMapper;
import com.bjsxt.pojo.TbItemCat;
import com.bjsxt.pojo.TbItemCatExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ItemCategoryServiceImpl implements ItemCategoryService {
@Autowired
private TbItemCatMapper tbItemCatMapper;
@Override
public List<TbItemCat> selectItemCategoryByParentId(Long id) {
TbItemCatExample example = new TbItemCatExample();
example.createCriteria().andParentIdEqualTo(id).andStatusEqualTo(1);
List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(example);
return tbItemCats;
}
}
测试:父节点为1的所有的子节点数据。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ad65cd1aa83d99b6369fc9a0802cf886.png)
2. 在backend_item服务中实现商品分类查询
2.1 创建controller
package com.bjsxt.backenditem.controller;
import com.bjsxt.backenditem.service.ItemCategoryService;
import com.bjsxt.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/backend/itemCategory")
public class ItemCategoryController {
@Autowired
private ItemCategoryService itemCategoryService;
@RequestMapping("/selectItemCategoryByParentId")
public Result selectItemCategoryByParentId(@RequestParam(value = "id",defaultValue = "0") Long id){
try{
Result result = itemCategoryService.selectItemCategoryByParentId(id);
return result;
}catch (Exception e){
e.printStackTrace();
}
return Result.build(500,"error");
}
}
2.2 创建service
package com.bjsxt.backenditem.service;
import com.bjsxt.utils.Result;
public interface ItemCategoryService {
Result selectItemCategoryByParentId(Long id);
}
2.3 创建serviceImpl
package com.bjsxt.backenditem.service.impl;
import com.bjsxt.backenditem.feign.CommonItemFeignClient;
import com.bjsxt.backenditem.service.ItemCategoryService;
import com.bjsxt.pojo.TbItemCat;
import com.bjsxt.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ItemCategoryServiceImpl implements ItemCategoryService {
@Autowired
private CommonItemFeignClient commonItemFeignClient;
@Override
public Result selectItemCategoryByParentId(Long id) {
List<TbItemCat> tbItemCats = commonItemFeignClient.selectTbItemAllByPage(id);
System.out.println(tbItemCats);
if(tbItemCats.size()>0 && tbItemCats!=null){
return Result.ok(tbItemCats);
}
return Result.error("查无结果");
}
}
2.4 创建FeignClient
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cf691e9f682bcd99241f0b481022d48e.png)
2.5 测试
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/36682a7830059080c531568532080b3e.png)