商品详情功能开发
(1)搭建service-item
点击service,选择New–>Module,操作如下
修改配置pom.xml
添加配置文件bootstrap.properties
spring.application.name=service-item
spring.profiles.active=dev
spring.cloud.nacos.discovery.server-addr=192.168.254.165:8848
spring.cloud.nacos.config.server-addr=192.168.254.165:8848
spring.cloud.nacos.config.prefix=${spring.application.name}
spring.cloud.nacos.config.file-extension=yaml
spring.cloud.nacos.config.shared-configs[0].data-id=common.yaml
添加启动类
因为父模块引入了mysql的依赖,由于自动配置,如果没有配置会报错,我们把他关了
exclude = DataSourceAutoConfiguration.class 排除数据库链接jar
表示当前项目{service-item} 不参与数据库查询
package com.atguigu.gmall.item;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消数据源自动配置
@ComponentScan({"com.atguigu.gmall"})
@EnableDiscoveryClient
@EnableFeignClients(basePackages= {"com.atguigu.gmall"})
public class ServiceItemApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceItemApplication.class, args);
}
}
service-item服务接口封装
package com.atguigu.gmall.item.service;
public interface ItemService {
/**
* 获取sku详情信息
* @param skuId
* @return
*/
Map<String, Object> getBySkuId(Long skuId);
}
实现类:
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private ProductFeignClient productFeignClient;
@Override
public Map<String, Object> getBySkuId(Long skuId) {
Map<String, Object> result = new HashMap<>();
return result;
}
}
@RestController
@RequestMapping("api/item")
public class ItemApiController {
@Autowired
private ItemService itemService;
/**
* 获取sku详情信息
* @param skuId
* @return
*/
@GetMapping("{skuId}")
public Result getItem(@PathVariable Long skuId){
Map<String,Object> result = itemService.getBySkuId(skuId);
return Result.ok(result);
}
}
说明:商品详情相关信息在service-product微服务都可以获取,所以我们在service-product模块编写所需要的接口
在service-product微服务提供api接口
(2)获取sku基本信息与图片信息
实体类:
package com.atguigu.gmall.model.product;
import com.atguigu.gmall.model.base.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
/**
* <p>
* SpuInfo
* </p>
*
*/
@Data
@ApiModel(description = "SpuInfo")
@TableName("spu_info")
public class SpuInfo extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "商品名称")
@TableField("spu_name")
private String spuName;
@ApiModelProperty(value = "商品描述(后台简述)")
@TableField("description")
private String description;
@ApiModelProperty(value = "三级分类id")
@TableField("category3_id")
private Long category3Id;
@ApiModelProperty(value = "品牌id")
@TableField("tm_id")
private Long tmId;
// 销售属性集合
@TableField(exist = false)
private List<SpuSaleAttr> spuSaleAttrList;
// 商品的图片集合
@TableField(exist = false)
private List<SpuImage> spuImageList;
// 商品的海报图片集合
@TableField(exist = false)
private List<SpuPoster> spuPosterList;
}
编写接口与实现类
在ManageService接口中添加
/**
* 根据skuId 查询skuInfo
* @param skuId
* @return
*/
SkuInfo getSkuInfo(Long skuId);
实现类
@Override
public SkuInfo getSkuInfo(Long skuId) {
SkuInfo skuInfo = skuInfoMapper.selectById(skuId);
// 根据skuId 查询图片列表集合
QueryWrapper<SkuImage> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("sku_id", skuId);
List<SkuImage> skuImageList = skuImageMapper.selectList(queryWrapper);
skuInfo.setSkuImageList(skuImageList);
return skuInfo;
}
编写控制器
package com.atguigu.gmall.product.api.ProductApiController
@RestController
@RequestMapping("api/product")
public class ProductApiController {
@Autowired
private ManageService manageService;
/**
* 根据skuId获取sku信息
* @param skuId
* @return
*/
@GetMapping("inner/getSkuInfo/{skuId}")
public SkuInfo getAttrValueList(@PathVariable("skuId") Long skuId){
SkuInfo skuInfo = manageService.getSkuInfo(skuId);
return skuInfo;
}
(3)获取分类信息(查看三级分类)
需求分析
sku是挂在三级分类下面的,我们的分类信息分别在base_category1、base_category2、base_category3这三张表里面,目前需要通过sku表的三级分类id获取一级分类名称、二级分类名称和三级分类名称
MySQL 视图(View)是一种虚拟存在的表,同真实表一样,视图也由列和行构成,但视图并不实际存在于数据库中。行和列的数据来自于定义视图的查询中所使用的表,并且还是在使用视图时动态生成的。 特点: 数据库中只存放了视图的定义,并没有存放视图中的数据,这些数据都存放在定义视图查询所引用的真实表中。 |
解决方案:
我们可以建立一个视图(view),把三张表关联起来,视图id就是三级分类id,这样通过三级分类id就可以查询到相应数据,效果如下:
创建视图 CREATE VIEW base_category_view AS select c3.id as id, c1.id as category1_id, c1.name as category1_name, c2.id as category2_id, c2.name as category2_name, c3.id as category3_id, c3.name as category3_name from base_category1 c1 inner join base_category2 c2 on c2.category1_id = c1.id inner join base_category3 c3 on c3.category2_id = c2.id |
对应实体类:
//
//
package com.atguigu.gmall.model.product;
import com.atguigu.gmall.model.base.BaseEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* <p>
* BaseCategoryView
* </p>
*
*/
@Data
@ApiModel(description = "BaseCategoryView")
@TableName("base_category_view")
public class BaseCategoryView implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "id")
private Long id;
@ApiModelProperty(value = "一级分类编号")
@TableField("category1_id")
private Long category1Id;
@ApiModelProperty(value = "一级分类名称")
@TableField("category1_name")
private String category1Name;
@ApiModelProperty(value = "二级分类编号")
@TableField("category2_id")
private Long category2Id;
@ApiModelProperty(value = "二级分类名称")
@TableField("category2_name")
private String category2Name;
@ApiModelProperty(value = "三级分类编号")
@TableField("category3_id")
private Long category3Id;
@ApiModelProperty(value = "三级分类名称")
@TableField("category3_name")
private String category3Name;
}
创建mapper:BaseCategoryViewMapper
@Mapper
public interface BaseCategoryViewMapper extends BaseMapper<BaseCategoryView> {
}
ManageService接口
/**
* 通过三级分类id查询分类信息
* @param category3Id
* @return
*/
BaseCategoryView getCategoryViewByCategory3Id(Long category3Id);
接口实现
@Autowired
private BaseCategoryViewMapper baseCategoryViewMapper;
@Override
public BaseCategoryView getCategoryViewByCategory3Id(Long category3Id) {
return baseCategoryViewMapper.selectById(category3Id);
}
编写控制器ProductApiController
/**
* 通过三级分类id查询分类信息
* @param category3Id
* @return
*/
@GetMapping("inner/getCategoryView/{category3Id}")
public BaseCategoryView getCategoryView(@PathVariable("category3Id")Long category3Id){
return manageService.getCategoryViewByCategory3Id(category3Id);
}