下面讲解如何使用spring-data-mongdb来对mongdb进行增删改查的操作
你需要的工具:
<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> <!-- 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> </project>
3.spring的配置
可以通过两种方式来配置spring data来使用mongodb
需要在java文件中声明spring的mongodb注解
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"); } }
然后在main函数中就可以通过下面方式来获取mongodbtemplate
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class); ApplicationContext ctx = new GenericXmlApplicationContext("mongo-config.xml");
XML
通过xml方式来配置
<?xml version="1.0" encoding="UTF-8"?>
<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"> <!-- Default bean name is 'mongo' --> <mongo:mongo host="localhost" port="27017" /> <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> <constructor-arg ref="mongo" /> <constructor-arg name="databaseName" value="yourdb" /> <constructor-arg name="defaultCollectionName" value="yourCollection" /> </bean> <!-- To translate any MongoExceptions thrown in @Repository annotated classes --> <context:annotation-config /> </beans>
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class); ApplicationContext ctx = new GenericXmlApplicationContext("mongo-config.xml");
4.定义用户模型
package com.mkyong.user;
public class User { private String id; private String firstname; private String lastname; private int age; //getter and setter methods }
5.CRUD操作
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 ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class); //For XML //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")), User.class); //List List<User> listUser = mongoOperation.getCollection("userprofile", User.class); System.out.println("Number of user = " + listUser.size()); } }
输出:
savedUser : User [id=1001, firstname=yong, lastname=mook kim, age=30] updatedUser : User [id=1001, firstname=yong, lastname=new lastname, age=30] Number of user = 0
参考:http://www.mkyong.com/mongodb/spring-data-mongodb-hello-world-example/