mongodb工具类

Posted 慧强杨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongodb工具类相关的知识,希望对你有一定的参考价值。

mongodb的一个基本工具类,提供的了基本的数据库操作,具体细节请阅读代码

MongoBaseDao.java


----------


import com.google.common.collect.Lists;
import com.mongodb.*;
import com.thinkgem.jeesite.common.config.Global;
import cz.spring.embedmongo.EmbeddedMongoFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2016/5/12.
 */
public class MongoBaseDao

    @Autowired
    private EmbeddedMongoFactoryBean embeddedMongoFactoryBean;

    private static String dbName = Global.setProperties("spider.properties").getProperty("mongo.spider");

    /**
     * 保存表数据
     *
     * @param tableName
     * @param map
     * @return
     */
    public WriteResult saveAndUpdate(String tableName, Map map) 
        DBCollection table = MongoUtils.getTable(dbName, tableName);
        WriteResult save = table.save(new BasicDBObject(map));
        return save;
    

    /**
     * 批量插入
     *
     * @param tableName
     * @param list
     * @return
     */
    public WriteResult insertBatch(String tableName, List<Map> list) 
        DBCollection table = MongoUtils.getTable(dbName, tableName);
        List<DBObject> DBList = Lists.newArrayList();
        for (Map map : list) 
            DBList.add(new BasicDBObject(map));
        
        WriteResult save = table.insert(DBList);
        return save;
    

    /**
     * 根据条件查找
     *
     * @param tableName
     * @param map
     * @return
     */
    public DBCursor find(String tableName, Map map) 
        DBCollection table = MongoUtils.getTable(dbName, tableName);
        DBCursor dbObjects = table.find(new BasicDBObject(map));
        return dbObjects;
    

    /**
     * 查找全部
     *
     * @param tableName
     * @return
     */
    public DBCursor findAll(String tableName) 
        DBCollection table = MongoUtils.getTable(dbName, tableName);
        DBCursor dbObjects = table.find(new BasicDBObject());
        return dbObjects;
    

    public DBObject findOne(String tableName, Map map) 
        DBCollection table = MongoUtils.getTable(dbName, tableName);
        DBObject one = table.findOne(new BasicDBObject(map));
        return one;
    



MongoUtils .java
----------


import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import cz.spring.embedmongo.EmbeddedMongoFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.Set;

/**
 * mongodb 工具类
 * Created by lenovo on 2016/5/12.
 */
public class MongoUtils 

    private static MongoClient mongoClient;
    private static DB db;

    @Autowired
    private static EmbeddedMongoFactoryBean embeddedMongoFactoryBean;

    /**
     * 获取mongodb client
     *
     * @return
     */
    public static MongoClient getMongoClient() 
        try 
            mongoClient = embeddedMongoFactoryBean.getObject();
         catch (IOException e) 
            e.printStackTrace();
        
        return mongoClient;
    


    /**
     * 关闭mongodbClient
     */
    public static void closeDbClient() 
        getMongoClient().close();
    

    /**
     * 获取数据库
     *
     * @param dbName
     * @return
     */
    public static DB getDB(String dbName) 
        db = getMongoClient().getDB(dbName);
        return db;
    

    /**
     * 删除数据库
     *
     * @param dbName
     */
    public static void dropDataBase(String dbName) 
        db = getDB(dbName);
        db.dropDatabase();
    

    /**
     * 创建数据库表
     *
     * @param dbName
     * @param tableName
     * @param options
     * @return
     */
    public static DBCollection createTable(String dbName, String tableName, DBObject options) 
        db = getDB(dbName);
        DBCollection collection = db.createCollection(tableName, options);
        return collection;
    

    /**
     * 获取数据库表
     *
     * @param dbName
     * @param tableName
     * @return
     */
    public static DBCollection getTable(String dbName, String tableName) 
        db = getDB(dbName);
        DBCollection collection = db.getCollection(tableName);
        return collection;
    

    /**
     * 获取数据库下所有的表名
     *
     * @param dbName
     * @param tableName
     * @return
     */
    public static Set getTables(String dbName, String tableName) 
        db = getDB(dbName);
        Set<String> collections = db.getCollectionNames();
        return collections;
    

以上是关于mongodb工具类的主要内容,如果未能解决你的问题,请参考以下文章

mongodb工具类

MongoDB的工具类

Springboot 整合mongodb 操作工具类仿mybatis-plus风格

Springboot 整合mongodb以及mongo数据操作工具类代码实现

java封装Mongodb3.2.1工具类

Spring boot 工具类静态属性注入及多环境配置