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-------------------");
}
}
* 作者: 张代浩
* 技术论坛:[www.jeecg.org](http://www.jeecg.org)
* 邮箱: jeecg@sina.com
* 交流群:325978980,143858350
相关推荐
自己电脑需要安装mongodb数据库 和mongoose ,自己在用的时候,记得要和对应数据库和表名一致
MongoDB权威指南+官方文档MongoDB数据库文档MongoDB数据库文档
这是一个简单的基于mina框架的实现增删改查的工程,融合了mongodb mybatis proxool等多个技术。
JSON+Support+in+PostgreSQL%2C+MySQL%2C+MongoDB%2C+and+SQL+Server
Java 解析xml 存入mongodb 返回org.json json包
SpringBoot+MongoDB+Echarts图表数据可视化 SpringBoot+MongoDB+Echarts图表数据可视化
开发软件: WebStorm 开发环境:Nodejs + vue + express + mongodb数据库 本网站是关于某个课程的在线学习平台,有网站前台,网站后台,服务器端接口和数据库4个部分。教师身份登录网站后端后可以发布管理教学视频,...
远古封神Server(erlang源码)+文档+mongodb数据库 远古封神Server(erlang源码)+文档+mongodb数据库
react+mongodb+node实现大众点评,前端用现在流行的react框架搭建,后端用node实现接口,数据库采用非关系型数据库mongodb。
里面包括了php的mvc结构,还有mongo操作类,还提供了mongo数据库文件mongodb-win32-i386-1.8.1,安装后直接可以使用mvc架构,方便快捷,易扩展
注意:内涵测试表test.sql表结构和数据 请修改application-dev.yml数据源配置项的数据库名称改为自己名字,并把test.sql导入数据库,配置好IP、账号、密码后...redis mongodb mybatis 测试示例都在 test模块!!!!
文档中包含整合的所有pom文件和资源文件(applictionContext.xml、form-jdbc.properties、form-mongodb.properties、form-mongodb.xml、form-mybatis.xml、mybatis-config.xml、spring-mvc.xml、web.xml),由于上传...
Java 操作Mongodb中存储的文件的实例
mongodb 数据库基本操作 1、mongoDB的介绍 mongoDB是一个基于分布式文件存储的数据库,由 C++ 语言编写,旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。它介于关系数据库和非关系数据库之间,被认为是非...
利用mongodb作为数据库,建立数据表以及定义字段,通过express实现接口,同源策略设置对所有IP开放,实现的一个开放式的接口案例
MongoDB权威指南深入MongoDB两本MongoDB新手入门必备书籍高清无码!
MongoDB是一种NoSQL的数据库。三者结合使用,可以构建出高效的Web引用。 《Node应用程序构建——使用MongoDB和Backbone》分为两部分,共10章。第一部分包括第1到4章,概述了Node.js、MongoDB和Backbone.js的核心技术...
mongodb 数据库基本操作 1、mongoDB的介绍 mongoDB是一个基于分布式文件存储的数据库,由 C++ 语言编写,旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。它介于关系数据库和非关系数据库之间,被认为是非...
YNOS 后台管理-系统管理模块 前端 vue+antd 后端 rust 数据库 MongoDB.zip
利用MongoDB与Jackson JSON框架结合实现快速开发应用接口的例子,详情参见文章:http://blog.csdn.net/chaijunkun/article/details/7263804。