模仿mongodb採用xml+json實現小型資料庫

來源:互聯網
上載者:User

mini-mongodb

   模仿mongodb採用Xml+json實現小型資料庫;    1.實現資料庫建立    2.表的建立    3.表資料的增、刪、改、查    供大家參考學習使用,有助於更好的瞭解MongoDB的實現原理!
代碼: 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";/** * 擷取表的全部資料 *  * @param path * @param tablename * @return * @throws Exception */public List loadTableDatas(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();List l = 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;}/** * 表插入資料 *  * @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;}/** * 表修改資料 *  * @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) {// 如果定位Data[通過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----------------------");}/** * 表刪除資料 *  * @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) {// 如果定位Data[通過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----------------------");}/** * 建立資料庫 *  * @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":"男","age":20.0,"name":"lisan","money":2000.98,"_uuid":"a2b64d1a-63ea-4a1b-b1e3-67adcc687c0a"}
  

   * 作者:     張代浩   * 技術論壇:[www.jeecg.org](http://www.jeecg.org)   * 郵箱:  jeecg@sina.com   * 交流群:325978980,143858350

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.