java:mongodb

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
java:mongodb [2018/04/16 08:42] – [Librairies] jcheronjava:mongodb [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.1
Ligne 1: Ligne 1:
 ====== MongoDb ====== ====== MongoDb ======
  
-===== Accès direct ===== +===== Librairies =====
- +
-==== Librairies ====+
  
   * [[https://mvnrepository.com/artifact/com.google.code.gson/gson|gson]]   * [[https://mvnrepository.com/artifact/com.google.code.gson/gson|gson]]
Ligne 9: Ligne 7:
  
  
-==== (dé)Sérialisation ====+====Sérialisation/dé-sérialisation JSON =====
 Gson va permettre de sérialiser et désérialiser pour passer d'objet java à JSON et inversement : Gson va permettre de sérialiser et désérialiser pour passer d'objet java à JSON et inversement :
  
Ligne 65: Ligne 63:
  } catch (Exception e) {  } catch (Exception e) {
  return null;  return null;
 + }
 + }
 +
 +}
 +</sxh>
 +
 +===== Opérations CRUD =====
 +
 +Classe à intégrer, à documenter et à tester (Tests unitaires)
 +
 +<sxh java>
 +public class MyMongo {
 + protected DB db;
 + protected MongoClient mongoClient;
 + private DBCollection collection;
 +
 + public DB getDb() {
 + return db;
 + }
 +
 + public boolean connect(String dbname) throws UnknownHostException {
 + return this.connect(dbname, "127.0.0.1", 27017);
 + }
 +
 + public boolean connect(String dbname, String server, int port) throws UnknownHostException {
 + mongoClient = new MongoClient(server, port);
 + List<String> dbnames = mongoClient.getDatabaseNames();
 + db = mongoClient.getDB(dbname);
 + return dbnames.contains(dbname);
 + }
 +
 + public DBCollection getCollection(String name) {
 + return db.getCollection(name);
 + }
 +
 + public WriteResult insert(String collectionName, Object object) {
 + setCollection(collectionName);
 + return insert(object);
 + }
 +
 + public WriteResult insert(Object object) {
 + return collection.insert(DBOAdapter.objectToDBObject(object));
 + }
 +
 + public DBObject findOne() {
 + return collection.findOne();
 + }
 +
 + public DBObject findOne(String collectionName) {
 + setCollection(collectionName);
 + return findOne();
 + }
 +
 + public DBObject findOne(BasicDBObject query) {
 + return collection.findOne(query);
 + }
 +
 + public DBObject findOne(String collectionName, BasicDBObject query) {
 + setCollection(collectionName);
 + return findOne(query);
 + }
 +
 + /**
 + * Retourne tous les documents de la collection
 +
 + * @param collectionName
 + * @return
 + */
 + public Cursor find(String collectionName) {
 + setCollection(collectionName);
 + return find();
 + }
 +
 + public Cursor find() {
 + return collection.find();
 + }
 +
 + public WriteResult insert(String collectionName, List<? extends Model> objects) {
 + setCollection(collectionName);
 + BasicDBObject[] dbList = new BasicDBObject[objects.size()];
 + int i = 0;
 + for (Model m : objects) {
 + dbList[i++] = DBOAdapter.objectToDBObject(m);
 + }
 + return collection.insert(dbList);
 + }
 +
 + public void save(String collectionName, List<? extends Model> objects) {
 + setCollection(collectionName);
 + for (Model m : objects) {
 + collection.save(DBOAdapter.objectToDBObject(m));
 + }
 + }
 +
 + public Cursor find(String collectionName, BasicDBObject query) {
 + setCollection(collectionName);
 + return find(query);
 + }
 +
 + public <T extends Model> List<T> load(Cursor cursor, Class<T> clazz) {
 + setCollection(clazz.getSimpleName());
 + List<T> result = new ArrayList<>();
 + while (cursor.hasNext()) {
 + result.add(DBOAdapter.dboToModel(cursor.next(), clazz));
 + }
 + return result;
 + }
 +
 + public <T extends Model> List<T> load(BasicDBObject query, Class<T> clazz) {
 + return load(find(query), clazz);
 + }
 +
 + public <T extends Model> List<T> load(Class<T> clazz) {
 + return load(find(clazz.getSimpleName()), clazz);
 + }
 +
 + public Cursor find(BasicDBObject query) {
 + return collection.find(query);
 + }
 +
 + public void setCollection(String name) {
 + collection = db.getCollection(name);
 + }
 +
 + public List<String> getDbNames() {
 + return mongoClient.getDatabaseNames();
 + }
 +
 + public void close() {
 + mongoClient.close();
 + }
 +
 + public void dropCollection(String name) {
 + this.setCollection(name);
 + collection.drop();
 + }
 +
 + public void dropCollection(Class<? extends Model> clazz) {
 + this.dropCollection(clazz.getSimpleName());
 + }
 +
 + @SuppressWarnings({ "rawtypes", "unchecked" })
 + public void dropCollections(Class... classes) {
 + for (Class clazz : classes) {
 + dropCollection(clazz);
  }  }
  }  }
  • java/mongodb.1523860942.txt.gz
  • Dernière modification : il y a 6 ans
  • (modification externe)