一、数据库结构
create table `foodie-shop-dev`.items ( id varchar(64) not null comment '商品主键id' primary key, item_name varchar(32) not null comment '商品名称 商品名称', cat_id int not null comment '分类外键id 分类id', root_cat_id int not null comment '一级分类外键id', sell_counts int not null comment '累计销售 累计销售', on_off_status int not null comment '上下架状态 上下架状态,1:上架 2:下架', content text not null comment '商品内容 商品内容', created_time datetime not null comment '创建时间', updated_time datetime not null comment '更新时间' ) comment '商品表 商品信息相关表:分类表,商品图片表,商品规格表,商品参数表' charset = utf8mb4;
create table `foodie-shop-dev`.items_img ( id varchar(64) not null comment '图片主键' primary key, item_id varchar(64) not null comment '商品外键id 商品外键id', url varchar(128) not null comment '图片地址 图片地址', sort int not null comment '顺序 图片顺序,从小到大', is_main int not null comment '是否主图 是否主图,1:是,0:否', created_time datetime not null comment '创建时间', updated_time datetime not null comment '更新时间' ) comment '商品图片 ' charset = utf8mb4;
create table `foodie-shop-dev`.items_spec ( id varchar(64) not null comment '商品规格id' primary key, item_id varchar(64) not null comment '商品外键id', name varchar(32) not null comment '规格名称', stock int not null comment '库存', discounts decimal(4, 2) not null comment '折扣力度', price_discount int not null comment '优惠价', price_normal int not null comment '原价', created_time datetime not null comment '创建时间', updated_time datetime not null comment '更新时间' ) comment '商品规格 每一件商品都有不同的规格,不同的规格又有不同的价格和优惠力度,规格表为此设计' charset = utf8mb4;
create table `foodie-shop-dev`.items_param ( id varchar(64) not null comment '商品参数id' primary key, item_id varchar(32) not null comment '商品外键id', produc_place varchar(32) not null comment '产地 产地,例:中国江苏', foot_period varchar(32) not null comment '保质期 保质期,例:180天', brand varchar(32) not null comment '品牌名 品牌名,例:三只大灰狼', factory_name varchar(32) not null comment '生产厂名 生产厂名,例:大灰狼工厂', factory_address varchar(32) not null comment '生产厂址 生产厂址,例:大灰狼生产基地', packaging_method varchar(32) not null comment '包装方式 包装方式,例:袋装', weight varchar(32) not null comment '规格重量 规格重量,例:35g', storage_method varchar(32) not null comment '存储方法 存储方法,例:常温5~25°', eat_method varchar(32) not null comment '食用方式 食用方式,例:开袋即食', created_time datetime not null comment '创建时间', updated_time datetime not null comment '更新时间' ) comment '商品参数 ' charset = utf8mb4;
二、service模块
1.接口定义(路径:com/imooc/service/ItemService.java)
package com.imooc.service; import com.imooc.pojo.Items; import com.imooc.pojo.ItemsImg; import com.imooc.pojo.ItemsParam; import com.imooc.pojo.ItemsSpec; import java.util.List; public interface ItemService { /** * 根据商品ID查询详情 * @param itemId * @return */ public Items queryItemById(String itemId); /** * 根据商品ID查询图片列表 * @param itemId * @return */ public List<ItemsImg> queryItemImgList(String itemId); /** * 根据商品ID查询商品规格列表 * @param itemId * @return */ public List<ItemsSpec> queryItemSpecList(String itemId); /** * 根据商品ID查询商品参数 * @param itemId * @return */ public ItemsParam queryItemParam(String itemId); }
2.接口实现
(路径:com/imooc/service/impl/ItemServiceImpl.java)
package com.imooc.service.impl; import com.imooc.mapper.ItemsImgMapper; import com.imooc.mapper.ItemsMapper; import com.imooc.mapper.ItemsParamMapper; import com.imooc.mapper.ItemsSpecMapper; import com.imooc.pojo.Items; import com.imooc.pojo.ItemsImg; import com.imooc.pojo.ItemsParam; import com.imooc.pojo.ItemsSpec; import com.imooc.service.ItemService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import tk.mybatis.mapper.entity.Example; import java.util.List; @Service public class ItemServiceImpl implements ItemService { @Autowired ItemsMapper itemsMapper; @Autowired ItemsImgMapper itemsImgMapper; @Autowired ItemsSpecMapper itemsSpecMapper; @Autowired ItemsParamMapper itemsParamMapper; @Transactional(propagation = Propagation.SUPPORTS) @Override public Items queryItemById(String itemId) { return itemsMapper.selectByPrimaryKey(itemId); } @Transactional(propagation = Propagation.SUPPORTS) @Override public List<ItemsImg> queryItemImgList(String itemId) { Example itemsImgExp = new Example(ItemsImg.class); Example.Criteria criteria =itemsImgExp.createCriteria(); criteria.andEqualTo("itemId",itemId); return itemsImgMapper.selectByExample(itemsImgExp); } @Transactional(propagation = Propagation.SUPPORTS) @Override public List<ItemsSpec> queryItemSpecList(String itemId) { Example itemsSpecExp = new Example(ItemsSpec.class); Example.Criteria criteria =itemsSpecExp.createCriteria(); criteria.andEqualTo("itemId",itemId); return itemsSpecMapper.selectByExample(itemsSpecExp); } @Transactional(propagation = Propagation.SUPPORTS) @Override public ItemsParam queryItemParam(String itemId) { Example itemsParamExp = new Example(ItemsParam.class); Example.Criteria criteria =itemsParamExp.createCriteria(); criteria.andEqualTo("itemId",itemId); return itemsParamMapper.selectOneByExample(itemsParamExp); } }
三、Api模块
路径:com/imooc/controller/ItemController.java
package com.imooc.controller; import com.imooc.enums.YesOrNo; import com.imooc.pojo.*; import com.imooc.pojo.vo.CategoryVO; import com.imooc.pojo.vo.ItemInfoVO; import com.imooc.pojo.vo.NewItemsVO; import com.imooc.service.CarouselService; import com.imooc.service.CategoryService; import com.imooc.service.ItemService; import com.imooc.utils.IMOOCJSONResult; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @Api(value = "商品接口",tags = "商品信息展示的相关接口") @RestController @RequestMapping("item") public class ItemController { @Autowired private ItemService itemService; @ApiOperation(value="查询商品详情",notes = "查询商品详情",httpMethod = "GET") @GetMapping("/info/{itemId}") public IMOOCJSONResult info( @ApiParam(name = "itemId",value = "商品ID",required = true) @PathVariable() String itemId) { if (StringUtils.isBlank(itemId)) { return IMOOCJSONResult.errorMsg(""); } Items item = itemService.queryItemById(itemId); List<ItemsImg> itemImgList=itemService.queryItemImgList(itemId); List<ItemsSpec> itemSpecList=itemService.queryItemSpecList(itemId); ItemsParam itemParam=itemService.queryItemParam(itemId); ItemInfoVO itemInfoVO=new ItemInfoVO(); itemInfoVO.setItem(item); itemInfoVO.setItemImgList(itemImgList); itemInfoVO.setItemSpecList(itemSpecList); itemInfoVO.setItemParam(itemParam); return IMOOCJSONResult.ok(itemInfoVO); } }
四、商品详情VO定义(用户返回到前端)
路径:com/imooc/pojo/vo/ItemInfoVO.java
package com.imooc.pojo.vo; import com.imooc.pojo.Items; import com.imooc.pojo.ItemsImg; import com.imooc.pojo.ItemsParam; import com.imooc.pojo.ItemsSpec; import java.util.List; /** * 商品详情VO */ public class ItemInfoVO { public Items getItem() { return item; } public void setItem(Items item) { this.item = item; } public List<ItemsImg> getItemImgList() { return itemImgList; } public void setItemImgList(List<ItemsImg> itemImgList) { this.itemImgList = itemImgList; } public List<ItemsSpec> getItemSpecList() { return itemSpecList; } public void setItemSpecList(List<ItemsSpec> itemSpecList) { this.itemSpecList = itemSpecList; } public ItemsParam getItemParam() { return itemParam; } public void setItemParam(ItemsParam itemParam) { this.itemParam = itemParam; } private Items item; private List<ItemsImg> itemImgList; private List<ItemsSpec> itemSpecList; private ItemsParam itemParam; }