日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

模仿mongodb采用xml+json实现小型数据库

發布時間:2025/3/16 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 模仿mongodb采用xml+json实现小型数据库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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<Map> 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<Map> l = new ArrayList(); List<Element> list = root.getChildren(); for (Element x : list) { if (x.getAttributeValue("name").equals(tablename)) { List<Element> 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<Element> 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<Element> list = root.getChildren(); for (Element x : list) { if (x.getAttributeValue("name").equals(tablename)) { List<Element> 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<Element> list = root.getChildren(); for (Element x : list) { List<Element> 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-------------------"); } } <?xml version="1.0" encoding="UTF-8"?> <database> <table name="test"> <data>{"sex":"男","age":20.0,"name":"lisan","money":2000.98,"_uuid":"a2b64d1a-63ea-4a1b-b1e3-67adcc687c0a"} </data> </table> <table name="system.indexs" /> <table name="system.users" /> </database>

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

總結

以上是生活随笔為你收集整理的模仿mongodb采用xml+json实现小型数据库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。