欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-14 23:21:32
...

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

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

1.1 创建controller

package com.bjsxt.item.controller;

import com.bjsxt.item.service.ItemParamService;
import com.bjsxt.pojo.TbItemParam;
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("/service/itemParam")
public class ItemParamController {
    @Autowired
    private ItemParamService itemParamService;

    /**
     * 查询商品规格参数模板
     */
    @RequestMapping("/selectItemParamByItemCatId")
    public TbItemParam selectItemParamByItemCatId(@RequestParam  Long itemCatId){
        TbItemParam tbItemParam = itemParamService.selectItemParamByItemCatId(itemCatId);
        return  tbItemParam;
    }
}

1.2 创建service

package com.bjsxt.item.service;

import com.bjsxt.pojo.TbItemParam;
import org.springframework.web.bind.annotation.RequestParam;

public interface ItemParamService {
    TbItemParam selectItemParamByItemCatId( Long itemCatId);
}

1.3 创建serviceImpl

package com.bjsxt.item.service.impl;

import com.bjsxt.item.service.ItemParamService;
import com.bjsxt.mapper.TbItemParamMapper;
import com.bjsxt.pojo.TbItemParam;
import com.bjsxt.pojo.TbItemParamExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemParamServiceImpl implements ItemParamService {
    @Autowired
    private TbItemParamMapper tbItemParamMapper;

    /**
     * 查询商品规格参数的模板
     * @param itemCatId
     * @return
     */
    @Override
    public TbItemParam selectItemParamByItemCatId(Long itemCatId) {
        TbItemParamExample example = new TbItemParamExample();
        example.createCriteria().andItemCatIdEqualTo(itemCatId);
        List<TbItemParam> tbItemParams = tbItemParamMapper.selectByExampleWithBLOBs(example);
        if(tbItemParams.size()>0 && tbItemParams!=null ){
            return  tbItemParams.get(0);
        }
        return null;
    }
}

1.4 使用PostMan测试

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

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

2.1 创建controller

在传递参数的时候注意
电商项目(七)----实现商品规格参数模板的查询

package com.bjsxt.backenditem.controller;

import com.bjsxt.backenditem.service.ItemParamService;
import com.bjsxt.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/backend/itemParam")
public class ItemParamController {
    @Autowired
    private ItemParamService itemParamService;
    /**
     * 根据商品id查询商品的规格参数
     * @param itemCatId
     * @return
     */
    @RequestMapping("/selectItemParamByItemCatId/{itemCatId}")
    public Result selectItemParamByItemCatId(@PathVariable Long itemCatId){
        try{
            Result result = itemParamService.selectItemParamByItemCatId(itemCatId);
            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 ItemParamService {
    Result selectItemParamByItemCatId(Long itemCatId);
}

2.3 创建serviceImpl

package com.bjsxt.backenditem.service.impl;

import com.bjsxt.backenditem.feign.CommonItemFeignClient;
import com.bjsxt.backenditem.service.ItemParamService;
import com.bjsxt.pojo.TbItemParam;
import com.bjsxt.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemParamServiceImpl implements ItemParamService {
    @Autowired
    private CommonItemFeignClient commonItemFeignClient;
    @Override
    public Result selectItemParamByItemCatId(Long itemCatId) {
        TbItemParam tbItemParam = commonItemFeignClient.selectItemParamByItemCatId(itemCatId);
        System.out.println("++++++++++"+tbItemParam);
        if(tbItemParam!=null){
            return Result.ok(tbItemParam);

        }
        return Result.error("查无结果");
    }
}

2.4 修改FeignClient

package com.bjsxt.backenditem.feign;

import com.bjsxt.pojo.TbItemCat;
import com.bjsxt.pojo.TbItemParam;
import com.bjsxt.utils.PageResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@FeignClient(value = "common-item")
public interface CommonItemFeignClient {

    //-------------/service.Item
    @GetMapping("/service/item/selectTbItemAllByPage")
     PageResult selectTbItemAllByPage(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows);

    //-------------/service.ItemCategory
    @PostMapping("/service/itemCategory/selectItemCategoryByParentId")
    List<TbItemCat> selectTbItemAllByPage(@RequestParam("id") Long id);

    //-------------/service.ItemParam
    @PostMapping("/service/itemParam/selectItemParamByItemCatId")
    TbItemParam selectItemParamByItemCatId(@RequestParam("itemCatId") Long itemCatId);
}


相关标签: 电商