天天看點

Mongodb的核心資料操作MongodbBaseDao

public abstract class MongodbBaseDao {

Logger log = Logger.getLogger(this.getClass()); // spring mongodb 內建操作類
                                                // protected MongoTemplate
                                                // mongoTemplate; //
                                                // 連結本地資料庫并建立資料表

public void CreateCollection(String collectionName) {
    try {
        log.info("Collection created successfully");
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
} // 擷取資料表 public DBCollection GetCollection(String collectionName) {
    // DBCollection coll = null; try { coll =
    // mongoTemplate.getCollection(collectionName); return coll; } catch
    // (Exception e) { System.err.println(e.getClass().getName() + ": " +
    // e.getMessage()); } return coll; } // 通過條件查詢實體(集合) public List<T>
    // Listfind(Query query) { return mongoTemplate.find(query,
    // this.getEntityClass()); } // 通過一定的條件查詢一個實體 public T findOne(Query
    // query) { return (T) mongoTemplate.findOne(query,
    // this.getEntityClass()); } // 通過條件查詢更新資料 public void update(Query
    // query, Update update) { mongoTemplate.upsert(query, update,
    // this.getEntityClass()); } // 儲存一個對象到mongodb public T save(T bean) {
    // mongoTemplate.save(bean); return bean; } // 通過ID擷取記錄 public T
    // get(String id) { return (T) mongoTemplate.findById(id,
    // this.getEntityClass()); } // 通過ID擷取記錄,并且指定了集合名 public T get(String
    // id, String collectionName) { return (T) mongoTemplate.findById(id,
    // this.getEntityClass(), collectionName); } // 擷取需要操作的實體類class
    // protected abstract Class getEntityClass(); //
    // spring容器注入mongodbTemplate protected abstract void
    // setMongoTemplate(MongoTemplate mongoTemplate);
           

}