商品查询
實體類
在learn-item-interface工程中添加實體類:
SPU
@Table(name = "tb_spu") public class Spu {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private Long brandId;private Long cid1;// 1級類目private Long cid2;// 2級類目private Long cid3;// 3級類目private String title;// 標題private String subTitle;// 子標題private Boolean saleable;// 是否上架private Boolean valid;// 是否有效,邏輯刪除用private Date createTime;// 創建時間private Date lastUpdateTime;// 最后修改時間// 省略getter和setter }?SPU詳情
@Table(name="tb_spu_detail") public class SpuDetail {@Idprivate Long spuId;// 對應的SPU的idprivate String description;// 商品描述private String specialSpec;// 商品特殊規格的名稱及可選值模板private String genericSpec;// 商品的全局規格屬性private String packingList;// 包裝清單private String afterService;// 售后服務// 省略getter和setter }mapper
public interface SpuMapper extends Mapper<Spu> { }controller
先分析:
-
請求方式:GET
-
請求路徑:/spu/page
-
請求參數:
-
page:當前頁
-
rows:每頁大小
-
key:過濾條件
-
saleable:上架或下架
-
-
返回結果:商品SPU的分頁信息。
-
要注意,頁面展示的是商品分類和品牌名稱,而數據庫中保存的是id,怎么辦?
我們可以新建一個類,繼承SPU,并且拓展cname和bname屬性,寫到learn-item-interface
-
編寫controller代碼:
我們把與商品相關的一切業務接口都放到一起,起名為GoodsController,業務層也是這樣
@Controller public class GoodsController {@Autowiredprivate GoodsService goodsService;@GetMapping("spu/page")public ResponseEntity<PageResult<SpuBo>> querySpuBoByPage(@RequestParam(value = "key", required = false)String key,@RequestParam(value = "saleable", required = false)Boolean saleable,@RequestParam(value = "page", defaultValue = "1")Integer page,@RequestParam(value = "rows", defaultValue = "5")Integer rows){PageResult<SpuBo> pageResult = this.goodsService.querySpuBoByPage(key, saleable, page, rows);if(CollectionUtils.isEmpty(pageResult.getItems())){return ResponseEntity.notFound().build();}return ResponseEntity.ok(pageResult);}}service
所有商品相關的業務(包括SPU和SKU)放到一個業務下:GoodsService。
@Service public class GoodsService {@Autowiredprivate SpuMapper spuMapper;@Autowiredprivate CategoryService categoryService;@Autowiredprivate BrandMapper brandMapper;public PageResult<SpuBo> querySpuBoByPage(String key, Boolean saleable, Integer page, Integer rows) {Example example = new Example(Spu.class);Example.Criteria criteria = example.createCriteria();// 搜索條件if (StringUtils.isNotBlank(key)) {criteria.andLike("title", "%" + key + "%");}if (saleable != null) {criteria.andEqualTo("saleable", saleable);}// 分頁條件PageHelper.startPage(page, rows);// 執行查詢List<Spu> spus = this.spuMapper.selectByExample(example);PageInfo<Spu> pageInfo = new PageInfo<>(spus);List<SpuBo> spuBos = new ArrayList<>();spus.forEach(spu->{SpuBo spuBo = new SpuBo();// copy共同屬性的值到新的對象BeanUtils.copyProperties(spu, spuBo);// 查詢分類名稱List<String> names = this.categoryService.queryNamesByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));spuBo.setCname(StringUtils.join(names, "/"));// 查詢品牌的名稱spuBo.setBname(this.brandMapper.selectByPrimaryKey(spu.getBrandId()).getName());spuBos.add(spuBo);});return new PageResult<>(pageInfo.getTotal(), spuBos);} }Category中拓展查詢名稱的功能
頁面需要商品的分類名稱需要在這里查詢,因此要額外提供查詢分類名稱的功能,
在CategoryService中添加功能:
public List<String> queryNamesByIds(List<Long> ids) {List<Category> list = this.categoryMapper.selectByIdList(ids);List<String> names = new ArrayList<>();for (Category category : list) {names.add(category.getName());}return names;// return list.stream().map(category -> category.getName()).collect(Collectors.toList()); }mapper的selectByIdList方法是來自于通用mapper。不過需要我們在mapper上繼承一個通用mapper接口:
public interface CategoryMapper extends Mapper<Category>, SelectByIdListMapper<Category, Long> { }測試
刷新頁面,查看效果:
基本與預覽的效果一致,OK!
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
- 上一篇: 规格参数查询后台代码实现
- 下一篇: 品牌新增页面