Spring Data – MongoDB Sample
MongoDB est un SGBDOD (système de gestion de base de données orientée documents). C’est une solution NoSQL utilisé par plusieurs sociétés telque MTV, NY-Times ou SourceForge.
L’exemple que je vais présenter est une application Maven utilisant le framework Spring Data MongoDB afin de communiquer avec le SGBDOD et de faire les requêtes de bases à savoir l’insertion, la suppression, et la recherche.
La première étape est d’ajouter les dépendances maven :
-
<repositories>
-
<repository>
-
<id>spring-milestone</id>
-
<name>Spring Maven MILESTONE Repository</name>
-
<url>http://maven.springframework.org/milestone</url>
-
</repository>
-
</repositories>
-
<dependencies>
-
<dependency>
-
<groupId>junit</groupId>
-
<artifactId>junit</artifactId>
-
<version>4.8.2</version>
-
<scope>test</scope>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-core</artifactId>
-
<version>3.1.1.RELEASE</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-context</artifactId>
-
<version>3.1.1.RELEASE</version>
-
</dependency>
-
<dependency>
-
<groupId>org.mongodb</groupId>
-
<artifactId>mongo-java-driver</artifactId>
-
<version>2.7.3</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.data</groupId>
-
<artifactId>spring-data-mongodb</artifactId>
-
<version>1.0.1.RELEASE</version>
-
</dependency>
-
<dependency>
-
<groupId>cglib</groupId>
-
<artifactId>cglib</artifactId>
-
<version>2.2.2</version>
-
</dependency>
-
</dependencies>
Pour commencer, nous allons créer une classe qui représente l’objet qu’on devra stocker. Dans notre cas d’étude, ce sera la classe Personne avec les attributs id, nom et prenom.
Par la suite, nous allons configurer la connexion à notre base de donnée MongoDB, nous supposons que c’est une instance de base en local sur le port de base à savoir 27017.
-
package com.nihed.mongodbdemo;
-
-
import org.springframework.context.annotation.Configuration;
-
import com.mongodb.Mongo;
-
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
-
-
-
@Configuration
-
public class SpringMongoConfig extends AbstractMongoConfiguration {
-
-
@Override
-
return "LocalDatabase";
-
}
-
-
@Override
-
return new Mongo("localhost");
-
}
-
-
}
Enfin, voila le main avec nos exemples d’insertion suppression et recherche.
-
package com.nihed.mongodbdemo;
-
-
import java.util.List;
-
import org.springframework.context.ApplicationContext;
-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
-
import org.springframework.data.mongodb.core.MongoOperations;
-
import org.springframework.data.mongodb.core.query.Criteria;
-
import org.springframework.data.mongodb.core.query.Query;
-
-
/**
-
* Spring Data MongoDB Sample
-
*
-
*/
-
public class App {
-
-
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
-
-
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
-
-
//Insert
-
{
-
Personne personne1 = new Personne(1, "nom1", "prenom1");
-
Personne personne2 = new Personne(2, "nom2", "prenom2");
-
mongoOperation.save(personne1);
-
mongoOperation.save(personne2);
-
}
-
//Select *
-
{
-
List<Personne> personnes = mongoOperation.findAll(Personne.class);
-
for (Personne personne : personnes) {
-
}
-
}
-
-
//select where nom like %1
-
{
-
List<Personne> personnes = mongoOperation.find(new Query(Criteria.where("nom").regex(".*1")), Personne.class);
-
for (Personne personne : personnes) {
-
}
-
}
-
-
//delete where nom like %1
-
{
-
mongoOperation.remove(new Query(Criteria.where("nom").regex(".*1")), Personne.class);
-
}
-
-
//Select *
-
{
-
List<Personne> personnes = mongoOperation.findAll(Personne.class);
-
for (Personne personne : personnes) {
-
}
-
}
-
-
}
-
}
j’ai un prob en hibernate quelq’un peut m’aider :'(