本文最后更新于 2026年6月6日 下午
添加配置接口的实现
添加配置接口的需求
检查配置键是否已存在
配置键不能重复 这是唯一键值
将配置(包括配置键,配置值和配置分类)加入缓存
代码实现
在 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 27 28 29 30 31 32 33 34
|
@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;
@ApiModelProperty(value = "排序", example = "1") private Integer sort;
@NotBlank(message = "配置分类不能为空") @Pattern(regexp = "^$|^[01]$", message = "配置分类只能为0或1") @ApiModelProperty(value = "配置分类(0-前台配置,1-后台配置)", example = "0") private String configType; }
|
创建 ConfigCacheVo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
@Data @AllArgsConstructor @NoArgsConstructor public class ConfigCacheVo { @ApiModelProperty(value = "配置键", example = "site.name") private String configKey;
@ApiModelProperty(value = "配置值", example = "云梦泽的个人博客") private String configValue;
@ApiModelProperty(value = "配置分类(0-前台配置,1-后台配置)", example = "0") private String configType; }
|
在 AdminConfigServiceImpl 中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @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()), new ConfigCacheVo(config.getConfigKey(), config.getConfigValue(), config.getConfigType()));
return ResponseResult.okResult(); }
|
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/