本文最后更新于 2026年5月30日 下午
添加配置接口的实现
添加配置接口的需求
检查配置键是否已存在
配置键不能重复 这是唯一键值
将配置加入缓存
代码实现
在 ConfigController 中添加接口
1 2 3 4 5 6 7 8
| @PostMapping @PreAuthorize("@ps.hasPermission('sys:config:create')") @SystemLog(businessName = "新增配置") @ApiOperation(value = "新增配置接口", notes = "新增配置", response = String.class) public ResponseResult addConfig(@Valid @RequestBody AddConfigDto addConfigDto) { return adminConfigService.addConfig(addConfigDto); }
|
创建 AddConfigDto
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 AddConfigDto {
@NotBlank(message = "配置名称不能为空") @ApiModelProperty(value = "配置名称", required = true, example = "配置名称") private String configName;
@NotBlank(message = "配置键不能为空") @ApiModelProperty(value = "配置键(唯一)", required = true, example = "site_name") private String configKey;
@NotBlank(message = "配置键不能为空") @ApiModelProperty(value = "配置值", required = true, example = "云梦泽的个人博客") private String configValue;
@ApiModelProperty(value = "备注", example = "给配置添加描述") private String remark; }
|
在 AdminConfigServiceImpl 中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @Override public ResponseResult addConfig(AddConfigDto addConfigDto) { LambdaQueryWrapper<Config> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Config::getConfigKey, addConfigDto.getConfigKey()); long count = adminConfigService.count(queryWrapper);
if (count > 0) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "配置键已存在,请勿重复添加!"); }
Config config = BeanCopyUtils.copyBean(addConfigDto, Config.class); adminConfigService.save(config);
redisTemplate.opsForValue().set(getConfigCacheKey(config.getConfigKey()), config.getConfigValue());
return ResponseResult.okResult(); }
|
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/