电商项目(六)----后台添加商品中的查询商品分类
程序员文章站
2022-04-14 23:17:59
...
一、实现添加商品的接口
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;
/**
* 根据父节点查询子节点
* @param id
* @return
*/
@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的所有的子节点数据。
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;
/**
* 查询商品类目:根据类目id,查询当前类目的子节点
*/
@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
2.5 测试
上一篇: 电商项目(十五)-前台项目的首页的商品分类查询和大广告位的显示
下一篇: 电商项目(二十三)订单