packagecom.floor.shop.map;importcom.floor.shop.dao.IDicDao;importcom.floor.shop.model.Dic;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importjava.util.HashMap;importjava.util.List;public classDicMap {
@Autowiredprivate staticIDicDao dicDao;private static HashMap hashMap = new HashMap<>();//静态方法在程序启动的时候只加载一次,这样为了让查询方法只去数据库查询一次
static{//获取应用上下文对象
ApplicationContext ctx = new ClassPathXmlApplicationContext("mapper/spring-config.xml");//获取dicDao实例
dicDao = ctx.getBean(IDicDao.class);
queryDic();
}//从数据库中取值放入到HashMap中
public static voidqueryDic(){
List dics =dicDao.queryAll();
StringBuilder sb= newStringBuilder();for(int i=0;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 staticString getFieldDetail(String tableName,String fieldName,String fieldValue){
StringBuilder sb= newStringBuilder();
StringBuilder keySb= sb.append(tableName).append("_").append(fieldName).append("_").append(fieldValue);
String key=keySb.toString();
String value=hashMap.get(key);returnvalue;
}
}