# scaffold 项目之配置管理
# 简介
在一个业务中,难免需要动态修改某个配置,比如可能需要更换新的 地图key , 或者创建用户的时候的默认密码等等。
# 使用
在 [基础设施 -> 配置管理] 菜单,可以查看和管理配置,适合业务上需要动态的管理某个配置。
# 配置的表结构
CREATE TABLE `infra_config` ( | |
`id` int NOT NULL AUTO_INCREMENT COMMENT '参数主键', | |
`group` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '参数分组', | |
`type` tinyint NOT NULL COMMENT '参数类型', | |
`name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '参数名称', | |
`key` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '参数键名', | |
`value` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '参数键值', | |
`sensitive` bit(1) NOT NULL COMMENT '是否敏感', | |
`remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '备注', | |
`creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '创建者', | |
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', | |
`updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '更新者', | |
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', | |
`deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除', | |
PRIMARY KEY (`id`) USING BTREE | |
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='参数配置表'; |
key字段,对应到 Spring Boot 配置文件的配置项,例如说scaffold.captcha.enable、sys.user.init-password等等
# 代码实现
package com.tz.scaffold.module.infra.controller.admin.config; | |
/** | |
* <p> Project: scaffold - ConfigController </p> | |
* | |
* 管理后台 - 参数配置 | |
* @author Tz | |
* @date 2024/01/09 23:45 | |
* @version 1.0.0 | |
* @since 1.0.0 | |
*/ | |
@Tag(name = "管理后台 - 参数配置") | |
@RestController | |
@RequestMapping("/infra/config") | |
@Validated | |
public class ConfigController { | |
@Resource | |
private ConfigService configService; | |
@PostMapping("/create") | |
@Operation(summary = "创建参数配置") | |
@PreAuthorize("@ss.hasPermission('infra:config:create')") | |
public CommonResult<Long> createConfig(@Valid @RequestBody ConfigCreateReqVO reqVO) { | |
return success(configService.createConfig(reqVO)); | |
} | |
@PutMapping("/update") | |
@Operation(summary = "修改参数配置") | |
@PreAuthorize("@ss.hasPermission('infra:config:update')") | |
public CommonResult<Boolean> updateConfig(@Valid @RequestBody ConfigUpdateReqVO reqVO) { | |
configService.updateConfig(reqVO); | |
return success(true); | |
} | |
@DeleteMapping("/delete") | |
@Operation(summary = "删除参数配置") | |
@Parameter(name = "id", description = "编号", required = true, example = "1024") | |
@PreAuthorize("@ss.hasPermission('infra:config:delete')") | |
public CommonResult<Boolean> deleteConfig(@RequestParam("id") Long id) { | |
configService.deleteConfig(id); | |
return success(true); | |
} | |
@GetMapping(value = "/get") | |
@Operation(summary = "获得参数配置") | |
@Parameter(name = "id", description = "编号", required = true, example = "1024") | |
@PreAuthorize("@ss.hasPermission('infra:config:query')") | |
public CommonResult<ConfigRespVO> getConfig(@RequestParam("id") Long id) { | |
return success(ConfigConvert.INSTANCE.convert(configService.getConfig(id))); | |
} | |
@GetMapping(value = "/get-value-by-key") | |
@Operation(summary = "根据参数键名查询参数值", description = "不可见的配置,不允许返回给前端") | |
@Parameter(name = "key", description = "参数键", required = true, example = "yunai.biz.username") | |
public CommonResult<String> getConfigKey(@RequestParam("key") String key) { | |
ConfigDO config = configService.getConfigByKey(key); | |
if (config == null) { | |
return success(null); | |
} | |
if (!config.getVisible()) { | |
throw exception(ErrorCodeConstants.CONFIG_GET_VALUE_ERROR_IF_VISIBLE); | |
} | |
return success(config.getValue()); | |
} | |
@GetMapping("/page") | |
@Operation(summary = "获取参数配置分页") | |
@PreAuthorize("@ss.hasPermission('infra:config:query')") | |
public CommonResult<PageResult<ConfigRespVO>> getConfigPage(@Valid ConfigPageReqVO reqVO) { | |
PageResult<ConfigDO> page = configService.getConfigPage(reqVO); | |
return success(ConfigConvert.INSTANCE.convertPage(page)); | |
} | |
@GetMapping("/export") | |
@Operation(summary = "导出参数配置") | |
@PreAuthorize("@ss.hasPermission('infra:config:export')") | |
@OperateLog(type = EXPORT) | |
public void exportConfig(@Valid ConfigExportReqVO reqVO, | |
HttpServletResponse response) throws IOException { | |
List<ConfigDO> list = configService.getConfigList(reqVO); | |
// 拼接数据 | |
List<ConfigExcelVO> datas = ConfigConvert.INSTANCE.convertList(list); | |
// 输出 | |
ExcelUtils.write(response, "参数配置.xls", "数据", ConfigExcelVO.class, datas); | |
} | |
} |
简要说明:
- 可以查询对应的配置
- 可以修改对应的配置