日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

商品查询

發布時間:2024/4/13 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 商品查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實體類

在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

public class SpuBo extends Spu {String cname;// 商品分類名稱String bname;// 品牌名稱// 略 。。 }

編寫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年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的商品查询的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。