谷粒商城day55-商品服务-API-平台属性-规格参数列表

本文介绍了一种基于分类ID的规格参数查询方法,通过优化查询逻辑减少重复代码,并实现了带有额外属性的响应对象。此外,还提供了详细的service层实现代码。

1.需求分析

可以看到打开规格参数界面时,与进入界面后点击某分类时,分别有两个几乎同样的请求,不同的只是两个参数不同,

刚进界面的是0点击分类时是该分类的ID,下面就要完成查询功能

2. 查询功能实现

创建AttrResponseVo,查看接口文档可以看到返回vo相比较与原生PO多了俩属性

@Data
public class AttrResponseVo extends AttrEntity {

    private String catelogName;
    private String groupName;
}

service层代码

    @Override
    public PageUtils baseAttrList(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>();

        if(catelogId != 0){
            queryWrapper.eq("catelog_id", catelogId);
        }

        String key = (String) params.get("key");
        if(StringUtils.isNotEmpty(key)){
            queryWrapper.and((obj) ->{
                obj.eq("attr_id",key).or().like("attr_name",key);
            });
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                queryWrapper
        );

        PageUtils pageUtils = new PageUtils(page);
        List<AttrEntity> records = page.getRecords();
        List<AttrResponseVo> attrResponseVos = records.stream().map((attrEntity) -> {

            AttrResponseVo attrResponseVo = new AttrResponseVo();
            BeanUtils.copyProperties(attrEntity, attrResponseVo);
            CategoryEntity categoryEntity = categoryService.getById(attrEntity.getCatelogId());
            if (categoryEntity != null) {
                attrResponseVo.setCatelogName(categoryEntity.getName());
            }

            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationService.
                    getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
            if (attrAttrgroupRelationEntity != null) {
                AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrAttrgroupRelationEntity.getAttrGroupId());
                attrResponseVo.setGroupName(attrGroupEntity.getAttrGroupName());
            }

            return attrResponseVo;
        }).collect(Collectors.toList());

        pageUtils.setList(attrResponseVos);
        return pageUtils;
    }

测试查询效果

 

gulimall_pms 商品 drop table if exists pms_attr; drop table if exists pms_attr_attrgroup_relation; drop table if exists pms_attr_group; drop table if exists pms_brand; drop table if exists pms_category; drop table if exists pms_category_brand_relation; drop table if exists pms_comment_replay; drop table if exists pms_product_attr_value; drop table if exists pms_sku_images; drop table if exists pms_sku_info; drop table if exists pms_sku_sale_attr_value; drop table if exists pms_spu_comment; drop table if exists pms_spu_images; drop table if exists pms_spu_info; drop table if exists pms_spu_info_desc; /*==============================================================*/ /* Table: pms_attr */ /*==============================================================*/ create table pms_attr ( attr_id bigint not null auto_increment comment '属性id', attr_name char(30) comment '属性名', search_type tinyint comment '是否需要检索[0-不需要,1-需要]', icon varchar(255) comment '属性图标', value_select char(255) comment '可选值列表[用逗号分隔]', attr_type tinyint comment '属性类型[0-销售属性,1-基本属性,2-既是销售属性又是基本属性]', enable bigint comment '启用状态[0 - 禁用,1 - 启用]', catelog_id bigint comment '所属分类', show_desc tinyint comment '快速展示【是否展示在介绍上;0-否 1-是】,在sku中仍然可以调整', primary key (attr_id) ); alter table pms_attr comment '商品属性'; /*==============================================================*/ /* Table: pms_attr_attrgroup_relation */ /*==============================================================*/ create table pms_attr_attrgroup_relation ( id bigint not null auto_increment comment 'id', attr_id bigint comment '属性id', attr_group_id bigint comment '属性分组id', attr_sort int comment '属性组内排序', primary key (id) ); alter table pms_attr_attrgroup_relation comment '属性&
谷粒商城平台发布商品时,如果商品介绍图片上传失败,通常可能是由于文件存储服务(如阿里云OSS)的配置问题、网络请求异常或后端服务未正确处理请求所致。根据引用中提到的一些常见问题及解决方案,以下是可能的解决方法: ### 1. 检查OSS配置是否正确 商品图片上传通常依赖于对象存储服务(如阿里云OSS)。如果OSS的配置信息缺失或错误,会导致上传失败。例如,`ossClient`实例创建失败时会抛出类似 `Oss endpoint can't be empty` 的异常[^4]。 **解决方法:** - 确保在配置文件(如`application.yml`或`bootstrap.yml`)中正确配置了OSS的访问密钥、Endpoint、Bucket名称等信息。 - 检查是否在Nacos配置中心中正确配置了OSS相关的参数,并确保服务能够正确拉取这些配置。 ### 2. 确保Nacos服务注册与配置中心正常工作 如果网关或相关服务无法从Nacos获取配置或服务实例,可能导致请求失败。例如,引用中提到的503错误与服务未在Nacos中注册或配置名称不一致有关[^1]。 **解决方法:** - 登录Nacos控制台,确认`renren-fast`等服务已成功注册。 - 确认服务的`Data ID`和`Group`与配置文件中的名称一致。 - 如果服务配置有误,可以尝试重新引入Nacos客户端依赖并重启服务[^1]。 ### 3. 检查Spring Boot与Spring Cloud版本兼容性 版本不兼容也可能导致服务启动失败或某些Bean无法正确注入,从而影响图片上传功能。例如,Spring Boot版本过高可能导致与Spring Cloud组件的兼容性问题[^3]。 **解决方法:** - 确保使用的Spring Boot与Spring Cloud Alibaba版本兼容。 - 参考引用中使用的版本配置,例如将Spring Boot版本调整为`2.2.1.RELEASE`或`2.1.8.RELEASE`,Spring Cloud Alibaba版本为`2.1.0.RELEASE`[^3]。 ### 4. 检查网关依赖配置 在网关服务中,如果引入了不兼容的依赖或缺少必要的配置,可能导致请求被拦截或转发失败。 **解决方法:** - 确保网关服务中没有引入冲突的依赖,如引用中提到的`spring-cloud-starter-netflix-ribbon`可能导致的问题[^2]。 - 可尝试注释掉`gulimall-common`中可能导致冲突的依赖,并引入`spring-cloud-loadbalancer`以替代。 ### 5. 检查前端上传请求与后端接口匹配 商品图片上传功能依赖于前后端接口的正确对接。如果接口路径、参数名称或请求方式不匹配,也可能导致上传失败。 **解决方法:** - 使用浏览器开发者工具(F12)查看上传请求的URL、请求头、请求体是否正确。 - 确认后端Controller方法是否正确接收并处理了上传请求,例如使用了`@RequestParam("file") MultipartFile file`等注解。 ### 示例代码:OSS上传接口示例 ```java @RestController @RequestMapping("/oss") public class OssController { @Autowired private OSS ossClient; @Value("${aliyun.oss.bucket}") private String bucketName; @PostMapping("/upload") public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return ResponseEntity.badRequest().body("File is empty"); } String fileName = UUID.randomUUID() + file.getOriginalFilename(); try { ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(file.getBytes())); String fileUrl = "https://" + bucketName + "." + "oss-cn-beijing.aliyuncs.com" + "/" + fileName; return ResponseEntity.ok(fileUrl); } catch (IOException e) { return ResponseEntity.status(500).body("Upload failed"); } } } ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我才是真的封不觉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值