您的位置:首页 > 数据库 > Mongodb

spring mvc mongodb数据库访问类

2016-05-05 11:45 417 查看
spring mvc mongodb数据库访问类

直接上代码吧,好像也没什么好说的

package dao;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.types.ObjectId;

/**
* mongodb
* 数据访问类
* @author zhengjiang
* 2016年5月5日
*/
public class MongodbHelper {
private MongoDatabase _db;

protected MongoDatabase db() {
if (_db == null) {
_db = new MongoClient("TestServer").getDatabase("test");
}
return _db;
}

/**
* 向数据库插入一条数据
* @param collectionName 表名
* @param objectJson     插入的数据(json格式)
* @return 返回插入信息和id
* @author zhengjiang
* @Time 2016年5月5日
*/
public Object Insert(String collectionName, String objectJson){
Document m = Document.parse(objectJson);
db().getCollection(collectionName).insertOne(m);
return new Object() {public String Message = "OK";public String objectId = m.getObjectId("_id").toString();public String message = "添加数据成功";};
}

/**
* 获取表数据
* @param collectionName 表名
* @return 返回collectionName下面的所有数据
* @author zhengjiang
* @Time 2016年5月5日
*/
public FindIterable Find(String collectionName) {
FindIterable iterable = db().getCollection(collectionName).find();
return iterable;
}

/**
* 返回首行数据
* @param collectionName 表名
* @param objectJson 查询条件字符串
* @return 返回collectionName下面符合条件的首行数据
* @author zhengjiang
* @Time 2016年5月5日
*/
public Object Find(String collectionName, String objectJson) {
Document m = Document.parse(objectJson);
return db().getCollection(collectionName).find(m).first().toJson();
}

/**
* 获取数据
* @param collectionName 表名
* @param objectJson 查询条件字符串
* @return 返回collectionName下面符合条件的数据
* @author zhengjiang
* @Time 2016年5月5日
*/
public FindIterable FindByWhere(String collectionName,String objectJson){
Document m = Document.parse(objectJson);
FindIterable iterable = db().getCollection(collectionName).find(m);
return iterable;
}

/**
* 获取数据,分页
* @param collectionName 表名
* @param objectJson     查询条件,相当于sql的where后面的条件
* @param page           当前页码
* @param pageSize       每页的最大条数
* @return 返回collectionName下面的符合条件的数据
* @author zhengjiang
* @Time 2016年5月5日
*/
public FindIterable FindPageByWhere(String collectionName,String objectJson, int page, int pageSize){
Document m = Document.parse(objectJson);
FindIterable iterable = db().getCollection(collectionName).find(m).skip((page - 1) * pageSize).sort(new BasicDBObject()).limit(pageSize);
return iterable;
}

/**
* 更新一条数据
* @param collectionName 表名
* @param objectJson     要更新的数据,json格式
* @param objectId       id
* @return 返回受影响的行数
* @author zhengjiang
* @Time 2016年5月5日
*/
public long UpdateOne(String collectionName, String objectId, String objectJson) {
BasicDBObject dbObj = new BasicDBObject();
dbObj.put("_id", new ObjectId(objectId));
Document m = Document.parse("{\"$set\":" + objectJson + "}");
return db().getCollection(collectionName).updateOne(dbObj, m).getModifiedCount();
}

/**
* 更新数据
* @param collectionName 表名
* @param objectJson     要更新的数据,json格式
* @param objectWhere    条件
* @return 返回受影响的行数
* @author zhengjiang
* @Time 2016年5月5日
*/
public long Update(String collectionName, String objectWhere, String objectJson) {
Document dbObj = Document.parse(objectWhere);
Document m = Document.parse("{\"$set\":" + objectJson + "}");
return db().getCollection(collectionName).updateMany(dbObj,m).getModifiedCount();
}

/**
* 删除一条数据
* @param collectionName 表名
* @param objectId       id
* @return 返回受影响的行数
* @author zhengjiang
* @Time 2016年5月5日
*/
public long DeleteOne(String collectionName, String objectId) {
BasicDBObject dbObj = new BasicDBObject();
dbObj.put("_id", new ObjectId(objectId));
return db().getCollection(collectionName).deleteOne(dbObj).getDeletedCount();
}

/**
* 删除数据
* @param collectionName 表名
* @param objectJson     条件
* @return 返回受影响的行数
* @author zhengjiang
* @Time 2016年5月5日
*/
public long Delete(String collectionName, String objectJson) {
Document m = Document.parse(objectJson);
return db().getCollection(collectionName).deleteMany(m).getDeletedCount();
}

/**
* 删除一数据集(表)
* @param collectionName 表名
* @return 返回结果
* @author zhengjiang
* @Time 2016年5月5日
*/
public int DropCollection(String collectionName) {
db().getCollection(collectionName).drop();
return 1;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: