网站运行环境建设方案,在线制作图网站,wordpress 调用头像,私域商城平台配置表
通过配置表#xff0c;灵活的配置。
开发中某些经常变更的参数值#xff0c;加上配置。比如 订单30分钟后失效#xff0c;需求变更#xff0c;要改为15分钟#xff0c;那么直接改配置表就行了#xff0c;不用发版。
某些关键的容易出错的逻辑#xff0c;加上一…配置表
通过配置表灵活的配置。
开发中某些经常变更的参数值加上配置。比如 订单30分钟后失效需求变更要改为15分钟那么直接改配置表就行了不用发版。
某些关键的容易出错的逻辑加上一个开关也就是 config_value 为 0或1为1表示打开为0表示关掉。
不需要的逻辑可以及时用开关关掉。
或者是逻辑复杂开发环境造数据麻烦时也可以用配置表配置开关把前置条件关掉方便验证数据。
建表语句
config_key 唯一索引保证配置的 key 唯一。
config_value如果有多个可以用逗号隔开。
is_delete 表示是否删除0-否1-是。
CREATE TABLE tb_system_config (id int NOT NULL AUTO_INCREMENT COMMENT 主键,config_key varchar(128) NOT NULL COMMENT 配置的KEY,config_value varchar(2000) DEFAULT COMMENT 配置的值。如果有多个用逗号隔开,description varchar(100) DEFAULT COMMENT 描述,create_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT 创建时间,update_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 更新时间,is_delete tinyint(1) NOT NULL DEFAULT 0 COMMENT 是否删除0-否1-是,PRIMARY KEY (id),UNIQUE KEY uk_config_key (config_key)
) ENGINEInnoDB DEFAULT CHARSETutf8mb4 COMMENT系统配置表;查询系统配置表
插入配置数据后查询
找出 config_key 为 config_test 的配置值。
SELECT config_value FROM tb_system_config_test WHERE config_keyconfig_test AND is_delete0;依赖包
采用 mybatisPlus 也可以自己用 mybatis 处理。 propertiesmybatis.plus.version3.4.0/mybatis.plus.version/propertiesdependencies!--mybatis-plus下面这两个依赖必须加--dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion${mybatis.plus.version}/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependency!--mybatis-plus以下依赖是拓展比如分页插件--dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-extension/artifactIdversion${mybatis.plus.version}/version/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-annotation/artifactIdversion${mybatis.plus.version}/version/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-core/artifactIdversion${mybatis.plus.version}/version/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-generator/artifactIdversion${mybatis.plus.version}/version/dependency!--单元测试依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependencies实体类
Data
EqualsAndHashCode(callSuper false)
TableName(tb_system_config)
public class SystemConfigEntity implements Serializable {private static final long serialVersionUID 1L;/*** 主键*/TableId(value id, type IdType.AUTO)private Integer id;/*** 配置的KEY*/private String configKey;/*** 配置的值*/private String configValue;/*** 描述*/private String description;/*** 创建时间*/TableField(fill FieldFill.INSERT)private Date createTime;/*** 更新时间*/TableField(fill FieldFill.UPDATE)private Date updateTime;/*** 是否删除0-否1-是*/TableLogicprivate Boolean isDelete;}
Mapper
public interface SystemConfigMapper extends BaseMapperSystemConfigEntity {}Mapper.xml
namespace 和 type 的路径自行修改。。
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.example.demo.dao.SystemConfigMapper!-- 通用查询映射结果 --resultMap idBaseResultMap typecom.example.demo.domain.SystemConfigEntityid columnid propertyid /result columnconfig_key propertyconfigKey /result columnconfig_value propertyconfigValue /result columndescription propertydescription /result columncreate_time propertycreateTime /result columnupdate_time propertyupdateTime /result columnis_delete propertyisDelete //resultMap!-- 通用查询结果列 --sql idBase_Column_Listid, config_key, config_value, description, create_time, update_time, is_delete/sql/mapper
Service 服务类
如果系统接入了 缓存也可以先从缓存中获取数据。
插入/更新数据后记得删掉缓存保持一致性。
系统配置表的逻辑如下
Service
public class SystemConfigServiceImpl extends ServiceImplSystemConfigMapper, SystemConfigEntity implements SystemConfigService {/*** 根据 key 获取配置的 value* param key* return*/public String getValueByKey(String key) {LambdaQueryWrapperSystemConfigEntity queryWrapper new LambdaQueryWrapper();queryWrapper.eq(SystemConfigEntity::getConfigKey, key);//未删除的数据queryWrapper.eq(SystemConfigEntity::getIsDelete, false);SystemConfigEntity systemConfigEntity getOne(queryWrapper);if (systemConfigEntity null) {return ;}return systemConfigEntity.getConfigValue();}/*** 获取所有的配置。* 需要多次查询时使用不用反复查数据表。** return*/public MapString, String getValueMap() {LambdaQueryWrapperSystemConfigEntity queryWrapper new LambdaQueryWrapper();//未删除的数据queryWrapper.eq(SystemConfigEntity::getIsDelete, false);ListSystemConfigEntity list list(queryWrapper);MapString, String map new HashMap();if (CollectionUtils.isEmpty( list)) {return map;}map list.stream().collect(Collectors.toMap(SystemConfigEntity::getConfigKey, SystemConfigEntity::getConfigValue, (key1, key2) - key2));return map;}}