本文最后更新于 2026年6月3日 晚上
获取前台配置接口的实现
获取前台配置接口的需求
先从缓存读取前台配置
代码实现
生成代码
在 ConfigController 中添加接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
@RestController @RequestMapping("/config") @Api(tags = "前台系统配置") public class ConfigController { @Autowired private ConfigService configService;
@GetMapping("/getBlogConfig") @SystemLog(businessName = "获取前台系统配置接口") @ApiOperation(value = "获取前台系统配置接口", notes = "获取博客前台的基本配置") public ResponseResult getBlogConfig() { return configService.getBlogConfig(); } }
|
BlogConfigVo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
@Data @AllArgsConstructor @NoArgsConstructor @ApiModel(description = "前台公共配置") public class BlogConfigVo {
@ApiModelProperty(value = "配置键", example = "site.name") private String configKey;
@ApiModelProperty(value = "配置值", example = "云梦泽") private String configValue; }
|
在 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 37 38 39 40 41 42 43 44 45 46 47
|
@Service("configService") public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements ConfigService { private static final String CONFIG_CACHE_PREFIX = "config:";
@Autowired private RedisTemplate<Object, Object> redisTemplate;
@Override @SuppressWarnings("unchecked") public ResponseResult<List<BlogConfigVo>> getBlogConfig() { Set<Object> keys = redisTemplate.keys(CONFIG_CACHE_PREFIX + "*"); List<BlogConfigVo> blogConfigVos = new ArrayList<>();
if (keys != null && !keys.isEmpty()) { keys.forEach(key -> { Object value = redisTemplate.opsForValue().get(key); if (value != null) { String configKey = String.valueOf(key).substring(CONFIG_CACHE_PREFIX.length()); blogConfigVos.add(new BlogConfigVo(configKey, String.valueOf(value))); } }); }
if (blogConfigVos.isEmpty()) { LambdaQueryWrapper<Config> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.select(Config::getConfigKey, Config::getConfigValue) .orderByAsc(Config::getId);
List<Config> configList = list(queryWrapper); blogConfigVos.addAll(BeanCopyUtils.copyBeanList(configList, BlogConfigVo.class)); }
return ResponseResult.okResult(blogConfigVos); } }
|
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/