电商微服务项目第二天(平台属性管理)

[复制链接]
发表于 2025-12-23 22:57:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

表关系:

1.查询平台属性
  1.     /**
  2.      * 查询平台属性
  3.      * 请求参数三个:一级菜单id,二级菜单id,三级菜单id
  4.      * attrInfoList/2/0/0
  5.      */
  6.     @GetMapping("attrInfoList/{category1_id}/{category2_id}/{category3_id}")
  7.      public Result<List<BaseAttrInfo>> a(@PathVariable("category1_id") Long category1_id,
  8.              @PathVariable("category2_id") Long category2_id,
  9.              @PathVariable("category3_id") Long category3_id){
  10.         //业务层查询,传递三个分类菜单主键,返回值,存储属性信息集合
  11.         List<BaseAttrInfo> baseAttrInfoList = baseAttrInfoService.
  12.                 findBaseAttrInfo(category1_id,category2_id,category3_id);
  13.         return Result.ok(baseAttrInfoList);
  14.     }
  15. }
复制代码
  1.   <resultMap id="findBaseAttrInfoResultMap" type="com.home.mall.entity.BaseAttrInfo">
  2.     <id column="id" property="id"/>
  3.         <result column="attr_name" property="attrName"/>
  4.         <result column="category_id" property="categoryId"/>
  5.         <result column="category_level" property="categoryLevel"/>
  6.         <collection property="attrValueList" ofType="com.home.mall.entity.BaseAttrValue">
  7.             <id column="vid" property="id"/>
  8.             <result column="value_name" property="valueName"/>
  9.             <result column="attr_id" property="attrId"/>
  10.         </collection>
  11.     </resultMap>
  12.     <select id="findBaseAttrInfo" resultMap="findBaseAttrInfoResultMap">
  13.         select bai.id,bai.attr_name,bai.category_id,bai.category_level,
  14.         bav.id vid, bav.value_name, bav.attr_id
  15.         from base_attr_info bai LEFT JOIN base_attr_value bav
  16.         on bai.id = bav.attr_id
  17.         <!--
  18.              拼接查询条件 根据请求参数
  19.               attrInfoList/2/0/0
  20.               @Param("category1_id") Long category1_id,
  21.               @Param("category2_id")Long category2_id,
  22.               @Param("category3_id")Long category3_id)
  23.    
  24.                where bai.category_level = 1 and  bai.category_id = #{category1_id}
  25.                or bai.category_level = 2 and  bai.category_id = #{category2_id}
  26.               -->
  27.         <where>
  28.             <if test="category1_id!=0">
  29.                 bai.category_level = 1 and bai.category_id = #{category1_id}
  30.             </if>
  31.             <if test="category2_id!=0">
  32.                 or (bai.category_level = 2 and bai.category_id = #{category2_id})
  33.             </if>
  34.             <if test="category3_id!=0">
  35.                 or (bai.category_level = 3 and bai.category_id = #{category3_id})
  36.             </if>
  37.         </where>
  38.     </select>
复制代码

2.添加平台属性


接口文档种规定,新增和修改都是用saveAttrInfo接口实现,以是逻辑如下:
传进的baseAttrInfo.getId()若为0,则实行插入,否则实行updata,
updata实行时为了制止主键辩说,必要将平台属性先更新,再将平台属性对应的平台属性值删除后在添加;
请留意在entity中的BaseAttrInfo的List<BaseAttrValue> attrValueList上写上@TableField(exist = false),告诉mp框架查询时不必要查找,此字段用于更新和插入;
  1. /**
  2. *  一对多的集合 ,查询实现手动映射
  3. *  新增数据,数据表BaseAttrInfo 没有attrValueList字段
  4. *  告诉MP框架,这个字段表中可能不存在
  5. */
  6. @TableField(exist = false)
  7. private List<BaseAttrValue> attrValueList;
复制代码
  1. /**
  2.      * 新增和修改平台属性
  3.      * 请求参数:新增的属性和属性值
  4.      * 属性是一个,属性值多个
  5.      * 向两个表中添加数据 必须有事务
  6.      * 请求参数 baseAttrInfo
  7.      * 判断请求参数中的 id = null
  8.      *   执行数据的新增
  9.      * id != null
  10.      *   执行修改的操作
  11.      *
  12.      */
  13.     @PostMapping("saveAttrInfo")
  14.     public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
  15.         baseAttrInfoService.saveAttrInfo(baseAttrInfo);
  16.         return Result.ok();
  17.     }
复制代码
  1. @Override
  2.     @Transactional
  3.     @Operation(summary = "保存属性信息")
  4.     public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
  5.         Long id = baseAttrInfo.getId();
  6.         // 判断是否新增
  7.         if (id==null){
  8.             save(baseAttrInfo);
  9.             baseAttrInfo.getAttrValueList().forEach(baseAttrValue -> {
  10.                 baseAttrValue.setAttrId(baseAttrInfo.getId());
  11.                 baseAttrValueMapper.insert(baseAttrValue);
  12.             });
  13.         }
  14.         // 修改
  15.         else if (id!=null){
  16.             updateById(baseAttrInfo);
  17.             // 删除所有属性值
  18.             QueryWrapper<BaseAttrValue> baseAttrValueQueryWrapper =
  19.                     new QueryWrapper<BaseAttrValue>().eq("attr_id",id);
  20.             baseAttrValueMapper.delete(baseAttrValueQueryWrapper);
  21.             // 添加所有属性值
  22.             baseAttrInfo.getAttrValueList().forEach(baseAttrValue -> {
  23.                 baseAttrValue.setAttrId(baseAttrInfo.getId());
  24.                 baseAttrValueMapper.insert(baseAttrValue);
  25.             });
  26.         }
  27.    }
复制代码
  1.     @Autowired
  2.     private BaseAttrValueMapper baseAttrValueMapper;
  3.     /**
  4.      * 平台属性主键查询
  5.      * 请求参数主键
  6.      */
  7.     @RequestMapping("getAttrValueList/{attrId}")
  8.     public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable("attrId") Long attrId){
  9.         QueryWrapper<BaseAttrValue> baseAttrValueQueryWrapper = new QueryWrapper<BaseAttrValue>().eq("attr_id",attrId);
  10.         List<BaseAttrValue> baseAttrValueList= baseAttrValueMapper.selectList(baseAttrValueQueryWrapper);
  11.         return Result.ok(baseAttrValueList);
  12.     }
复制代码
3.分页查询贩卖属性
  1. @RestController
  2. @RequestMapping("admin/product")
  3. public class SpuInfoController {
  4.     @Autowired
  5.     private SpuInfoService spuInfoService;
  6.     @Operation(summary = "分页查询销售属性")
  7.    @GetMapping("/{page}/{limit}")
  8.     public Result<Page<SpuInfo>> findPageByCategoryId(@PathVariable("page") Integer page,
  9.                                   @PathVariable("limit") Integer limit,
  10.                                   Long category3Id){
  11.        Page<SpuInfo> pageParam =spuInfoService.findPageByCategoryId(page,limit,category3Id);
  12.        return Result.ok(pageParam);
  13.    }
  14. }
复制代码
  1. /**
  2. * @author Bob
  3. * @description 针对表【spu_info(商品表)】的数据库操作Service实现
  4. * @createDate 2024-11-16 10:49:18
  5. */
  6. @Service
  7. public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
  8.     implements SpuInfoService{
  9.     @Override
  10.     @Operation(summary = "分页查询销售属性")
  11.     public Page<SpuInfo> findPageByCategoryId(Integer page, Integer limit, Long category3Id) {
  12.         Page<SpuInfo> page1=new Page<>(page,limit);
  13.         QueryWrapper<SpuInfo> spuInfoQueryWrapper = new QueryWrapper<SpuInfo>()
  14.                 .eq("category3_id",category3Id);
  15.         return this.page(page1,spuInfoQueryWrapper);
  16.     }
  17. }
复制代码

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表