java中数据字典的使用:
数据字典:数据库中一个字段下存在多个值的情况(type:1:肉类 2:素菜类 3:服装类):
分析:
1:这种情况下往往需要新建一张表来对应type下面的字段,通常以---表名--字段名---字段下面的值--字段明细--具体描述,等组成。
例如:数据库中
2:数据字典的具体使用:
思路:相当于把上面dic这张表的所有数据查询出来,放入到一个集合中(HashMap中------以键值对的形式存放),再通过key,value的形式把需要的值取出来
具体实现:
通过key取值:
model层:通过typeValue来实现type的具体明细:
代码:

package com.floor.shop.map; import com.floor.shop.dao.IDicDao; import com.floor.shop.model.Dic; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.HashMap; import java.util.List; public class DicMap { @Autowired private static IDicDao dicDao; private static HashMap<String, String> hashMap = new HashMap<>(); //静态方法在程序启动的时候只加载一次,这样为了让查询方法只去数据库查询一次 static { //获取应用上下文对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("mapper/spring-config.xml"); //获取dicDao实例 dicDao = ctx.getBean(IDicDao.class); queryDic(); } //从数据库中取值放入到HashMap中 public static void queryDic(){ List<Dic> dics = dicDao.queryAll(); StringBuilder sb = new StringBuilder(); for(int i=0;i<dics.size();i++){ Dic dic = dics.get(i); String tableName = dic.getTableName(); String fieldName = dic.getFieldName(); String fieldValue = dic.getFieldValue(); String key = tableName+"_"+fieldName+"_"+fieldValue; String value = dic.getFieldDetail(); System.out.println(key+"="+value); hashMap.put(key,value); } } // static{ // hashMap.put("product_type_1","肉类"); // hashMap.put("product_type_2","蔬菜类"); // hashMap.put("product_type_3","服装类"); // hashMap.put("product_type_4","零食"); // hashMap.put("product_type_5","其他"); // } public static String getFieldDetail(String tableName,String fieldName,String fieldValue){ StringBuilder sb = new StringBuilder(); StringBuilder keySb = sb.append(tableName).append("_").append(fieldName).append("_").append(fieldValue); String key = keySb.toString(); String value = hashMap.get(key); return value; } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步