本文最后更新于 2026年5月24日 下午
添加字典接口的实现
添加字典接口的需求
检查字典名称和字典编码是否已存在
如果存在则不允许添加
代码实现
DictController
1 2 3 4 5 6 7 8
| @PostMapping @PreAuthorize("@ps.hasPermission('blog:dict:create')") @SystemLog(businessName = "新增字典") @ApiOperation(value = "新增字典接口", notes = "新增字典", response = String.class) public ResponseResult addDict(@Valid @RequestBody AddDictDto addDictDto) { return sysDictService.addDict(addDictDto); }
|
创建 AddDictDto
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
|
@Data @AllArgsConstructor @NoArgsConstructor @ApiModel(description = "添加字典请求对象") public class AddDictDto {
@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
| @Override public ResponseResult addDict(AddDictDto addDictDto) { LambdaQueryWrapper<SysDict> nameWrapper = new LambdaQueryWrapper<>(); nameWrapper.eq(SysDict::getName, addDictDto.getName()); long nameCount = sysDictService.count(nameWrapper); if (nameCount > 0) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "字典名称已存在,请勿重复添加!"); }
LambdaQueryWrapper<SysDict> codeWrapper = new LambdaQueryWrapper<>(); codeWrapper.eq(SysDict::getDictCode, addDictDto.getDictCode()); long codeCount = sysDictService.count(codeWrapper); if (codeCount > 0) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "字典编码已存在,请勿重复添加!"); }
SysDict dict = BeanCopyUtils.copyBean(addDictDto, SysDict.class); sysDictService.save(dict);
return ResponseResult.okResult(); }
|
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/