做網(wǎng)站圖片百度競價排名系統(tǒng)
應(yīng)用開發(fā)中數(shù)據(jù)字典項設(shè)計實現(xiàn)方案
?
在應(yīng)用開發(fā)中,總會遇到許多數(shù)據(jù)字典項,比如對象狀態(tài)、對象類型等等,這些項一般都是固定的若干可選值選項,比如對象狀態(tài)可能有新建、修改、刪除等狀態(tài),這些數(shù)據(jù)字典項一旦定義完畢改動的頻率非常低;在應(yīng)用開發(fā)中,為了處理方便,一般要對這些數(shù)據(jù)字典項值選項進(jìn)行數(shù)字編碼(例如: 0表示新建,1表示修改,2表示刪除等),以方便應(yīng)用程序中使用。而UI顯示對象信息時不能顯示對象狀態(tài)等的編碼,對于編碼值設(shè)計人員知道代表什么意思,但用戶就不明白了,所以需要進(jìn)行編碼轉(zhuǎn)換,從編碼轉(zhuǎn)換為文字描述(名稱),也就是需要把狀態(tài)編碼0轉(zhuǎn)換為“新建”,把1轉(zhuǎn)換為“修改”,把2轉(zhuǎn)換為“刪除”等顯示給用戶,用戶才明白對象當(dāng)前的狀態(tài)是什么。
下面介紹一下常用的實現(xiàn)方法:
實現(xiàn)方案:
一、在java文件中定義數(shù)據(jù)字典項
我們習(xí)慣上把應(yīng)用中遇到的數(shù)據(jù)字典項都定義到一個java文件中,這是最常用的方法,實現(xiàn)起來比較簡單,但維護(hù)起來就非常繁瑣,特別是數(shù)據(jù)字典項比較多的情況下,相應(yīng)的java文件就會比較大,一旦數(shù)據(jù)字典項有更新那么維護(hù)起來就比較費(fèi)時費(fèi)力。
在java文件中定義數(shù)據(jù)字典項通常情況下定義為static,舉例來說,類ReportConstants中定義了以下數(shù)據(jù)字典項:
??? public static final int CODE_USERINF_TECHELEVEL_GJ = 1;
?
??? public static final String CODE_USERINF_TECHELEVEL_GJ_KEY = "高級";
?
??? public static final int CODE_USERINF_TECHELEVEL_ZJ = 2;
?
??? public static final String CODE_USERINF_TECHELEVEL_ZJ_KEY = "中級";
?
??? public static final int CODE_USERINF_TECHELEVEL_CJ = 3;
?
??? public static final String CODE_USERINF_TECHELEVEL_CJ_KEY = "初級";
?
??? public static final int CODE_USERINF_TECHELEVEL_WJ = 4;
?
public static final String CODE_USERINF_TECHELEVEL_WJ_KEY = "無職稱";
那么我們在實現(xiàn)中就可以直接引用相應(yīng)的數(shù)據(jù)字典項編碼及名稱,另外,一般情況下需要定義數(shù)據(jù)字典項編碼和名稱的轉(zhuǎn)換方法,比如:
??? public static String getCodeName(int lCode)
?????? {
????????????? //初始化返回值
????????????? String strReturn = "未知";
??????? switch (lCode)
????????????? {
???????????????????? case CODE_USERINF_TECHELEVEL_GJ :
??????????????????????????? strReturn = CODE_USERINF_TECHELEVEL_GJ_KEY;
??????????????????????????? break;
???????????????????? case CODE_USERINF_TECHELEVEL_ZJ :
??????????????????????????? strReturn = CODE_USERINF_TECHELEVEL_ZJ_KEY;
??????????????????????????? break;
???????????????????? case? CODE_USERINF_TECHELEVEL_CJ :
??????????????????????????? strReturn = CODE_USERINF_TECHELEVEL_CJ_KEY;
??????????????????????????? break;
???????????????????? case? CODE_USERINF_TECHELEVEL_WJ :
??????????????????????????? strReturn = CODE_USERINF_TECHELEVEL_WJ_KEY;
??????????????????????????? break;
????????????? }
????????????? return strReturn;
?????? }
這個方法實現(xiàn)了通過數(shù)據(jù)字典項編碼獲得數(shù)據(jù)字典項名稱的功能。那么還需要實現(xiàn)一個對應(yīng)的方法,getCodeByName(String name),即通過數(shù)據(jù)字典項名稱獲取數(shù)據(jù)字典項編碼功能(代碼這里省略,請讀者自己完成)。這樣就可以實現(xiàn)數(shù)據(jù)字典項編碼和名稱的相互轉(zhuǎn)換。
但是一旦出現(xiàn)數(shù)據(jù)字典項名稱或編碼需要更改(“無職稱”項編碼需要由“4”改為“0”),或增加減少數(shù)據(jù)字典項,都需要更新java文件代碼。是否有簡便的方法在滿足上述需求的情況下又不更新java文件代碼?答案是肯定的。下面我們來介紹兩種實現(xiàn)方法:一中使用xml文件,一種在數(shù)據(jù)庫定義。
二、在xml文件中定義
第一種方案是應(yīng)用xml配置文件來定義數(shù)據(jù)字典項。使用xml配置文件,以便最大限度的減小維護(hù)的工作量,避免java代碼的頻繁修改。
下面我們分步驟詳細(xì)介紹一下使用xml配置文件的實現(xiàn)方案
?
第一步:定義xml數(shù)據(jù)字典項配置文件
首先新建一個xml文件,命名為DataDictionaryConfig.xml(名字可以自己定義),把應(yīng)用的用到的數(shù)據(jù)字典項分組定義到xml文件中,舉例如下,我們定義了下列數(shù)據(jù)字典項:
<?xml version="1.0" encoding="GB2312"?>
<data-dictionaries>
??? <data-dictionary>
????? <group value = "0" name="ObjectStatus">
???????? <option value="0" name="detached"/>
???????? <option value="1" name="new"/>
???????? <option value="2" name="updated"/>
???????? <option value="3" name="deleted"/>
????? </group>
????? <group value = "1" name="ObjectTypes">
???????? <option value="0" name="對象類型選項0"/>
???????? <option value="1" name="對象類型選項1"/>
???????? <option value="2" name="對象類型選項2"/>
???????? <option value="3" name="對象類型選項3"/>
<option value="4" name="對象類型選項4"/>
????? </group>
??? </data-dictionary>
</data-dictionaries>
這個xml文件可以根據(jù)需要進(jìn)行擴(kuò)展,滿足更復(fù)雜應(yīng)用的需要。
第二步,定義數(shù)據(jù)字典項對象類和數(shù)據(jù)字典項分組對象類:
??? 對于數(shù)據(jù)字典項這里我們定義了一個數(shù)據(jù)字典項對象類,一組數(shù)據(jù)字典選項集我們定義了一個數(shù)據(jù)字典項分組對象類,如下:
(1)、數(shù)據(jù)字典項類:
public class DataDictionaryItem
{
? public DataDictionaryItem()
? {
? }
?
? private String code;
? private String name;
?
? public void setCode(String code)
? {
??? this.code = code;
? }
?
? public String getCode()
? {
??? return this.code;
? }
?
? public void setName(String name)
? {
??? this.name = name;
? }
?
? public String getName()
? {
??? return this.name;
? }
?
}
?
(2)、數(shù)據(jù)字典項分組類
public class DataDictionaryItems
{
? public DataDictionaryItems()
? {
? }
? //數(shù)據(jù)字典項分組編碼
? private String groupCode;
? //數(shù)據(jù)字典項分組名稱
? private String groupName;
? //數(shù)據(jù)字典項詳細(xì)
? private java.util.ArrayList items;
?
? public void setGroupCode(String code)
? {
??? this.groupCode = code;
? }
? public String getGroupCoude()
? {
??? return this.groupCode;
? }
?
? public void setGroupName(String name)
? {
??? this.groupName = name;
? }
? public String getGroupName()
? {
??? return this.groupName;
? }
?
??//設(shè)置數(shù)據(jù)字典項
? public void setDataDictionaryItem(DataDictionaryItem item)
? {
??? if(this.items == null)
????? this.items = new java.util.ArrayList();
??? this.items.add(item);
? }
?
? //設(shè)置數(shù)據(jù)字典項
? public void setDataDictionaryItem(String itemName, String itemCode)
? {
??? if(this.items == null)
????? this.items = new java.util.ArrayList();
??? DataDictionaryItem item = new DataDictionaryItem();
??? item.setCode(itemCode);
??? item.setName(itemName);
??? this.items.add(item);
? }
?
? //獲得數(shù)據(jù)字典項組對象
? public java.util.ArrayList getDataDictioanryItems()
? {
??? return this.items;
? }
?
第三步,定義Xml數(shù)據(jù)字典項配置文件解析類,這里我們使用Dom4J,相應(yīng)的jar可以在http://www.dom4j.org/上找到
import org.dom4j.*;
import org.dom4j.io.*;
import java.util.*;
?
public class XMLDDItemParser {
?
? //數(shù)據(jù)字典項結(jié)構(gòu)
? public static DataDictionaryItems dataItems ;
? private static String GROUP_NAME = "name";
? private static String GROUP_CODE = "value";
? private static String ITEM_NAME = "name";
? private static String ITEM_CODE = "value";
?
? public XMLDDItemParser() {
? }
?
? ??/**
?? * 獲得分組數(shù)據(jù)字典項集
?? * @param groupName String
?? * @return DataDictionaryItems
?? */
? public static DataDictionaryItems getDataDictionaryItems(String groupName)
? {
??? if(dataItems == null)
????? dataItems = parseXML(groupName);
??? return dataItems;
? }
?
? /**
?? * 根據(jù)分組名稱解析xml文件,獲得該分組下數(shù)據(jù)字典項集
?? * @param gName String
?? * @return DataDictionaryItems 數(shù)據(jù)字典項分組對象
?? */
? public static DataDictionaryItems parseXML(String gName)
? {
??? try
??? {
????? org.dom4j.io.SAXReader saxReader = new org.dom4j.io.SAXReader();
????? Document document = saxReader.read("DataDictionaryConfig.xml");
?
????? dataItems = new DataDictionaryItems();
?
????? List list = document.selectNodes("//group");
????? Iterator iter = list.iterator();
????? while (iter.hasNext())
????? {
???? ???Node node = (Node) iter.next();
??????? if (node instanceof Element)
??????? {
????????? //document
????????? Element element = (Element) node;
????????? String GroupName = element.attributeValue(GROUP_NAME);
????????? String GroupValue = element.attributeValue(GROUP_CODE);
????????? //設(shè)置分組名稱編碼
????????? dataItems.setGroupName(GroupName);
????????? dataItems.setGroupCode(GroupValue);
????????? //取組內(nèi)數(shù)據(jù)字典項
????????? if (gName.equals(GroupName))
????????? {
??????????? //取數(shù)據(jù)字典項名稱編碼
??????????? Iterator elemIter = element.elementIterator();
??????????? while (elemIter.hasNext())
??????????? {
????????????? Element elem = (Element) elemIter.next();
????????????? dataItems.setDataDictionaryItem(elem.attributeValue(ITEM_NAME), elem.attributeValue(ITEM_CODE));
? ??????????}
????????? }
??????? }
????? }
??? }
??? catch (Exception ex) {
????? ex.printStackTrace();
??? }
??? return dataItems;
? }
第四步,提供數(shù)據(jù)字典項編碼轉(zhuǎn)換方法類:
public class DataDictionaryUtils {
? public DataDictionaryUtils() {
? }
?
? /**
?? * 根據(jù)數(shù)據(jù)項名稱轉(zhuǎn)換為數(shù)據(jù)項編碼
?? * @param groupName String
?? * @param itemName String
?? * @return String? 數(shù)據(jù)項編碼
?? */
? public static String getItemCode(String groupName, String itemName)
? {
??? String code = "-1";
??? DataDictionaryItems dataItems = XMLDDItemParser.getDataDictionaryItems(groupName);
???
??? java.util.ArrayList items = dataItems.getDataDictioanryItems();
??? if(items != null)
??? {
????? DataDictionaryItem item;
????? for(int i = 0; i < items.size(); i++)
????? {
??????? item = (DataDictionaryItem) items.get(i);
??? ????if(item != null)
??????? {
????????? String name = item.getName();
????????? if(name.equals(itemName))
????????? {
??????????? code = item.getCode();
??????????? break;
????????? }
??????? }
????? }
??? }
??? return code;
? }
? /**
?? * 根據(jù)數(shù)據(jù)項編碼轉(zhuǎn)換為數(shù)據(jù)項名稱
?? * @param groupName String
?? * @param itemCode String
?? * @return String
?? */?
? public static String getItemName(String groupName, String itemCode)
? {
??? String name = "未知";
??? DataDictionaryItems dataItems = XMLDDItemParser.getDataDictionaryItems(groupName);
???
??? java.util.ArrayList items = dataItems.getDataDictioanryItems();
??? if (items != null)
??? {
????? DataDictionaryItem item;
????? for (int i = 0; i < items.size(); i++)
????? {
??????? item = (DataDictionaryItem) items.get(i);
???? ???if (item != null)
??????? {
????????? String code = item.getCode();
????????? if (code.equals(itemCode))
????????? {
??????????? name = item.getName();
??????????? break;
????????? }
??????? }
????? }
??? }
??? return name;
? }
?
至此,我們已經(jīng)完成了該方案的設(shè)計。使用xml文件,增加刪除數(shù)據(jù)字典項等只需要更新xml文件即可,不涉及java文件的更新。
Xml可以根據(jù)應(yīng)用的具體需要進(jìn)行擴(kuò)展設(shè)計。這里僅僅拋磚引玉,提供一種思路。
三、使用數(shù)據(jù)庫表
上一種方法我們使用xml文件定義數(shù)據(jù)字典項,現(xiàn)在我們把數(shù)據(jù)字典項定義在數(shù)據(jù)庫表中,下面我們來詳細(xì)介紹實現(xiàn)方式:
第一步:定義數(shù)據(jù)字典項數(shù)據(jù)表結(jié)構(gòu)
根據(jù)前面xml文件定義,這里我們定義兩張表,一張是數(shù)據(jù)字典分組信息表,一張是數(shù)據(jù)字典項詳細(xì)信息表。如下:
drop table datadic_groups;
create table datadic_groups(
??? group_code varchar2(20) primary key,
??? group_name varchar2(50)
);
drop table datadic_items;
create table datadic_items(
??? dataitem_code varchar2(20) primary key,
??? dataitem_name varchar2(50),
??? group_code varchar2(20)
);
alter table datadic_items
add constraint dataitem_foreignkey foreign key (group_code)
references datadic_groups(group_code);
?
?
這兩張表可以根據(jù)應(yīng)用的具體需求進(jìn)行擴(kuò)充,這里不再贅述。
第二步:根據(jù)定義的數(shù)據(jù)字典表結(jié)構(gòu)定義數(shù)據(jù)字典實體類。
(請參照二、在xml文件中定義的第二步)
第三步:實現(xiàn)數(shù)據(jù)庫表中數(shù)據(jù)字典項的查詢功能
? /**
?? * 實現(xiàn)從數(shù)據(jù)庫查詢數(shù)據(jù)字典項
?? * @param gName String
?? * @return DataDictionaryItems
?? */
? public static DataDictionaryItems getFromDB(String gName)
? {
???? dataItems = new DataDictionaryItems();
????
???? try
???? {?
??????? //獲取數(shù)據(jù)連接
??????? java.sql.Connection conn = getConnection();
???? ???if(conn != null)
??????? {
?????????? //查詢數(shù)據(jù)庫,根據(jù)組名稱查詢組編號,根據(jù)組編號獲取該組內(nèi)數(shù)據(jù)字典項信息
?????????? String strSql = "select items.dataitem_code, items.dataitem_name, items.group_code, dgroups.group_name from datadic_items items, datadic_groups dgroups where items.group_code = dgroups.group_code and dgroups.group_name='"+gName+"'";
?????????? java.sql.Statement stmt = conn.createStatement();
?????????? java.sql.ResultSet rs = stmt.executeQuery(strSql);
?????????? while(rs.next())
?????????? {
?????????????? String dataitem_code = rs.getString(1);
?????????????? String dataitem_name = rs.getString(2);
?????????????? dataItems.setDataDictionaryItem(dataitem_name, dataitem_code);
?
?????????????? String group_code = rs.getString(3);
?????????????? String group_name = rs.getString(4);
?????????????? dataItems.setGroupCode(group_code);
?????????????? dataItems.setGroupName(group_name);
?????????? }
??????? }
???? }
???? catch(Exception ex)
???? {
?????? ex.printStackTrace();
???? }
???? return dataItems;
? }
?
第四步:提供數(shù)據(jù)字典項編碼轉(zhuǎn)換方法類:
??? (請參照二、在xml文件中定義的第四步)
?
四、進(jìn)一步完善
1、兩種方式都可以提供數(shù)據(jù)字典項維護(hù)界面,直接在維護(hù)界面上操作數(shù)據(jù)字典項,避免由于誤操作導(dǎo)致xml文件或數(shù)據(jù)庫數(shù)據(jù)錯誤。具體的實現(xiàn)也是比較簡單,不再詳細(xì)說明。
2、使用數(shù)據(jù)庫表方式時,如果想減少頻繁查詢數(shù)據(jù)庫,可以將數(shù)據(jù)字典項信息在系統(tǒng)啟動后第一次訪問時加載內(nèi)存中,如果數(shù)據(jù)字典項數(shù)據(jù)量比較大,可實現(xiàn)一自維護(hù)線程,采用最近最少使用算法,將頻繁使用的數(shù)據(jù)字典項駐留內(nèi)存,將長期不用的數(shù)據(jù)字典項從內(nèi)存中刪除,每次自動檢查內(nèi)存中的數(shù)據(jù)字典項,如果存在則從內(nèi)存中讀取,如果不存在則查詢數(shù)據(jù)庫,替換內(nèi)存中最少使用的數(shù)據(jù)字典項。
3、增加運(yùn)行日志記錄,可以使用log4J來記錄運(yùn)行日志
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
create table datadic_groups(
??? group_code varchar2(20) primary key,
??? group_name varchar2(50)
);
drop table datadic_items;
create table datadic_items(
??? dataitem_code varchar2(20) primary key,
??? dataitem_name varchar2(50),
??? group_code varchar2(20)
);
alter table datadic_items
add constraint dataitem_foreignkey foreign key (group_code)
references datadic_groups(group_code);