自定义数据字典工具类

字典注解

类注解

import lombok.NonNull;
import java.lang.annotation.*;

/**
 * 数据字典类注解
 *
 * @author huxiang
 */
@Target({
   ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DictClass {
   

    /**
     * js字典文件名称 ,不填生成公用js 字典文件
     */
    String dictJsonFile() default "dict";

    /**
     * 字典key
     */
    @NonNull
    String dictKey();
}

属性注解

import lombok.NonNull;

import java.lang.annotation.*;

/**
 * 数据字典属性注解
 *
 * @author huxiang
 */
@Target({
   ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DictField {
   

    /**
     * 字典值样式 生成js用
     */
    String cssClass() default "";

    /**
     * 字典值对应描述-中文
     */
    @NonNull
    String dictLabelZH();

    /**
     * 字典值对应描述-英文
     */
    @NonNull
    String dictLabelEN();

}

属性拓展注解

import lombok.NonNull;

import java.lang.annotation.*;

/**
 * 数据字典拓展属性注解---用于标准数据字典外需要额外生成key-value
 *
 * @author huxiang
 */
@Target({
   ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DictFieldEx {
   

    /**
     * 拓展属性名
     */
    @NonNull
    String[] propKey();

    /**
     * 拓展属性值
     */
    @NonNull
    String[] propValue();
}

工具类

import com.paratera.console.dict.annotation.DictClass;
import com.paratera.console.dict.annotation.DictField;
import com.paratera.console.dict.annotation.DictFieldEx;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.StringUtils;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 数据字典工具类
 *
 * @author huxiang
 */
@Slf4j
public class DictUtils {
   

    /**
     * 缓存后台数据字典数据
     */
    private static ConcurrentHashMap<String, String> dictMap = new ConcurrentHashMap<String, String>();

    /**
     * 空格常量
     */
    private static String SPACE1 = "  ";
    private static String SPACE2 = "    ";
    private static String SPACE3 = "      ";

    /**
     * 数据字典常量类路径
     */
    private static String constansFilePath = "classpath:com/paratera/console/dict/constants/*.class";

    /**
     * 数据字典 全限定类名前缀
     */
    private static String constansClassPrefix = "com.paratera.console.dict.constants.";

    /**
     * 生成前端json 字典文件路径
     */
    private static String jsonDirUrl = DictUtils.class.getResource("/").getPath() + "dict/";

    /**
     * 初始化数据字典数据到缓存(可以在引入项目启动时加载)
     *
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static void initDictMapCache() throws IOException, ClassNotFoundException, NoSuchMethodException,
        IllegalAccessException, InvocationTargetException, InstantiationException {
   
        PathMatchingResourcePatternResolver util = new PathMatchingResourcePatternResolver();
        Resource[] files = util.getResources(constansFilePath);
        for (Resource resource : files) {
   
            String className = constansClassPrefix
                + resource.getFilename
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值