您的位置:首页 > 博客中心 > 互联网 >

数据字典实现缓存

时间:2022-04-14 16:44

数据字典的好处很多比如:

  1、可以减少使用表,来专门记录类型。

  2、类型使用key检索,或者报表统计分析,在一定程度上相比汉字来讲,效率好得多。

  3、使用缓存的数据字典、也可以减少不少的io操作。

  等等、、、、

 

首先,库表设计就智者见智了、不多说、爱怎么设计就怎么设计。

 

完整的数据字典设计 ,需要 

  1、生成select 自定义标签。

  2、list页面,或者get页面, 一个key转 value的标签

使用自定义标签,搭配上缓存的数据字典是最方便、最完美的解决办法,

接下来,就直接贴代码了。

 

一、数据字典缓存配置:

  1、数据字典缓存监听器(在web容器启动成功的时候、进行缓存)

  web.xml

1 
2         初始化数据字典
3         com.hotent.core.web.listener.DictionaryCacheListener
4 

  2、DictionaryCacheListener

gxlsystem.com,gxl网gxlsystem.com,gxl网
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.hotent.platform.model.system.Dictionary;
import com.hotent.platform.model.system.GlobalType;
/**
 *  缓存数据字典  
 * @author miao
 *
 */
public class CacheDict {
    
    /**
     *      所有的数据字典类型  
     */
    public static Map allTypes = new HashMap();
    /**
     *  所有类型,对应的数据字典项
     */
    public static Map> dictList = new HashMap>();
            
    /**
     * 类型与字典项  由List 转成Map(key,value)
     */
    public static Map> dictMap = new HashMap>();
    
}
CacheDict

  4、为缓存注入数据 。    提示一点:在更新、或者添加数据字典的时候,记得重新调用缓存的方法、更新数据

  

gxlsystem.com,gxl网gxlsystem.com,gxl网
  1 import java.io.IOException;
  2 import java.util.List;
  3 
  4 import javax.servlet.jsp.JspTagException;
  5 import javax.servlet.jsp.JspWriter;
  6 import javax.servlet.jsp.tagext.TagSupport;
  7 
  8 import org.apache.commons.lang.StringUtils;
  9 
 10 import com.hotent.core.cache.impl.CacheDict;
 11 import com.hotent.platform.model.system.Dictionary;
 12 
 13 
 14 /**
 15  * 
 16  * 选择下拉框
 17  * 
 18  * @author: miao
 19  * @date: 日期:2013-9-25 23:14:26
 20  * @version 1.0
 21  */
 22 public class DictSelectTag extends TagSupport {
 23     private String id;    //EAMPLE:");
 89         //    if(this.getValue().equals(""));{ 
 90                 sb.append("");    //
 91             for (Dictionary dic : dictionary) {
 92 /*                String v = this.getValue();
 93                 Long i =  dic.getDicId();*/
 94                 if (dic.getDicId().toString().equals(this.getValue())   ) {
 95                     sb.append(" ");
101             }
102             
103             sb.append("");
104             if (hasLabel) {
105                 sb.append("
"); 106 } 107 } 108 }catch (Exception e) { 109 e.printStackTrace(); 110 return sb.append("

异常

此nodekey: "+ this.nodeKey+" 查找数据库数据字典异常
"+e.getMessage()+"
"); 111 } 112 return sb; 113 } 114 115 public String getValue() { 116 return value; 117 } 118 119 public void setValue(String value) { 120 this.value = value; 121 } 122 123 public String getId() { 124 return id; 125 } 126 127 public void setId(String id) { 128 this.id = id; 129 } 130 131 132 public String getDivClass() { 133 return divClass; 134 } 135 136 public void setDivClass(String divClass) { 137 this.divClass = divClass; 138 } 139 140 public String getLabelClass() { 141 return labelClass; 142 } 143 144 public void setLabelClass(String labelClass) { 145 this.labelClass = labelClass; 146 } 147 148 public String getTitle() { 149 return title; 150 } 151 152 public void setTitle(String title) { 153 this.title = title; 154 } 155 156 public String getNodeKey() { 157 return nodeKey; 158 } 159 160 public void setNodeKey(String nodeKey) { 161 this.nodeKey = nodeKey; 162 } 163 164 public boolean isHasLabel() { 165 return hasLabel; 166 } 167 168 public void setHasLabel(boolean hasLabel) { 169 this.hasLabel = hasLabel; 170 } 171 172 public String getName() { 173 return name; 174 } 175 176 public String getClazz() { 177 return clazz; 178 } 179 180 public boolean isRequired() { 181 return required; 182 } 183 184 public void setRequired(boolean required) { 185 this.required = required; 186 } 187 188 public void setClazz(String clazz) { 189 this.clazz = clazz; 190 } 191 192 public void setName(String name) { 193 this.name = name; 194 } 195 196 197 public String getStyle() { 198 return style; 199 } 200 201 public void setStyle(String style) { 202 this.style = style; 203 } 204 205 206 207 208 } DictSelectTag

  6、将数据字典通过key 转换成value

gxlsystem.com,gxl网gxlsystem.com,gxl网
import java.io.IOException;
import java.util.Map;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.hotent.core.cache.impl.CacheDict;


public class DictKey2Value extends TagSupport {
    private Long key;

    private String nodeKey;

    public int doStartTag() throws JspTagException {
        return EVAL_PAGE;
    }

    public int doEndTag() throws JspTagException {
        try {
            JspWriter out = this.pageContext.getOut();
            out.print(dictName().toString());
        } catch (IOException e) {
            e.printStackTrace(); 
        }
        return EVAL_PAGE;
    }
    
    public StringBuffer dictName() {
        StringBuffer sb = new StringBuffer();
        Map map = CacheDict.dictMap.get(this.getNodeKey());
        if(map == null) 
            sb.append("未发现NodeKey");
        else{
            if(map.get(key)!=null)   
              sb.append( map.get(key));
        }
        return sb;
    }
    
    
    public String getNodeKey() {
        return nodeKey;
    }

    public void setNodeKey(String nodeKey) {
        this.nodeKey = nodeKey;
    }

    public Long getKey() {
        return key;
    }

    public void setKey(Long key) {
        this.key = key;
    }


}
DictKey2Value

  7、tag标签配置

gxlsystem.com,gxl网gxlsystem.com,gxl网
 1 
 2     
 3         dict
 4         数据字典
 5         com.hotent.core.web.tag.DictSelectTag
 6         JSP
 7         数据字典选择控件
 8         
 9             id
10             false
11             true
12         
13         
14             required
15             false
16             true
17         
18         
19             name
20             true
21             true
22         
23         
24             title
25             false
26             true
27         
28         
29             value
30             true
31             true
32         
33         
34             nodeKey
35             true
36             true
37              数据字典的节点key
38         
39         
40             divClass
41             false
42             true
43         
44         
45             labelClass
46             false
47             true
48         
49         
50             hasLabel
51             false
52             true
53         
54         
55             clazz
56             false
57             true
58         
59                 
60             style
61             false
62             true
63         
64     
65         
66         dicK2V
67         数据字典
68         com.hotent.core.web.tag.DictK2V
69         JSP
70         数据转换
71         
72             key
73             true
74             true
75         
76         
77             nodeKey
78             true
79             true
80         
81         
tag.tld

 

  看下页面使用效果:

  

  //生成标准的select标签、可以添加自己的样式等、

  

  

  

  

over,

 

数据字典实现缓存,gxlsystem

本类排行

今日推荐

热门手游