4.3 修改规格 4.3.1 获取规格数据 实现思路:通过规格 ID,到后端查询规格和规格选项列表,然后通过组合实体类返回结果 (1)修改 pinyougou-sellergoods-interface 的 SpecificationService.java
/**
*根据 ID 获取实体
*@param id
*@return
*/
public Specification findOne(Long id); (2)修改 pinyougou-sellergoods-service 的 SpecificationServiceImpl.java
/**
*根据 ID 获取实体
*@param id
*@return
*/ @Override public Specification findOne(Long id){
//查询规格
TbSpecification tbSpecification = specificationMapper.selectByPrimaryKey(id); //查询规格选项列表
TbSpecificationOptionExample example=new TbSpecificationOptionExample(); Criteria criteria = example.createCriteria(); criteria.andSpecIdEqualTo(id);//根据规格 ID 查询
List<TbSpecificationOption> optionList = specificationOptionMapper.selectByExample(example); //构建组合实体类返回结果
Specification spec=new Specification(); spec.setSpecification(tbSpecification); spec.setSpecificationOptionList(optionList); return spec; } (3)修改 pinyougou-manager-web 的 SpecificationController.java
@RequestMapping("/findOne")
public Specification findOne(Long id){ return specificationService.findOne(id); (4)修改页面 specification.html 中列表的修改按钮
<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" ng-click="findOne(entity.id)">修改</button> 4.3.1 保存修改结果 (1)修改 pinyougou-sellergoods-interface 的 SpecificationService.java
/**
- 修 改
*/
public void update(Specification specification); (2)修改 pinyougou-sellergoods-service 的 SpecificationServiceImpl.java
/**
- 修 改
*/ @Override public void update(Specification specification){
//保存修改的规格specificationMapper.updateByPrimaryKey(specification.getSpecification());// 保存规格
//删除原有的规格选项 TbSpecificationOptionExample example=new TbSpecificationOptionExample(); com.pinyougou.pojo.TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
criteria.andSpecIdEqualTo(specification.getSpecification().getId());//指定规格 ID 为条件
specificationOptionMapper.deleteByExample(example);//删除
//循环插入规格选项 for(TbSpecificationOption specificationOption:specification.getSpecificationOptionList()){
specificationOption.setSpecId(specification.getSpecification().getId()); specificationOptionMapper.insert(specificationOption); [font=微软雅黑]}
} (3)修改 pinyougou-manager-web 的 SpecificationController.java
/**
*修改
*@param specification
*@return
*/ @RequestMapping("/update") public Result update(@RequestBody Specification specification){
try {
specificationService.update(specification);
return new Result(true, "修改成功");
} catch (Exception e) { e.printStackTrace(); return new Result(false, "修改失败");
}
} 4.5.1 后端代码 修改 pinyougou-sellergoods-service 的 SpecificationServiceImpl.java