乐优商场项目day09—商品查询
在完成商品查询之前,我们先将图片信息导入。因为现在商品的图片数据还没有导入。
1.连接虚拟机或服务器。
在leyou下创建static目录 ,并将图片资源文件传进去。

2.解压图片资源压缩文件
![]()
unzip iamges.zip
3.修改Nginx配置,使nginx反向代理这些图片地址:
@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}
SPUDetail:
@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}
因为页面展示的商品分类和品牌名称,而数据库中保存的是相遇的id。
我们要新创建一个类,继承SPU,并且扩展Cname、bname属性:
(bo: business object ,业务对象,为满足业务而创建的对象。)

public class SpuBo extends Spu {String cname;// 商品分类名称String bname;// 品牌名称//记得生成getter,setter方法
}
2.添加通用mapper

import tk.mybatis.mapper.common.Mapper;public interface SpuMapper extends Mapper {
}
4.编写controller

@Controller
public class GoodsController {@Autowiredprivate GoodsService goodsService;@GetMapping("spu/page")public ResponseEntity> querySpuBoByPage(@RequestParam(value = "key",required = false)String key,@RequestParam(value = "saleable",required = false)Boolean saleable,@RequestParam(value = "page",required = false)Integer page,@RequestParam(value = "rows",required = false)Integer rows){PageResult pageResult = this.goodsService.querySpuBoByPage(key,saleable,page,rows);if (CollectionUtils.isEmpty(pageResult.getItems())){return ResponseEntity.notFound().build();}return ResponseEntity.ok(pageResult);}
}
5.编写service 并实现querySpuBoByPage方法

@Service
public class GoodsService {@Autowiredprivate SpuMapper spuMapper;@Autowiredprivate CategoryService categoryService;@Autowiredprivate BrandMapper brandMapper;public PageResult 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 spus = this.spuMapper.selectByExample(example);PageInfo pageInfo = new PageInfo<>(spus);List spuBos = new ArrayList<>();spus.forEach(spu->{SpuBo spuBo = new SpuBo();// copy共同属性的值到新的对象BeanUtils.copyProperties(spu, spuBo);// 查询分类名称List 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);}
}
其中的queryNamesByIds方法,需要我们去CatrgoryService中添加:

public List queryNamesByIds(List ids) {List list = this.categoryMapper.selectByIdList(ids);List names = new ArrayList<>();for (Category category : list) {names.add(category.getName());}return names;// return list.stream().map(category -> category.getName()).collect(Collectors.toList());
}
而queryNamesByIds方法中的 selectByIdList方法,我们可以用通用mapper中的,要去加个接口:

public interface CategoryMapper extends Mapper, SelectByIdListMapper {}
重启 leyou-item 服务,去页面测试:

商品查询成功了!
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
