马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
表关系:
1.查询平台属性
- /**
- * 查询平台属性
- * 请求参数三个:一级菜单id,二级菜单id,三级菜单id
- * attrInfoList/2/0/0
- */
- @GetMapping("attrInfoList/{category1_id}/{category2_id}/{category3_id}")
- public Result<List<BaseAttrInfo>> a(@PathVariable("category1_id") Long category1_id,
- @PathVariable("category2_id") Long category2_id,
- @PathVariable("category3_id") Long category3_id){
- //业务层查询,传递三个分类菜单主键,返回值,存储属性信息集合
- List<BaseAttrInfo> baseAttrInfoList = baseAttrInfoService.
- findBaseAttrInfo(category1_id,category2_id,category3_id);
- return Result.ok(baseAttrInfoList);
- }
- }
复制代码- <resultMap id="findBaseAttrInfoResultMap" type="com.home.mall.entity.BaseAttrInfo">
- <id column="id" property="id"/>
- <result column="attr_name" property="attrName"/>
- <result column="category_id" property="categoryId"/>
- <result column="category_level" property="categoryLevel"/>
- <collection property="attrValueList" ofType="com.home.mall.entity.BaseAttrValue">
- <id column="vid" property="id"/>
- <result column="value_name" property="valueName"/>
- <result column="attr_id" property="attrId"/>
- </collection>
- </resultMap>
- <select id="findBaseAttrInfo" resultMap="findBaseAttrInfoResultMap">
- select bai.id,bai.attr_name,bai.category_id,bai.category_level,
- bav.id vid, bav.value_name, bav.attr_id
- from base_attr_info bai LEFT JOIN base_attr_value bav
- on bai.id = bav.attr_id
- <!--
- 拼接查询条件 根据请求参数
- attrInfoList/2/0/0
- @Param("category1_id") Long category1_id,
- @Param("category2_id")Long category2_id,
- @Param("category3_id")Long category3_id)
-
- where bai.category_level = 1 and bai.category_id = #{category1_id}
- or bai.category_level = 2 and bai.category_id = #{category2_id}
- -->
- <where>
- <if test="category1_id!=0">
- bai.category_level = 1 and bai.category_id = #{category1_id}
- </if>
- <if test="category2_id!=0">
- or (bai.category_level = 2 and bai.category_id = #{category2_id})
- </if>
- <if test="category3_id!=0">
- or (bai.category_level = 3 and bai.category_id = #{category3_id})
- </if>
- </where>
- </select>
复制代码
2.添加平台属性
接口文档种规定,新增和修改都是用saveAttrInfo接口实现,以是逻辑如下:
传进的baseAttrInfo.getId()若为0,则实行插入,否则实行updata,
updata实行时为了制止主键辩说,必要将平台属性先更新,再将平台属性对应的平台属性值删除后在添加;
请留意在entity中的BaseAttrInfo的List<BaseAttrValue> attrValueList上写上@TableField(exist = false),告诉mp框架查询时不必要查找,此字段用于更新和插入;- /**
- * 一对多的集合 ,查询实现手动映射
- * 新增数据,数据表BaseAttrInfo 没有attrValueList字段
- * 告诉MP框架,这个字段表中可能不存在
- */
- @TableField(exist = false)
- private List<BaseAttrValue> attrValueList;
复制代码- /**
- * 新增和修改平台属性
- * 请求参数:新增的属性和属性值
- * 属性是一个,属性值多个
- * 向两个表中添加数据 必须有事务
- * 请求参数 baseAttrInfo
- * 判断请求参数中的 id = null
- * 执行数据的新增
- * id != null
- * 执行修改的操作
- *
- */
- @PostMapping("saveAttrInfo")
- public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
- baseAttrInfoService.saveAttrInfo(baseAttrInfo);
- return Result.ok();
- }
复制代码- @Override
- @Transactional
- @Operation(summary = "保存属性信息")
- public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
- Long id = baseAttrInfo.getId();
- // 判断是否新增
- if (id==null){
- save(baseAttrInfo);
- baseAttrInfo.getAttrValueList().forEach(baseAttrValue -> {
- baseAttrValue.setAttrId(baseAttrInfo.getId());
- baseAttrValueMapper.insert(baseAttrValue);
- });
- }
- // 修改
- else if (id!=null){
- updateById(baseAttrInfo);
- // 删除所有属性值
- QueryWrapper<BaseAttrValue> baseAttrValueQueryWrapper =
- new QueryWrapper<BaseAttrValue>().eq("attr_id",id);
- baseAttrValueMapper.delete(baseAttrValueQueryWrapper);
- // 添加所有属性值
- baseAttrInfo.getAttrValueList().forEach(baseAttrValue -> {
- baseAttrValue.setAttrId(baseAttrInfo.getId());
- baseAttrValueMapper.insert(baseAttrValue);
- });
- }
- }
复制代码- @Autowired
- private BaseAttrValueMapper baseAttrValueMapper;
- /**
- * 平台属性主键查询
- * 请求参数主键
- */
- @RequestMapping("getAttrValueList/{attrId}")
- public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable("attrId") Long attrId){
- QueryWrapper<BaseAttrValue> baseAttrValueQueryWrapper = new QueryWrapper<BaseAttrValue>().eq("attr_id",attrId);
- List<BaseAttrValue> baseAttrValueList= baseAttrValueMapper.selectList(baseAttrValueQueryWrapper);
- return Result.ok(baseAttrValueList);
- }
复制代码 3.分页查询贩卖属性
- @RestController
- @RequestMapping("admin/product")
- public class SpuInfoController {
- @Autowired
- private SpuInfoService spuInfoService;
- @Operation(summary = "分页查询销售属性")
- @GetMapping("/{page}/{limit}")
- public Result<Page<SpuInfo>> findPageByCategoryId(@PathVariable("page") Integer page,
- @PathVariable("limit") Integer limit,
- Long category3Id){
- Page<SpuInfo> pageParam =spuInfoService.findPageByCategoryId(page,limit,category3Id);
- return Result.ok(pageParam);
- }
- }
复制代码- /**
- * @author Bob
- * @description 针对表【spu_info(商品表)】的数据库操作Service实现
- * @createDate 2024-11-16 10:49:18
- */
- @Service
- public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
- implements SpuInfoService{
- @Override
- @Operation(summary = "分页查询销售属性")
- public Page<SpuInfo> findPageByCategoryId(Integer page, Integer limit, Long category3Id) {
- Page<SpuInfo> page1=new Page<>(page,limit);
- QueryWrapper<SpuInfo> spuInfoQueryWrapper = new QueryWrapper<SpuInfo>()
- .eq("category3_id",category3Id);
- return this.page(page1,spuInfoQueryWrapper);
- }
- }
复制代码
|