本文最后更新于 2026年5月24日 晚上
更新字典接口的实现
更新字典接口的需求
检查字典是否存在
如果存在则不允许添加
校验字典名称是否重复(排除自己)
校验字典编码是否重复(排除自己)
修改字典信息时自动把原来属于这个字典的所有字典项里的数据也一起更新
代码实现
DictController
1 2 3 4 5 6 7 8 9
| @PutMapping("/{id}") @PreAuthorize("@ps.hasPermission('blog:dict:update')") @SystemLog(businessName = "修改字典接口") @ApiOperation(value = "编辑字典接口", notes = "编辑字典", response = String.class) @ApiImplicitParam(name = "id", value = "字典ID", dataType = "long", paramType = "path", required = true) public ResponseResult updateDict(@PathVariable Long id, @Valid @RequestBody UpdateDictDto updateDictDto) { return sysDictService.updateDict(id, updateDictDto); }
|
创建 UpdateDictDto
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
@Data @AllArgsConstructor @NoArgsConstructor @ApiModel(description = "更新字典请求对象") public class UpdateDictDto {
@NotBlank(message = "字典名称不能为空") @ApiModelProperty(value = "部门名称", required = true, example = "性别") private String name;
@NotBlank(message = "字典名称不能为空") @ApiModelProperty(value = " 字典编码(唯一)", required = true, example = "sex") private String dictCode;
@ApiModelProperty(value = "备注", example = "这是用户的性别") private String remark;
@NotBlank(message = "状态不能为空") @Pattern(regexp = "^[01]$", message = "状态只能为0或1") @ApiModelProperty(value = "状态(0-正常,1-禁用)", required = true, example = "0") private String status; }
|
在 SysDictServiceImpl 中实现功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| @Override @Transactional(rollbackFor = Exception.class) public ResponseResult updateDict(Long id, UpdateDictDto updateDictDto) { SysDict oldDict = sysDictService.getById(id); if (oldDict == null) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "该字典不存在!"); }
LambdaQueryWrapper<SysDict> nameQueryWrapper = new LambdaQueryWrapper<>(); nameQueryWrapper.eq(SysDict::getName, updateDictDto.getName()) .ne(SysDict::getId, id); long nameCount = sysDictService.count(nameQueryWrapper); if (nameCount > 0) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "字典名称已存在,请修改后重试!"); }
LambdaQueryWrapper<SysDict> codeQueryWrapper = new LambdaQueryWrapper<>(); codeQueryWrapper.eq(SysDict::getDictCode, updateDictDto.getDictCode()) .ne(SysDict::getId, id); long codeCount = sysDictService.count(codeQueryWrapper); if (codeCount > 0) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "字典编码已存在,请修改后重试!"); }
SysDict dict = BeanCopyUtils.copyBean(updateDictDto, SysDict.class); dict.setId(id);
boolean result = sysDictService.updateById(dict); if (result) { List<SysDictItem> dictItemList = sysDictItemService.list( new LambdaQueryWrapper<SysDictItem>() .eq(SysDictItem::getDictCode, oldDict.getDictCode()) .select(SysDictItem::getId) );
if (!dictItemList.isEmpty()){ List<Long> dictItemIds = dictItemList.stream().map(SysDictItem::getId).toList(); SysDictItem dictItem = new SysDictItem(); dictItem.setDictCode(dict.getDictCode()); sysDictItemService.update(dictItem, new LambdaQueryWrapper<SysDictItem>() .in(SysDictItem::getId, dictItemIds) ); } }
return ResponseResult.okResult(); }
|
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/