数据丢失(动态修改)
三种解决方案(隐藏域,SQL不修改,先到数据库查)
这里为第三种
@ModelAttribute : 在路径访问这个方法的时候会先执行它
解决n-to-n的问题
添加:/employee/save 修改:/employee/update?cmd=update
@ModelAttribute("editEmployee")
public Employee beforeEdit(Long id,String cmd){
//修改的时候才查询(只要有id会就进行一次查询,这是不对的)
if(id!=null && "update".equals(cmd)) {
Employee dbEmp = employeeService.findOne(id);
//把要传过来的关联对象都清空,就可以解决n-to-n的问题
dbEmp.setDepartment(null);
return dbEmp;
}
return null;
}
//这里的ModelAttribute和上面的名称是对应上的
@RequestMapping("/update")
@ResponseBody
public JsonResult update(@ModelAttribute("editEmployee")Employee employee){
return saveOrUpdate(employee);
}