Mini-mongodb
Simulate mongodb using Xml + json to implement small databases; 1. create a database. 2. table creation 3. the addition, deletion, modification, and query of table data are for your reference. This helps you better understand the implementation principles of MongoDB!
Code: http://zhangdaiscott.github.io/mini-mogodb
Package org. jeecgframework; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import java. util. UUID; import org. jdom. document; import org. jdom. element; import org. jdom. JDOMException; import org. jdom. input. SAXBuilder; import org. jdom. output. XMLOutputter; import com. google. gson. gson; public class MiniMogodb {private static final String _ uuid = "_ uuid "; /*** get all table data ** @ param path * @ param tablename * @ return * @ throws Exception */public ListLoadTableDatas (String path, String tablename) throws Exception {Gson gson = new Gson (); FileInputStream file = new FileInputStream (path); SAXBuilder saxBuilder = new SAXBuilder (); document document = saxBuilder. build (file); Element root = document. getRootElement (); ListL = new ArrayList (); List
List = root. getChildren (); for (Element x: list) {if (x. getAttributeValue ("name"). equals (tablename) {List
Al = x. getChildren (); for (Element s: al) {String data = s. getText (); if (data = null) {break;} Map mp = gson. fromJson (data, Map. class); l. add (mp) ;}} return l ;} /*** Insert table data ** @ param path * @ param tablename * @ param po * @ return * @ throws JDOMException * @ throws IOException */public boolean addData (String path, string tablename, Object po) throws JDOMException, IOException {Gson gson = new Gson (); boolean flag = false; FileInputStream file = new FileInputStream (path ); SAXBuilder saxBuilder = new SAXBuilder (); Document document = saxBuilder. build (file); Element root = document. getRootElement (); List
List = root. getChildren (); for (Element x: list) {if (x. getAttributeValue ("name "). equals (tablename) {Map base = new HashMap (); base. put (_ uuid, UUID. randomUUID (). toString (); Element data = new Element ("data"); String json = gson. toJson (po); Map mp = gson. fromJson (json, Map. class); base. putAll (mp); data. addContent (gson. toJson (base); x. addContent (data) ;}} XMLOutputter out = new XMLOutputter (); out. output (document, new FileOutputStream (path); flag = true; System. out. println ("---------- insert --- data --- success ----------------------"); return flag ;} /*** Modify Table Data ** @ param path * @ param tablename * @ param po * @ throws JDOMException * @ throws IOException */public void updateData (String path, String tablename, object po) throws JDOMException, IOException {FileInputStream file = new FileInputStream (path); SAXBuilder saxBuilder = new SAXBuilder (); Document document = saxBuilder. build (file); Element root = document. getRootElement (); Gson gson = new Gson (); List
List = root. getChildren (); for (Element x: list) {if (x. getAttributeValue ("name"). equals (tablename) {List
Al = x. getChildren (); for (Element s: al) {// if Data is located [uniquely identified by UUID] Map mp = gson. fromJson (s. getText (), Map. class); Map newmp = gson. fromJson (gson. toJson (po), Map. class); if (mp. get (_ uuid ). equals (newmp. get (_ uuid) {mp. putAll (newmp); Element data = new Element ("data"); data. setText (gson. toJson (mp); x. removeContent (s); x. addContent (data); break ;}}} XMLOutputter out = new XMLOutputter (); out. output (document, new FileOutputStream (path); System. out. println ("---------- update --- data --- success ----------------------");} /*** table data deletion ** @ param path * @ param tablename * @ param po * @ throws JDOMException * @ throws IOException */public void deleteData (String path, String tablename, object po) throws JDOMException, IOException {FileInputStream file = new FileInputStream (path); Gson gson = new Gson (); SAXBuilder saxBuilder = new SAXBuilder (); Document document = saxBuilder. build (file); Element root = document. getRootElement (); List
List = root. getChildren (); for (Element x: list) {List
Al = x. getChildren (); if (x. getAttributeValue ("name "). equals (tablename) {for (Element s: al) {// if Data is located [uniquely identified by UUID] Map mp = gson. fromJson (s. getText (), Map. class); Map newmp = gson. fromJson (gson. toJson (po), Map. class); if (mp. get (_ uuid ). equals (newmp. get (_ uuid) {x. removeContent (s); break ;}}} XMLOutputter out = new XMLOutputter (); out. output (document, new FileOutputStream (path); System. out. println ("---------- delete --- data --- success ----------------------");}/*** create database *** @ param path * @ throws Exception */public void createDataBase (String path) throws Exception {FileOutputStream file1 = new FileOutputStream (path); Document document = new Document (); Element root = new Element ("database "); element sort1 = new Element ("table"); sort1.setAttribute ("name", "test"); Element sort2 = new Element ("table"); sort2.setAttribute ("name ", "system. indexs "); Element sort3 = new Element (" table "); sort3.setAttribute (" name "," system. users "); root. addContent (sort1); root. addContent (sort2); root. addContent (sort3); document. setRootElement (root); XMLOutputter out = new XMLOutputter (); out. output (document, file1); System. out. println ("---------- create --- database --- success -------------------");}}
{"Sex": "male", "age": 20.0, "name": "lisan", "money": 2000.98, "_ uuid": "a2b64d1a-63ea-4a1b-b1e3-67adcc687c0a "}
* Author: Zhang Dai Hao * Technical Forum: [www.jeecg.org] (http://www.jeecg.org) * mailbox: jeecg@sina.com * chat group: 325978980,14.0350