zoukankan      html  css  js  c++  java
  • 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;
        }
    }
    View Code
  • 相关阅读:
    修改spring boot 启动logo
    查看jvm常用命令
    intellij IDEA破解
    hdu 新生晚会
    How many prime numbers(素数)
    2077 汉诺塔IV
    Factorial
    双人黑白块
    EasyX
    七夕情人节
  • 原文地址:https://www.cnblogs.com/dw3306/p/9340031.html
Copyright © 2011-2022 走看看