collectionName=voting
DBHelper
package com.yc.votingsys.dao;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.WriteResult;
import com.yc.votingsys.utils.LogUtil;
import com.yc.votingsys.utils.StringUtil;
public class DBHelper {
private static Mongo mongo=null;
private DB db=null;
private DBCollection collection=null;
/**
* 连接到server
*/
static{
try {
mongo= new Mongo(MyPro.getInstance().getProperty("id"),Integer.parseInt( MyPro.getInstance().getProperty("port"))
);
} catch (UnknownHostException e) {
LogUtil.log.error(e.toString());
e.printStackTrace();
} catch (MongoException e) {
LogUtil.log.error(e.toString());
e.printStackTrace();
}
}
/**
* 获取数据库连接
* @param dbName:数据库名
* @param uname:登陆数据库的username
* @param pwd:登录password
* @return:成功返回true
*/
public boolean getDb(String dbName,String uname,String pwd){
db=mongo.getDB(MyPro.getInstance().getProperty("dbName"));
//校验用户password是否正确
if(!StringUtil.isNull(uname) && !StringUtil.isNull(pwd)){ //假设给定了username和password
if (!db.authenticate(uname, pwd.toCharArray())){
LogUtil.log.error("username或password错误,连接MongoDB数据库失败....");
return false;
}else{
LogUtil.log.error("连接MongoDB数据库成功....");
return true;
}
}else{ //假设没有给定,则读取配置文件里配置的username和password
String unames=MyPro.getInstance().getProperty("uname");
String pwds=MyPro.getInstance().getProperty("password");
//校验用户password是否正确
if(!StringUtil.isNull(unames) && !StringUtil.isNull(pwds)){
if (!db.authenticate(unames, pwds.toCharArray())){
LogUtil.log.error("username或password错误,连接MongoDB数据库失败....");
return false;
}else{
LogUtil.log.error("连接MongoDB数据库成功....");
return true;
}
}else{
return true;
}
}
}
/**
* 关闭数据库连接
* @param mongo:server
* @param db:数据库
*/
public void closeAll(Mongo mongo,DB db){
if(db!=null){
db.requestDone();
}
//
if(mongo!=null){
//
mongo.close();
//
}
}
/**
* 获取指定的集合
* @param collectionName:集合名称
* @param dbName:数据库名
* @param uname:登陆数据库的username
* @param pwd:登录password
* @return:返回获取到的集合
*/
public DBCollection getDBCollection(String collectionName,String dbName,String uname,String pwd){
if(getDb(dbName,uname,pwd)){ //连接数据库成功
db.requestStart(); //启动
if(collectionName==null){
collectionName=MyPro.getInstance().getProperty("collectionName");
}
collection=db.getCollection(collectionName); //获取指定的集合
}else{
throw new RuntimeException("数据库连接失败。请检查username和password...");
}
return collection;
}
/**
* 加入对象
* @param map
* @param collection
* @return
*/
public int addObject(Map<String,Object> map,String collectionName){
WriteResult result=null;
try {
if(collectionName==null){
collection=this.getDBCollection(null, null, null, null);
}else{
collection=this.getDBCollection(collectionName, null, null, null);
}
result=collection.save( new BasicDBObject(map) );
} catch (Exception e) {
LogUtil.log.error(e);
e.printStackTrace();
} finally{
this.closeAll(mongo, db);
}
return result.getN();
}
/**
* 加入对象
* @param map
* @param collection
* @return
*/
public int addObjects(Map<String,String> map,String collectionName){
WriteResult result=null;
try {
if(collectionName==null){
collection=this.getDBCollection(null, null, null, null);
}else{
collection=this.getDBCollection(collectionName, null, null, null);
}
result=collection.save( new BasicDBObject(map) );
} catch (Exception e) {
LogUtil.log.error(e);
e.printStackTrace();
} finally{
this.closeAll(mongo, db);
}
return result.getN();
}
/**
* 查询单个结果
* @param params
* @param collectionName
* @return
*/
public Map<String,Object> findOne(Map<String,String> params,String collectionName){
Map<String,Object> map=new HashMap<String,Object>();
try {
if(collectionName==null){
collection=this.getDBCollection(null, null, null, null);
}else{
collection=this.getDBCollection(collectionName, null, null, null);
}
DBObject object;
if(params!=null){
object=collection.findOne( new BasicDBObject(params) );
}else{
object=collection.findOne();
}
Set<String> keys=object.keySet();
for(String key:keys){
map.put(key, object.get(key));
}
}catch (MongoException e) {
LogUtil.log.error(e);
e.printStackTrace();
}finally{
this.closeAll(mongo, db);
}
return map;
}
public Object findOneToObject(Map<String,String> params,String collectionName){
DBObject object = null;
try {
if(collectionName==null){
collection=this.getDBCollection(null, null, null, null);
}else{
collection=this.getDBCollection(collectionName, null, null, null);
}
if(params!=null){
object=collection.findOne( new BasicDBObject(params) );
}else{
object=collection.findOne();
}
}catch (MongoException e) {
LogUtil.log.error(e);
e.printStackTrace();
}finally{
this.closeAll(mongo, db);
}
return object;
}
public List<Object> find(Map<String,Object> params,String collectionName){
List<Object> list=new ArrayList<Object>();
try {
if(collectionName==null){
collection=this.getDBCollection(null, null, null, null);
}else{
collection=this.getDBCollection(collectionName, null, null, null);
}
DBCursor cursor;
if(params!=null){
cursor=collection.find(new BasicDBObject(params) );
}else{
cursor=collection.find();
}
DBObject object;
while(cursor.hasNext()){
object=cursor.next();
list.add(object);
}
}catch (MongoException e) {
LogUtil.log.error(e);
e.printStackTrace();
}finally{
this.closeAll(mongo, db);
}
return list;
}
/**
* 获取总记录条数
* @param params:參数列表
* @return:总记录数
*/
public int getTotal(Map<String,Object> params,String collectionName){
int total=0;
try {
if(collectionName==null){
collection=this.getDBCollection(null, null, null, null);
}else{
collection=this.getDBCollection(collectionName, null, null, null);
}
if(params!=null){
total=(int) collection.count( new BasicDBObject(params) );
}else{
total=(int) collection.count();
}
}catch (MongoException e) {
LogUtil.log.error(e);
e.printStackTrace();
}finally{
this.closeAll(mongo, db);
}
return total;
}
/**
* 更新信息
* @param map:条件
* @param params:改动的值
* @param collectionName
* @return
*/
public int update(Map<String,Object> map,Map<String,Map<String,Object>> params,String collectionName){
WriteResult result = null;
try {
if(collectionName==null){
collection=this.getDBCollection(null, null, null, null);
}else{
collection=this.getDBCollection(collectionName, null, null, null);
}
BasicDBObject object=new BasicDBObject();
if(params!=null){
Set<String> set=params.keySet();
for(String key:set){
object.append(key,params.get(key));
}
}
if(map!=null){
result=collection.update(new BasicDBObject(map),object);
}else{
result=collection.update(null,new BasicDBObject(params));
}
}catch (MongoException e) {
LogUtil.log.error(e);
e.printStackTrace();
}finally{
this.closeAll(mongo, db);
}
return result.getN();
}
}