/**
*根据 ID 获取实体
*@param id
*@return
*/
public Specification findOne(Long id);
/**
*根据 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;
}
@RequestMapping("/findOne")
public Specification findOne(Long id){ return specificationService.findOne(id);
<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" ng-click="findOne(entity.id)">修改</button>
/**
* 修 改
*/
public void update(Specification specification);
/**
* 修 改
*/ @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);
}
}
/**
*修改
*@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, "修改失败");
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |