第二十一章第二节:规格参数列表查询
查询请求:/product/attr/base/list/{catId}
请求参数:
分类ID: catId
{
page: 1,//当前页码
limit: 10,//每页记录数
sidx: 'id',//排序字段
order: 'asc/desc',//排序方式
key: '上市'//检索关键字
}
响应数据示例:
{
"msg": "success",
"code": 0,
"page": {
"totalCount": 1,
"pageSize": 10,
"totalPage": 1,
"currPage": 1,
"list": [{
"attrId": 3,
"attrName": "上市年份",
"searchType": 1,
"icon": "xx",
"valueSelect": "2018;2019",
"attrType": 1,
"enable": 1,
"catelogId": 225,
"showDesc": 1,
"attrGroupId": 7,
"catelogName": "手机",
"groupName": "主体"
}]
}
}
操作数据库表:
pms_attr(规格参数的基本信息)
pms_attr_attrgroup_relation(通过attr_id查询规格参数和属性分组的关系表,得到属性分组id)
pms_attr_group(通过属性分组id查询属性分组名字)
pms_category(通过分类id查询分类名字)
1:定义规格参数查询的接口
PageUtils queryBaseAttrPage(Map<String, Object> params, Long catId);
2:定义规格参数响应到页面的vo(因为比数据库实体类多了几个字段)
com.applesnt.onlinemall.product.vo.AttrRespVo
package com.applesnt.onlinemall.product.vo;
import lombok.Data;
/*
* 规格参数:响应到页面的数据对象,继承attrvo里面的属性
* */
@Data
public class AttrRespVo extends AttrVo{
/*分类的名字*/
private String catelogName;
/*分组的名字*/
private String groupName;
}
3:定义规格参数查询的接口实现
/*注入关联关系dao*/
@Autowired
AttrAttrgroupRelationDao relationDao;
/*注入属性分组dao*/
@Autowired
AttrGroupDao attrGroupDao;
/*注入分类dao*/
@Autowired
CategoryDao categoryDao;
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catId) {
/*定义一个查询条件对象*/
QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>();
/*获取key查询关键字*/
String key = (String) params.get("key");
/*判断 如果传递的分类id不等于0,就拼接分类id的查询条件*/
if(catId!=0){
wrapper.eq("catelog_id",catId);
}
/*如果key查询关键字不为空,就拼接key的查询条件*/
if(!StringUtils.isEmpty(key)){
wrapper.and((obj)->{
obj.eq("attr_id",key).or().like("attr_name",key);
});
}
/*执行查询 返回AttrEntity集合及分页信息*/
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params), wrapper);
/*获取上一步查询的AttrEntity的列表记录*/
List<AttrEntity> attrEntityList =page.getRecords();
/*流式遍历attrEntityList列表记录,经过数据处理,收集成AttrRespVo集合*/
List<AttrRespVo> attrRespVos = attrEntityList.stream().map((attrEntity)->{
/*定义一个响应数据对象AttrRespVo*/
AttrRespVo respVo = new AttrRespVo();
/*字段值拷贝*/
BeanUtils.copyProperties(attrEntity,respVo);
/*因为需要把属性分组的名字放到AttrRespVo,但pms_attr中并没有保存属性分组的相关信息,所有需要通过attrid查询到pms_attr_attrgroup_relation关联表的属性分组的id*/
AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrEntity.getAttrId()));
if(relationEntity!=null){
/*通过上一步得到的属性分组的id 查询整个属性分组对象*/
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(relationEntity.getAttrGroupId());
/*把属性分组的id和名字放到响应vo中*/
respVo.setAttrGroupId(relationEntity.getAttrGroupId());
respVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
/*通过分类id查询pms_category 得到实体对象*/
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if(categoryEntity!=null){
/*把分类名称放到响应vo中*/
respVo.setCatelogName(categoryEntity.getName());
}
return respVo;
}).collect(Collectors.toList());
PageUtils pageUtils = new PageUtils(page);
/*把查询到的分页对象中的list重新赋值,把AttrEntity集合替换成AttrRespVo集合*/
pageUtils.setList(attrRespVos);
return pageUtils;
}
4:controller调用
com.applesnt.onlinemall.product.controller.AttrController
/**
* 列表分页查询
*/
@RequestMapping("/base/list/{catId}")
public R baseAttrList(@RequestParam Map<String, Object> params,@PathVariable("catId") Long catId){
PageUtils page = attrService.queryBaseAttrPage(params,catId);
return R.ok().put("page", page);
}

浙公网安备 33010602011771号