1.商品查询
1.1.基本代码实现
-
实体类
Spu实体类:
@Table(name = "tb_spu") @Data public class Spu { @Id @KeySql(useGeneratedKeys = true) private Long id; private Long BrandId; private Long cid1; //一级类目 private Long cid2; //二级类目 private Long cid3; //三级类目 private String title; //标题 private String SubTitle; //子标题 private Boolean saleable; //是否上架 private Boolean valid; //是否有效,逻辑删除用 private Date createTime; //创建时间 private Date lastUpdateTime; //最后修改时间 }
SpuDetail实体类:
@Data @Table(name = "tb_spu_detail") public class SpuDetail { private Long spuId; //对应SPU的id private String description; //商品描述 private String specialSpec; //商品特殊规格的名称及可选值模板 private String genericSpec; //商品的全局规格属性 private String packingList; //包装清单 private String afterService; //售后服务 }
-
mapper
public interface SpuMapper extends Mapper<Spu> { } public interface SpuDetailMapper extends Mapper<SpuDetail> { }
-
service
@Service public class GoodsService { @Autowired private SpuMapper spuMapper; @Autowired private SpuDetailMapper spuDetailMapper; }
-
controller
@RestController @RequestMapping public class GoodsController { }
1.2.controller分析
先分析:
-
请求方式:GET
-
请求路径:/spu/page
-
请求参数:
- page:当前页
- rows:每页大小
- key:过滤条件
- saleable:上架或者下架
-
返回结果:商品spu的分页信息PageResult。
-
要注意:
返回结果忽略某个属性可以使用@JsonIgnore,需引依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.6</version> </dependency>
页面展示的是商品分类和品牌名称,而数据库中保存的是id,怎么办?
我们可以对Spu拓展cname和bname,写到ly-item-interface
@Table(name = "tb_spu") @Data public class Spu { @Id @KeySql(useGeneratedKeys = true) private Long id; private Long BrandId; private Long cid1; //一级类目 private Long cid2; //二级类目 private Long cid3; //三级类目 private String title; //标题 private String SubTitle; //子标题 private Boolean saleable; //是否上架 @JsonIgnore private Boolean valid; //是否有效,逻辑删除用 private Date createTime; //创建时间 @JsonIgnore private Date lastUpdateTime; //最后修改时间 @Transient private String cname; @Transient private String bname; }
-
编辑controller
@RestController
public class GoodsController {
@Autowired
private GoodsService goodsService;
/**
* 分页查询spu
*
* @param page
* @param rows
* @param saleable
* @param key
* @return
*/
@GetMapping("/spu/page")
public ResponseEntity<PageResult<Spu>> querySpuByPage(
@RequestParam(value = "page", defaultValue = "1") Integer page, //页码
@RequestParam(value = "rows", defaultValue = "5") Integer rows, //当前页
@RequestParam(value = "saleable", required = false) Boolean saleable, //是否上架
@RequestParam(value = "key", required = false) String key //搜索
) {
return ResponseEntity.ok(goodsService.querySpuByPage(page,rows,saleable,key));
}
}
1.3.编辑GoodsService中的querySpuByPage()方法
重点关注:解析分类和品牌的名称
@Service
public class GoodsService {
@Autowired
private SpuMapper spuMapper;
@Autowired
private SpuDetailMapper spuDetailMapper;
@Autowired
private CategoryService categoryService;
@Autowired
private BrandService brandService;
public PageResult<Spu> querySpuByPage(Integer page, Integer rows, Boolean saleable, String key) {
//分页
PageHelper.startPage(page,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);
}
//默认排序
example.setOrderByClause("last_Update_Time DESC");
//查询
List<Spu> spus = spuMapper.selectByExample(example);
//判断
if (CollectionUtils.isEmpty(spus)){
throw new LyException(ExceptionEnum.GOODS_NOT_FOND);
}
//解析分类和品牌的名称
loadCategoryAndBrandName(spus);
//解析分页结果
PageInfo<Spu> info = new PageInfo<>(spus);
return new PageResult<>(info.getTotal(),spus);
}
private void loadCategoryAndBrandName(List<Spu> spus) {
for (Spu spu:spus) {
//处理分类名称
List<String> names = categoryService.queryByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()))
.stream().map(Category::getName).collect(Collectors.toList());
spu.setCname(StringUtils.join(names,"/"));
//处理品牌名称
spu.setBname(brandService.queryById(spu.getBrandId()).getName());
}
}
}
1.4.分别编辑categoryService和brandService
-
categoryService中的queryByIds()方法
public List<Category> queryByIds(List<Long> ids){ List<Category> list = categoryMapper.selectByIdList(ids); if (CollectionUtils.isEmpty(list)){ throw new LyException(ExceptionEnum.CATEGORY_NOT_FOND); } return list; }
注意:使用categoryMapper.selectByIdList(ids)需要:
public interface CategoryMapper extends Mapper<Category> ,IdListMapper<Category,Long> {
}
-
brandService中的queryById()方法
public Brand queryById(Long id) { Brand brand = brandMapper.selectByPrimaryKey(id); if (brand == null) { throw new LyException(ExceptionEnum.BRAND_NOT_FOUND); } return brand; }
1.5.测试
访问地址:http://localhost:8081/spu/page
访问地址:http://manage.leyou.com/#/item/list