制作数据字典工具类

制作简单的数据字典工具类,以下代码提供简单思路,请自行优化。

1。设计数据字典表。

 制作数据字典工具类

2。制作工具类:数据字典最常用的两个方法是获取下拉列表所有的选项,和获取字典码对应的值。

/**
 * @作者: LYB
 * @创建日期: 2018/8/6
 * @描述:数据字典工具
 */
public final class DictUtils {

    /**
     * 存储字典
     */
    private static Map<String, List<Map<String, Object>>> dictMaps = new ConcurrentHashMap<>();//可换redis存储

    static {
        DictService dictService = new DictServiceImpl();
        final List<Dict> dicts = dictService.selectAllEnabled();//获取所有已启用的数据字典
        //先对字典进行排序,根据排序字段排序
        Collections.sort(dicts, new Comparator<Dict>() {
            public int compare(Dict dict1, Dict dict2) {
                return dict1.getSort().compareTo(dict2.getSort());
            }
        });
        for (Dict dict : dicts) {
            String code = dict.getCode();
            Long type = dict.getType();
            String content = dict.getContent();
            if (dictMaps.get(code) == null) {//查看字典集合是否已经有该数据,如果没有就创建
                List<Map<String, Object>> dictList = new LinkedList<>();
                Map<String, Object> dictMap = new HashMap<>();
                dictMap.put(dict.getType().toString(), dict.getContent());
                dictList.add(dictMap);
                dictMaps.put(code,dictList);
            }else{//如果有就直接加进去
                List<Map<String, Object>> dictList = dictMaps.get(code);
                Map<String, Object> dictMap = new HashMap<>();
                dictMap.put(dict.getType().toString(), dict.getContent());
                dictList.add(dictMap);
                dictMaps.put(code,dictList);
            }
        }
    }

    //获取下拉列表
    public List<Map<String, Object>> getMapList(String code){
        return dictMaps.get(code);
    }

    //获取字典值
    public static String getValue(String code,String type){
        List<Map<String, Object>> maps = dictMaps.get(code);
        for (Map<String, Object>  map: maps) {
            if(map.get(type) != null){
                return map.get(type).toString();
            }
        }
        return null;
    }
}

3。其它

@[email protected] //此处用的lombok
public class Dict implements Serializable{

    public static final Boolean NOT_ENABLED = false;
    public static final Boolean IS_ENABLED = true;

    private Long dictId;

    private String code;//字典码

    private Long type;//类型

    private String content;//内容

    private Long sort;//排序

    private Boolean isEnabled;//启用状态:0未启用,1启用
}
@Service
public interface DictService {

   public List<Dict> selectAllEnabled();
}

 

@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private DictMapper dictMapper;

    /**
     * 查看所有启用的字典
     */
    public List<Dict> selectAllEnabled(){
        return dictMapper.selectAllEnabled();
    }
}

@Component
public interface DictMapper {

    List<Dict> selectAllEnabled();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.alialiso.MyDemo.order.mapper.DictMapper">

    <resultMap id="BaseResultMap" type="com.alialiso.MyDemo.order.domain.Dict">
        <id column="dict_id" property="dictId"/>
        <result column="code" property="code"/>
        <result column="type" property="type"/>
        <result column="content" property="content"/>
        <result column="sort" property="sort"/>
        <result column="is_enabled" property="isEnabled"/>
    </resultMap>

    <select id="selectAllEnabled" resultMap="BaseResultMap">
        SELECT
           `dict_id`,
           `code`,
           `type`,
           `content`,
           `sort`,
           `is_enabled`
        from
           `bas_dict`
        where
        is_enabled = true
    </select>
</mapper>