天天看點

電商項目(七)----實作商品規格參數模闆的查詢

一、實作商品規格參數模闆的查詢

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);
}