1. 程式人生 > >ssm redis 數據字典在J2EE中的多種應用與實現

ssm redis 數據字典在J2EE中的多種應用與實現

stat ide ddk ucc gif ndt ida creat img

數據字典在項目中是不可缺少的“基礎設施”,關於數據字典如何設計如何實現,今天抽空講一下吧

先看一下表設計:

技術分享

技術分享

通過自定義標簽來實現頁面的渲染:

public class DataDictValueTag extends SimpleTagSupport {

    private String typeCode;

    private String ddKey;
    
    StringWriter sw = new StringWriter();
    
    public void doTag() throws JspException, IOException {
        
if (StringUtils.isNotEmpty(typeCode) && StringUtils.isNotEmpty(ddKey)) { DataDictService ddService = (DataDictService)SpringContextUtil.getBean(DataDictService.class); String ddValue = ddService.queryDataDictValueByCodeKey(typeCode, ddKey); JspWriter out
= getJspContext().getOut(); out.println(ddValue); } else { getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); } } public String getTypeCode() { return typeCode; } public void setTypeCode(String typeCode) {
this.typeCode = typeCode; } public String getDdKey() { return ddKey; } public void setDdKey(String ddKey) { this.ddKey = ddKey; } }

再看一下service,根據字典碼和數據字典key來獲取具體的值:

需要註意的是數據字典屬於靜態數據,要放到redis中

@Override
    public String queryDataDictValueByCodeKey(String typeCode, String ddKey) {
        String redisKey = "redis_datadict:" + typeCode + ":" + ddKey;
        // 從緩存中獲取數據字典的值,如果沒有該值,則從數據庫中獲取,最後再存入redis中
        String redisDdvalue = jedis.get(redisKey);
        if (StringUtils.isNotEmpty(redisDdvalue)) {
            return redisDdvalue;
        }
        
        DataDictExample dataDictExample = new DataDictExample();
        Criteria dataDictCriteria = dataDictExample.createCriteria();
        dataDictCriteria.andTypeCodeEqualTo(typeCode);
        dataDictCriteria.andDdkeyEqualTo(ddKey);
        dataDictCriteria.andIsShowEqualTo(YesOrNo.YES.value);
        List<DataDict> list = dataDictMapper.selectByExample(dataDictExample);
        if (list != null && list.size() > 0) {
            DataDict dd = (DataDict)list.get(0);
            
            String ddvalue = dd.getDdvalue();
            // 在Redis中設置數據字典的值
            jedis.set(redisKey, ddvalue);
            
            return ddvalue;
        }
        
        return "";
    }

再JSP中的使用:

<label>
                                                <input type="radio" name="sex" class="icheck" value="0" /> <dataDict:dataDictValue typeCode="sex" ddKey="0"/>
                                            </label>
                                            
                                            <label>
                                                <input type="radio" name="sex" class="icheck" value="1" /> <dataDict:dataDictValue typeCode="sex" ddKey="1"/>
                                            </label>
                                             
                                            <label>
                                                <input type="radio" name="sex" class="icheck" value="2" checked="checked"/> <dataDict:dataDictValue typeCode="sex" ddKey="2"/>
                                            </label>

如果是用jqgrid類似這樣的js插件來渲染的話,那麽需要再額外自定義api接口供js或者其他應用調用:

{ name: ‘sex‘, index: ‘sex‘, width: 20, sortable: false,
                    formatter:function(cellvalue, options, rowObject) {
                        
                        var typeCode = "sex";
                        var ddkey = cellvalue;
                        var ddvalue = "";
                        
                        $.ajax({
                            url: $("#hdnContextPath").val() + "/dataDict/queryDataDictValue.action",
                            type: "POST",
                            async: false,
                            data: {"typeCode": typeCode, "ddkey": ddkey},
                            success: function(data) {
                                if(data.status == 200 && data.msg == "OK") {
                                    ddvalue = data.data;
                                } else {
                                    console.log(JSON.stringify(data));
                                }
                            },
                            error: function (response, ajaxOptions, thrownError) {
                                Error.displayError(response, ajaxOptions, thrownError);                
                            }
                        });
                        
                        return ddvalue}  
                },

基本上就是這些用法了,具體我錄制了一些視頻,代碼也上傳到了github
http://www.itzixi.com/course/detail.shtml?courseId=17092078Y3009WX4

https://github.com/leechenxiang/LeeCX

ssm redis 數據字典在J2EE中的多種應用與實現