sys_dict_type 表用于存储数据字典类型。
CREATE TABLE `sys_dict_type` (
`id` char(19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '标识',
`type_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典类型名称',
`type_code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典类型标识',
`description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典类型描述',
`enable` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '是否启用',
`remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注',
`create_by` char(19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_by` char(19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '修改人',
`update_time` datetime DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
sys_dict_type 对应实体
@Data
@Data
public class SysDictType extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
/** 标识 */
@TableId(value = "id", type = IdType.ASSIGN_UUID)
private String id;
/** 字典类型名称 */
private String typeName;
/** 字典类型标识 */
private String typeCode;
/** 字典类型描述 */
private String description;
/** 状态 */
private String status;
/** 备注 */
private String remark;
}
sys_dict_data 表用于存储字典数据,并与 sys_dict_type 表通过 type_code 关联。
CREATE TABLE `sys_dict_data` (
`data_id` char(19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '标识',
`data_label` char(19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典标签',
`data_value` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字典值',
`type_code` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '所属类型',
`is_default` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '是否默认',
`remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注',
`enable` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '是否启用',
`update_by` char(19) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '修改人',
`update_time` datetime DEFAULT NULL COMMENT '修改时间',
`create_by` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`data_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
sys_dict_data 对应实体
@Data
public class SysDictData extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
/** 标识 */
private String id;
/** 字典标签 */
private String dataLabel;
/** 字典值 */
private String dataValue;
/** 所属类型 */
private String typeCode;
/** 是否默认 */
private String isDefault;
/** 是否启用 */
private String status;
/** 是否启用 */
private String remark;
}
前端查询数据字典时,使用sysDict包装返回,如有修改,可以修改其属性
@Data
public class SysDict implements Serializable {
private static final long serialVersionUID = 1L;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String typeName;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String typeCode;
private String dictLabel;
private String dictValue;
private String dictTag;
private String dictColor;
}
项目启动时,初始化字典容器
@Component
public class DictContainer {
public static final Map<String, List<SysDict>> dictContainer = new HashMap<>();
@Resource
private SysDictTypeMapper sysDictTypeMapper;
@PostConstruct
public void init(){
sysDictTypeMapper.selectList().forEach(sysDict -> {
dictContainer.computeIfAbsent(sysDict.getTypeCode(),key -> new ArrayList<>()).add(sysDict);
});
}
}
前端查询接口
@RestController
@RequestMapping("common/dict")
public class DictController {
@GetMapping("getByType")
public Result getByType(@RequestParam String types) {
List<String> typeList = Arrays.asList(types.split(","));
//拿到字典
Map<String, List<SysDict>> result = DictContainer.dictContainer.entrySet().stream()
.filter(entry -> typeList.contains(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
//填充缺省值
typeList.stream().forEach(type ->{
result.computeIfAbsent(type,key -> new ArrayList<>());
});
return Result.success(result);
}
}
如果不需要填充并不存在的类型,可以把缺省值代码删除