Java操作MongoDB数据库示例分享(2)
MongoServiceImpl.java /************************* 版权声明 ********************************* * * * 版权所有:百洋软件 * * Copyright (c) 2010 by www.po-soft.com * * * **********************
MongoServiceImpl.java
/************************* 版权声明 *********************************
* *
* 版权所有:百洋软件 *
* Copyright (c) 2010 by www.po-soft.com *
* *
************************* 变更记录 *********************************
*
* 创建者:yongtree 创建日期: 2010-7-7
* 备注:
*
* 修改者: 修改日期:
* 备注:
*
*/
package com.posoftframework.mongodb;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.bson.types.ObjectId;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
/**
*
* @author yongtree
* @date 2010-7-7 下午07:22:15
* @version 1.0
*/
public class MongoServiceImpl implements MongoService {
private String dbName;
private String collName;
private DB db;
public MongoServiceImpl(String dbName, String collName) {
this.dbName = dbName;
this.collName = collName;
try {
db = MongoDBConfig.getDBByName(this.dbName);
} catch (Throwable e) {
e.printStackTrace();
}
}
public MongoServiceImpl() {
getDb();
}
public DBCollection getCollection() {
return db.getCollection(this.collName);
}
public DBObject map2Obj(Map<String, Object> map) {
DBObject obj = new BasicDBObject();
if (map.containsKey("class") && map.get("class") instanceof Class)
map.remove("class");
obj.putAll(map);
return obj;
}
public DBObject insert(DBObject obj) {
getCollection().insert(obj);
return obj;
}
public void insertBatch(List<DBObject> list) {
if (list == null || list.isEmpty()) {
return;
}
List<DBObject> listDB = new ArrayList<DBObject>();
for (int i = 0; i < list.size(); i++) {
listDB.add(list.get(i));
}
getCollection().insert(listDB);
}
public void delete(DBObject obj) {
getCollection().remove(obj);
}
public void deleteBatch(List<DBObject> list) {
if (list == null || list.isEmpty()) {
return;
}
for (int i = 0; i < list.size(); i++) {
getCollection().remove(list.get(i));
}
}
public long getCollectionCount() {
return getCollection().getCount();
}
public long getCount(DBObject obj) {
if (obj != null)
return getCollection().getCount(obj);
return getCollectionCount();
}
public List<DBObject> find(DBObject obj) {
DBCursor cur = getCollection().find(obj);
return DBCursor2list(cur);
}
@Override
public List<DBObject> find(DBObject query, DBObject sort) {
DBCursor cur;
if (query != null) {
cur = getCollection().find(query);
} else {
cur = getCollection().find();
}
if (sort != null) {
cur.sort(sort);
}
return DBCursor2list(cur);
}
@Override
public List<DBObject> find(DBObject query, DBObject sort, int start,
int limit) {
DBCursor cur;
if (query != null) {
cur = getCollection().find(query);
} else {
cur = getCollection().find();
}
if (sort != null) {
cur.sort(sort);
}
if (start == 0) {
cur.batchSize(limit);
} else {
cur.skip(start).limit(limit);
}
return DBCursor2list(cur);
}
private List<DBObject> DBCursor2list(DBCursor cur) {
List<DBObject> list = new ArrayList<DBObject>();
if (cur != null) {
list = cur.toArray();
}
return list;
}
public void update(DBObject setFields, DBObject whereFields) {
getCollection().updateMulti(setFields, whereFields);
}
public List<DBObject> findAll() {
DBCursor cur = getCollection().find();
List<DBObject> list = new ArrayList<DBObject>();
if (cur != null) {
list = cur.toArray();
}
return list;
}
public DBObject getById(String id) {
DBObject obj = new BasicDBObject();
obj.put("_id", new ObjectId(id));
DBObject result = getCollection().findOne(obj);
return result;
}
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
this.db = MongoDBConfig.getDBByName(this.dbName);
}
public String getCollName() {
return collName;
}
public void setCollName(String collName) {
this.collName = collName;
}
public DB getDb() {
if (this.db == null) {
if (this.dbName == null) {
this.db = MongoDBConfig.getDB();
} else {
this.db = MongoDBConfig.getDBByName(this.dbName);
}
}
return this.db;
}
public List<String> getAllDBNames() {
return MongoDBConfig.getDBNames();
}
}
精彩图集
精彩文章

