http://city81.blogspot.co.uk/2012/07/using-spring-data-to-access-mongodb.html
SpringMongoConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.config.AbstractMongoConfiguration;
import com.mongodb.Mongo;
@Configuration
public class SpringMongoConfig extends AbstractMongoConfiguration {
@Override
public @Bean Mongo mongo() throws Exception {
return new Mongo("localhost");
}
@Override
public @Bean MongoTemplate mongoTemplate() throws Exception {
return new MongoTemplate(mongo(),"blog","users");
}
}
MongoApp.java (Main)
import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class MongoApp {
public static void main(String[] args) throws UnknownHostException {
try {
// connect to mongoDB
Mongo mongo = new Mongo("localhost", 27017);
// if database doesn't exists, mongoDB will create it
DB db = mongo.getDB("blog");
// Get collection from MongoDB, database named "mydb"
// if collection doesn't exists, mongoDB will create it
DBCollection collection = db.getCollection("users");
// create a document to store attributes
BasicDBObject document = new BasicDBObject();
document.get("_id");
// save it into collection named "myCollection"
System.out.println( collection.find());
DBCursor cur = collection.find();
while(cur.hasNext()) {
System.out.println(cur.next());
}
// search query
} catch (MongoException e) {
e.printStackTrace();
}
}
}
User.java
public class User {
private String id;
private String password;
public User(){};
public User(String id, String password) {
super();
this.id = id;
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", firstname=" + password + "]";
}
}
pom.xml
Just change the groupID by your package name
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId></groupId>
<artifactId>SpringExample</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>SpringMongoExample</name>
<url>http://maven.apache.org</url>
<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.10</version>
<scope>test</scope>
</dependency>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<!-- mongodb java driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.0.0.M2</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
No comments:
Post a Comment