Skip to content

字典组件

字典组件,提供了字典的基础管理功能外,增加了字典的翻译功能。

xml
<!-- 系统配置相关controller接口及web配置 -->
<dependency>
    <groupId>cn.zjtele.pubinfo.sys.config-data</groupId>
    <artifactId>config-data-api</artifactId>
</dependency>

字典ER图

字段转换

转换注解

自定义注解@DictTranslate@DictTranslateField的作用是把字典项中的dict_value转换成dict_label。 @DictTranslate注解如下:

java
/**
 * 系统字典转换拦截注解
 *
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DictTranslate {
}

@DictTranslateField注解具体属性如下:

java
/**
 * 系统字典转换字段注解
 *
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DictTranslateField
{
    /**
     * 字典类型编码
     */
    String dictType();

    /**
     * 字典标签名写入字段,为空则写入原字段
     * @return
     */
    String dictLabelField() default "";
}

使用示例

例如controller层返回查询对象中包含字典数据,如type字段在字典项表中的数据dict_label的值”省“,dict_value的值为”province“;下图是为转化前返回值:

json
{
  "data": [
    {
      "id": "1788494322098855938",
      "type": "province"
    }]
}

使用方法在当前controller上加@DictTranslate注解,如下所示:

java
@Operation(summary = "根据parentId获取区域列表")
@GetMapping("/getListByParentId")
@DictTranslate
public ResponseData<List<PubRegion>> getListByParentId(
      @Parameter(description = "父级id")
      @RequestParam(value = "parentId", required = false, defaultValue = "0") String parentId) {}
{
    // todo
}

在返回类中需要在转换的type字段上加自定义注解@DictTranslateField(dictType = "regionType") ,其中dictType的值为字典项表中的code值,如下所示:

java
public class PubRegion extends Model {
    @DictTranslateField(dictType = "regionType")
    private String type;
}

则返回结果为:

json
{
  "data": [
    {
      "id": "1788494322098855938",
      "type": "省"
    }]
}

字典缓存

字典项实现类PubDictItemServiceImpllistDictItems方法增加了缓存功能,代码所示:

java
@Cacheable(cacheNames = "dict_item_list",key = "#dictCode",unless = "#result.size() == 0")
public List<PubDictItem> listDictItems(String dictCode) {}

其中在字典项删除及修改时,会清除缓存,代码如下:

java
@CacheEvict(cacheNames = "dict_item_list",key = "#result.code",condition = "#result != null")
public PubDictItem deleteDictItem(String id) {}

@CacheEvict(cacheNames = "dict_item_list",key = "#dictItem.code",condition = "#result != null")
public String saveDictItem(DictItemSaveDto dictItem) {}