『博客开发日记-后台』之更新配置接口的实现

本文最后更新于 2026年6月3日 晚上

更新配置接口的实现


更新配置接口的需求

检查配置是否存在

检查配置键是否与其他配置键重复(排除自己)

更新配置信息

从缓存里更新配置


代码实现

在 ConfigController 中添加接口

1
2
3
4
5
6
7
8
9
 @PutMapping("/{id}")
@PreAuthorize("@ps.hasPermission('sys:config:update')")
@SystemLog(businessName = "修改配置")
@ApiOperation(value = "修改配置接口", notes = "修改配置", response = String.class)
@ApiImplicitParam(name = "id", value = "配置ID", dataType = "long", paramType = "path", required = true)
public ResponseResult updateConfig(@PathVariable Long id, @Valid @RequestBody UpdateConfigDto updateConfigDto)
{
return adminConfigService.updateConfig(id, updateConfigDto);
}

创建 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
/**
* 更新配置请求DTO
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "更新配置请求对象")
public class UpdateConfigDto {

@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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 //更新配置
@Override
public ResponseResult updateConfig(Long id, UpdateConfigDto updateConfigDto)
{
//检查配置是否存在
Config oldConfig = adminConfigService.getById(id);
if (oldConfig == null) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "该配置不存在!");
}

//检查配置键是否与其他配置键重复(排除自己)
LambdaQueryWrapper<Config> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Config::getConfigKey, updateConfigDto.getConfigKey())
.ne(Config::getId, id); // 排除当前配置
long count = adminConfigService.count(queryWrapper);

if (count > 0) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "配置键已存在,请勿重复添加!");
}

String oldCacheKey = getConfigCacheKey(oldConfig.getConfigKey());
String newCacheKey = getConfigCacheKey(updateConfigDto.getConfigKey());

//更新配置信息
Config config = BeanCopyUtils.copyBean(updateConfigDto, Config.class);
config.setId(id);
adminConfigService.updateById(config);

//从缓存里更新配置
if (!Objects.equals(oldConfig.getConfigKey(), updateConfigDto.getConfigKey())) {
redisTemplate.delete(oldCacheKey);
}
redisTemplate.opsForValue().set(newCacheKey, updateConfigDto.getConfigValue());

return ResponseResult.okResult();
}




PS:该系列只做为作者学习开发项目做的笔记用

不一定符合读者来学习,仅供参考


预告

后续会记录博客的开发过程

每次学习会做一份笔记来进行发表

“一花一世界,一叶一菩提”


版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/


『博客开发日记-后台』之更新配置接口的实现
http://example.com/2026/05/30/『博客开发日记-后台』之更新配置接口的实现/
作者
云梦泽
发布于
2026年5月30日
更新于
2026年6月3日
许可协议