数据字典设计
数据字典功能
表设计:
列名 | id | type_code | type_name | code | name | has_child | parent_id | disabled |
枚举类设计(项目启动时,将数据字典保存到了枚举类内部)注:数据库的type_code字段就是枚举类的typeCode字段
public enum DictionaryTypeEnum { 薪资范围("薪资范围", "01", false),学历("学历","02", false),部门("部门","03", false); // 成员变量 private String typeName; private String typeCode; private boolean tree; //数据字典 private List<Dictionary> dictionaries = new ArrayList<Dictionary>(); // 构造方法 private DictionaryTypeEnum(String name, String code, boolean tree) { this.typeName = name; this.typeCode = code; this.tree = tree; } //加载数据字典 public void load(List<Dictionary> dictionaries) { //设置只读 this.dictionaries = Collections.unmodifiableList(dictionaries); } public String getTypeName() { return typeName; } public void setTypeName(String typeName) { this.typeName = typeName; } public String getTypeCode() { return typeCode; } public void setTypeCode(String typeCode) { this.typeCode = typeCode; } public boolean isTree() { return tree; } public void setTree(boolean tree) { this.tree = tree; } public List<Dictionary> getDictionaries() { return dictionaries; } }
加载数据字典代码(仅供参考)
package cn.tornado.job.service.dictionary.impl; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import cn.tornado.job.mapper.dictionary.DictionaryMapper; import cn.tornado.job.po.dictionary.Dictionary; import cn.tornado.job.service.dictionary.DictionaryService; import cn.tornado.job.util.DictionaryTypeEnum; @Service public class DictionaryServiceImpl implements DictionaryService{ @Autowired private DictionaryMapper dictionaryMapper; @Override public void initDictionaryEnum() throws Exception { List<Dictionary> dictionarys = dictionaryMapper.listByTypeCode(null); this.toTree(dictionarys); //用枚举存放数据字典 for (DictionaryTypeEnum type : DictionaryTypeEnum.values()) { List<Dictionary> list = new ArrayList<Dictionary>(); for (Dictionary dictionary : dictionarys) { if (type.getTypeCode().equals(dictionary.getTypeCode())){ list.add(dictionary); } } //加载字典 type.load(list); } } /** * <p>Description: 将节点放进父节点 </p> * @param dictionarys * @Author 韩亚华 * @Date 2017-5-16 */ private List<Dictionary> toTree(List<Dictionary> dictionarys) { for (Dictionary dictionary : dictionarys) { if ("-1".equals(dictionary.getParentId())) continue; for (Dictionary parent : dictionarys) { if (dictionary.getParentId().equals(parent.getId())){ if (parent.getChildrens() == null){ List<Dictionary> childrens = new ArrayList<Dictionary>(); parent.setChildrens(childrens); } parent.getChildrens().add(dictionary); break; } } } return dictionarys; } }
使用数据字典util
package cn.tornado.job.util; import java.util.ArrayList; import java.util.List; import cn.tornado.job.po.dictionary.Dictionary; public class DictionaryUtils { /** * <p>Description: 根据数据字典code返回name,如未找到则返回null </p> * @param code 如果code 为空null则返回null * @Author 韩亚华 * @Date 2017-5-16 */ public static String getName(String code){ if (StringUtils.isEmpty(code)){ return null; } for (DictionaryTypeEnum typeEnum : DictionaryTypeEnum.values()) { for (Dictionary dictionary : typeEnum.getDictionaries()) { if (code.equals(dictionary.getCode())){ return dictionary.getName(); } } } return null; } /** * <p>Description: 根据数据字典code和类别返回name,如未找到则返回null </p> * @param code 如果code 为空null则返回null * @Author 韩亚华 * @Date 2017-5-16 */ public static String getName(String code,DictionaryTypeEnum typeEnum){ if (StringUtils.isEmpty(code)){ return null; } for (Dictionary dictionary : typeEnum.getDictionaries()) { if (code.equals(dictionary.getCode())){ return dictionary.getName(); } } return null; } /** * <p>Description: 根据字典类型返回返回类型树,若该类型不是树返回普通的list </p> * @param typeEnum * @Author 韩亚华 * @Date 2017-5-16 */ public static List<Dictionary> getTrees(DictionaryTypeEnum typeEnum){ if (!typeEnum.isTree()){ return typeEnum.getDictionaries(); } List<Dictionary> trees = new ArrayList<Dictionary>(); for (Dictionary dictionary : typeEnum.getDictionaries()) { if ("-1".equals(dictionary.getParentId())){ trees.add(dictionary); } } return trees; } /** * <p>Description: 根据字典code返回字典元素,如果是树返回该元素和子树(未找到则返回null)</p> * @param code 如果为null或""则返回null * @Author 韩亚华 * @Date 2017-5-16 */ public static Dictionary get(String code){ if (StringUtils.isEmpty(code)){ return null; } for (DictionaryTypeEnum typeEnum : DictionaryTypeEnum.values()) { for (Dictionary dictionary : typeEnum.getDictionaries()) { if (code.equals(dictionary.getCode())){ return dictionary; } } } return null; } /** * <p>Description: 根据字典code和字典类型返回字典元素,如果是树返回该元素和子树(未找到则返回null)</p> * @param code 如果为null或""则返回null * @Author 韩亚华 * @Date 2017-5-16 */ public static Dictionary get(String code, DictionaryTypeEnum typeEnum){ if (StringUtils.isEmpty(code)){ return null; } for (Dictionary dictionary : typeEnum.getDictionaries()) { if (code.equals(dictionary.getCode())){ return dictionary; } } return null; } }
欢迎交流,仅供参考。