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 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;
}

}

  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值