Welcome reprint, Reprint please indicate the source http://blog.csdn.net/wklken/archive/2011/05/07/6402557.aspx
The data dictionary here is not a data dictionary of the database, but involves some operations like dictionary query in the project coding process.
Demand:
In project development, some variables need to be used in several places, but the values of these variables are within a range, and each value corresponds to the literal amount of a translation.
Ø If using database storage, because the variable value is fixed, unnecessary database access is not necessary;
Ø if it is directly hard-coded into the code, it will cause maintenance difficulties if it needs to be modified.
Ø If you are writing in a constant fashion and referencing it in use, you will find that your constant class constants is extremely difficult to name and use.
Common this type of key value pairs:
1. Provincial Code table, provincial coding corresponding to a description of information
2.XX types, such as operation type, packing type
This type of key-value pair features: stored in the library is a number or letter encoding, but in the display of the use of its translation
Treatment mode:
1. Write the key value to the content in the XML file;
2. Read load into memory when the system starts;
3. Call translation directly when using.
XML Document Structure design: "The DTD is delayed, and some details are blurred."
<?xml version= "1.0" encoding= "UTF-8"?> <dics> <table id= "bussiness" > <field id= "TYPE" > <row > <value><!--[cdata[01]]></value> <desc><! [cdata[Air import]]></desc> </row> <row> <value><! [cdata[02]]></value> <desc><! [cdata[Air export]]></desc> </row> </field> <field id= "AB" > <row> <value><! [cdata[1]]></value> <desc><! [cdata[import]]></desc> </row> <row> <value><! [cdata[2]]></value> <desc><! [cdata[export]]></desc> </row> </field> </table> <table id= "TRADE" > <field id= "TYPE" > <row> <value><! [cdata[11]]></value> <desc><! [cdata[General trade]]></desc> </row> <row> <value><! [cdata[12]]></value> <desc><! [cdata[Barter Trade]]--></desc> </row> </field> </table> </dics>
Then it is stored in memory in the form:
Data
Use Linkedhashmap "You might want to put it in the Drop-down list in sequence."
linkedhashmap<string,linkedhashmap<string,linkedhashmap<string,string> > > sysDic;
It's a little confusing, isn't it, a picture?
Then, read the memory when the system is loaded
Depending on your personal preference, you can use JDK's own, or jdom, personal preferences dom4j
Using dom4j, then selecting nodes based on XPath, less code, and introducing
The following is the function code read-by-layer
/** * Initialization Data dictionary */public static void Initsysdic () {try {saxreader reader = new Saxreader (); Document document = Reader.read (new File ("Src/main/resources/system/sysdic.xml")); Linkedhashmap<string, Linkedhashmap<string, linkedhashmap<string, string>>> sysDic = BuildSysDic ( Document); Sysdicutil.setsysdic (Sysdic); catch (Exception e) {e.printstacktrace ();}} @SuppressWarnings ("unchecked") public static linkedhashmap<string, linkedhashmap<string, linkedhashmap< String, string>>> buildsysdic (document document) {string XPath = "dics/table"; list<node> tables = Document.selectnodes (XPath); return handletables (tables); /** * Process a group table * * @param tables * @return/private static linkedhashmap<string, linkedhashmap<string, Linkedha Shmap<string, string>>> handletables (list<node> tables) {linkedhashmap<string, LinkedHashMap <string, linkedhashmap<string, string>>> tablemap = new linkedhashmap<string,Linkedhashmap<string, Linkedhashmap<string, string>>> (); Node table = null; String id = null; Linkedhashmap<string, linkedhashmap<string, string>> field = null; for (int i = 0, Len = tables.size (); i < Len; i++) {table = Tables.get (i); id = table.selectsinglenode ("@id"). GetText ( ); SYSTEM.OUT.PRINTLN (ID); field = Handlefields (Table.selectnodes ("field")); Tablemap.put (ID, field); return tablemap; /** * Processing a group of field * * @param fields * @return/private static linkedhashmap<string, linkedhashmap<string, STRING&G T;> Handlefields (list<node> fields) {linkedhashmap<string, linkedhashmap<string, String>> Fieldmap = new linkedhashmap<string, linkedhashmap<string, string>> (); Node field = null; String id = null; linkedhashmap<string, string> row = null; for (int i = 0, Len = fields.size (); i < Len; i++) {field = Fields.get (i); id = field.selectsinglenode ("@id"). GetText ( ); SYSTEM.OUT.PRINTLN (ID); row = Handlerows (Field.selectnodes ("Row")); Fieldmap.put (ID, row); return fieldmap; /** * Process A group of row * * @param rows * @return/private static linkedhashmap<string, string> handlerows (list<node> ; rows) {linkedhashmap<string, string> rowmap = new linkedhashmap<string, string> (); Node row = null; String key = null; String value = null; for (int i = 0, Len = rows.size (); i < Len; i++) {row = Rows.get (i); key = Row.selectsinglenode ("value"). GetText (); VA Lue = Row.selectsinglenode ("desc"). GetText (); Rowmap.put (key, value); SYSTEM.OUT.PRINTLN (key + "" + value);} return rowmap; }
Finally, it is to obtain and use
Import Java.util.LinkedHashMap; /** * Data Dictionary tool class, get data dictionary related information * @author Ken */public class Sysdicutil {private static Linkedhashmap<string,linkedhashmap <String,LinkedHashMap<String,String> > > sysdic; Private Sysdicutil () {//TODO auto-generated constructor stub} public static void Setsysdic (Linkedhashmap<string,link edhashmap<string,linkedhashmap<string,string>> > Asysdic) {sysdic = asysdic;}/** * Get a data dictionary all values-description * @pa RAM TableName * @param fieldName * @return/public static linkedhashmap<string,string> Getallfields (String Tablena Me,string fieldName) {if (Sysdic.containskey (tablename)) {linkedhashmap<string,linkedhashmap<string,string > > Subdic = sysdic.get (tablename); if (Subdic.containskey (FieldName)) {return subdic.get (fieldName);} else{return null;} else{return null;} /** * Gets a description of a field value for a data dictionary * @param tablename * @param fieldName * @param value * @return/public static String Getfieldde SC (String tablename,stringFieldname,string value) {if (Sysdic.containskey (tablename)) {if (Sysdic.get (tablename). ContainsKey (FieldName)) { Return Sysdic.get (tablename). Get (FieldName). get (value); }else{return value;} else{return value;} }
Use:
Get a single value corresponding description: Sysdicutil.getfielddesc ("OP", "TYPE", value)
"In fact, the method here can be overloaded with parameters (" a.b "," value ").
Get an entire list: Sysdicutil.getallfields ("OP", "TYPE")