(1)属性的添加修改
平台属性对象:
package com.atguigu.gmall.model.product;import com.atguigu.gmall.model.base.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;import java.util.List;/*** <p>* BaseAttrInfo* </p>**/
@Data
@ApiModel(description = "平台属性")
@TableName("base_attr_info")
public class BaseAttrInfo extends BaseEntity {private static final long serialVersionUID = 1L;@ApiModelProperty(value = "属性名称")@TableField("attr_name")private String attrName;@ApiModelProperty(value = "分类id")@TableField("category_id")private Long categoryId;@ApiModelProperty(value = "分类层级")@TableField("category_level")private Integer categoryLevel;// 平台属性值集合@TableField(exist = false)private List<BaseAttrValue> attrValueList;}
属性值对象:
package com.atguigu.gmall.model.product;import com.atguigu.gmall.model.base.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;/*** <p>* BaseAttrValue* </p>**/
@Data
@ApiModel(description = "平台属性值")
@TableName("base_attr_value")
public class BaseAttrValue extends BaseEntity {private static final long serialVersionUID = 1L;@ApiModelProperty(value = "属性值名称")@TableField("value_name")private String valueName;@ApiModelProperty(value = "属性id")@TableField("attr_id")private Long attrId;
}
在BaseManageController继续添加接口:
/*** 保存平台属性方法* @param baseAttrInfo* @return*/
@PostMapping("saveAttrInfo")
public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {// 前台数据都被封装到该对象中baseAttrInfomanageService.saveAttrInfo(baseAttrInfo);return Result.ok();
}
继续在ManageService添加service接口
/*** 保存平台属性方法* @param baseAttrInfo*/
void saveAttrInfo(BaseAttrInfo baseAttrInfo);
两个表的Mpper在前面已经创建:
实现类中实现:
为了让平台属性跟平台属性值一块成功或失败,需要加上事务: IO、SQL异常也让他回滚,加上rollbackFor
@Override
@Transactional(rollbackFor = Exception.class)
public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {// 什么情况下 是添加,什么情况下是更新,修改 根据baseAttrInfo 的Id// baseAttrInfoif (baseAttrInfo.getId() != null) {// 修改数据平台属性baseAttrInfoMapper.updateById(baseAttrInfo);} else {// 新增平台属性// baseAttrInfo 插入数据baseAttrInfoMapper.insert(baseAttrInfo);}// baseAttrValue 平台属性值// 修改:通过先删除{baseAttrValue},在新增的方式!// 删除条件:baseAttrValue.attrId = baseAttrInfo.idQueryWrapper queryWrapper = new QueryWrapper<BaseAttrValue>();queryWrapper.eq("attr_id", baseAttrInfo.getId());baseAttrValueMapper.delete(queryWrapper);// 获取页面传递过来的所有平台属性值数据List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();if (attrValueList != null && attrValueList.size() > 0) {// 循环遍历for (BaseAttrValue baseAttrValue : attrValueList) {// 获取平台属性Id 给attrIdbaseAttrValue.setAttrId(baseAttrInfo.getId()); // ?baseAttrValueMapper.insert(baseAttrValue);}}
}
集合判断是否为空:
接受的参数id为空,当往属性表里添加数据后会生成id
属性表添加数据后生成id111
属性表:
属性值表
修改带上id:
数据库进行了修改:
(2)平台属性值回显
在BaseManageController继续添加接口:
@GetMapping("getAttrValueList/{attrId}")
public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable("attrId") Long attrId) {BaseAttrInfo baseAttrInfo = manageService.getAttrInfo(attrId);List<BaseAttrValue> baseAttrValueList = baseAttrInfo.getAttrValueList();return Result.ok(baseAttrValueList);
}
继续在ManageService添加service接口
接口
选中准修改数据 , 根据该attrId 去查找AttrInfo,该对象下 List<BaseAttrValue> !
所以在返回的时候,需要返回BaseAttrInfo
/*** 根据attrId 查询平台属性对象* @param attrId* @return*/
BaseAttrInfo getAttrInfo(Long attrId);
实现类实现:
实现类
@Override
public BaseAttrInfo getAttrInfo(Long attrId) {BaseAttrInfo baseAttrInfo = baseAttrInfoMapper.selectById(attrId);// 查询到最新的平台属性值集合数据放入平台属性中!baseAttrInfo.setAttrValueList(getAttrValueList(attrId));return baseAttrInfo;
}/*** 根据属性id获取属性值* @param attrId* @return*/
private List<BaseAttrValue> getAttrValueList(Long attrId) {// select * from baseAttrValue where attrId = ?QueryWrapper queryWrapper = new QueryWrapper<BaseAttrValue>();queryWrapper.eq("attr_id", attrId);List<BaseAttrValue> baseAttrValueList = baseAttrValueMapper.selectList(queryWrapper);return baseAttrValueList;
}