1. Download the example from the official website below, and the download address is as follows:
http://www.springsource.org/samples
2. Troubleshooting,
the
The SpringExample just downloaded has an error. First of all, it is not a maven project. Right-click to convert it into a maven project, and add some dependencies, such as spring-aop, spring-context, spring-core, log4j and other dependencies. At the same time, update your dependent packages and configurations. After the final operation is successful, run the next step.
3. Code explanation.
3.1
Method 1 (open mongo.exe in annotation mode, and create database yourdb and document yourCollection at the same time), the code is as follows:
package com.mkyong.config;
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;
/**
* Spring MongoDB configuration file
*
*/
@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(),”yourdb”,”yourCollection”);
}
}
3.2 Method 2 (configuration file method), the code is as follows:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:cOntext=”http://www.springframework.org/schema/context”
xmlns:mOngo=”http://www.springframework.org/schema/data/mongo”
xsi:schemaLocation=”http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>
3.3 Writing entity classes
package com.mkyong.user;
public class User {
private String id;
private String firstname;
private String lastname;
private int age;
public User(){};
public User(String id, String firstname, String lastname, int age) {
super();
This.id = id;
This.firstname = firstname;
This.lastname = lastname;
This.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
This.id = id;
}
public String getFirstname() {
Return firstname;
}
public void setFirstname(String firstname) {
This.firstname = firstname;
}
public String getLastname() {
Return lastname;
}
public void setLastname(String lastname) {
This.lastname = lastname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
This.age = age;
}
@Override
public String toString() {
return “User [id=” + id + “, firstname=” + firstname + “, lastname=”
+ lastname + “, age=” + age + “]”;
}
}
Finally, write the main class as follows:
package com.mkyong.core;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.document.mongodb.query.Update;
import com.mkyong.config.SpringMongoConfig;
import com.mkyong.user.User;
public class App
{
public static void main( String[] args )
{
//For Annotation annotation method
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
//For XML XML configuration file method
//ApplicationContext ctx = new GenericXmlApplicationContext(“mongo-config.xml”);
MongoOperations mongoOperation = (MongoOperations)ctx.getBean(“mongoTemplate”);
User user = new User(“1001”, “yong”, “mook kim”, 30);
//save
mongoOperation. save(“userprofile”, user);
//find
User savedUser = mongoOperation. findOne(“userprofile”,
New Query(Criteria.where(“id”).is(“1001”)),
User.class);
System.out.println(“savedUser: ” + savedUser);
//update
mongoOperation. updateFirst(“userprofile”,
New Query(Criteria.where(“firstname”).is(“yong”)),
Update.update(“lastname”, “new lastname”));
//find
User updatedUser = mongoOperation. findOne(
“userprofile”,
New Query(Criteria.where(“id”).is(“1001”)),
User.class);
System.out.println(“updatedUser: ” + updatedUser);
//delete
mongoOperation. remove(“userprofile”,
New Query(Criteria.where(“id”).is(“1001”)),
class);
//List
List listUser =
mongoOperation.getCollection(“userprofile”, User.class);
System.out.println(“Number of user = ” + listUser.size());
}
}
Since it is just some CURD operations, it is relatively simple, and you can understand it directly by looking at the code, so I won’t go into details.
4. Running effect
ass);
//For XML XML configuration file method
//ApplicationContext ctx = new GenericXmlApplicationContext(“mongo-config.xml”);
MongoOperations mongoOperation = (MongoOperations)ctx.getBean(“mongoTemplate”);
User user = new User(“1001”, “yong”, “mook kim”, 30);
//save
mongoOperation. save(“userprofile”, user);
//find
User savedUser = mongoOperation. findOne(“userprofile”,
New Query(Criteria.where(“id”).is(“1001”)),
User.class);
System.out.println(“savedUser: ” + savedUser);
//update
mongoOperation. updateFirst(“userprofile”,
New Query(Criteria.where(“firstname”).is(“yong”)),
Update.update(“lastname”, “new lastname”));
//find
User updatedUser = mongoOperation. findOne(
“userprofile”,
New Query(Criteria.where(“id”).is(“1001”)),
User.class);
System.out.println(“updatedUser: ” + updatedUser);
//delete
mongoOperation. remove(“userprofile”,
New Query(Criteria.where(“id”).is(“1001”)),
class);
//List
List listUser =
mongoOperation.getCollection(“userprofile”, User.class);
System.out.println(“Number of user = ” + listUser.size());
}
}
Since it is just some CURD operations, it is relatively simple, and you can understand it directly by looking at the code, so I won’t go into details.
4. Running effect