Mongodb整合Spring 参照Spring官方文档中Spring-data-mongodb对Mongodb的操作,做了一个简单的示例,以防遗忘。 文档地址:http://docs.spring.io/spring-data/data-mongodb/docs/1.4.0.RELEASE/reference/html/ 相关软件版本: Maven-3.1.1 Mongodb-2.4.8Ec
参照Spring官方文档中Spring-data-mongodb对Mongodb的操作,做了一个简单的示例,以防遗忘。
文档地址:http://docs.spring.io/spring-data/data-mongodb/docs/1.4.0.RELEASE/reference/html/
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>lee.forum</groupId> <artifactId>Forum1.0.1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!--========================================================== --> <!--======================= 测试 ========================== --> <!--========================================================== --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <!--=========================================================== --> <!--======================= Spring ========================== --> <!--=========================================================== --> <!-- Spring-data --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.3.4.RELEASE</version> </dependency> <!-- Spring core beans tx context expression test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.2.8.RELEASE</version> </dependency> <!--=========================================================== --> <!--======================== Mongodb ========================== --> <!--=========================================================== --> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.11.4</version> </dependency> <!--=========================================================== --> <!--======================== Logs ========================== --> <!--=========================================================== --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.1</version> </dependency> </dependencies> <repositories> <repository> <id>spring-milestone</id> <name>Spring Maven MILESTONE Repository</name> <url>http://repo.spring.io/libs-milestone</url> </repository> </repositories> </project>
<?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" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <context:annotation-config /> <context:component-scan base-package="lee.forum" /> <mongo:repositories base-package="lee.forum.core.dao" /> <!-- Default bean name is 'mongo' --> <mongo:mongo host="localhost" port="27017"/> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo"/> <constructor-arg name="databaseName" value="forum"/> </bean> </beans>forum为数据库名称,如果不存在则会自动创建。lee.forum.core.dao为dao的所在包名,这个例子的dao继承了MongoRepository,只需要按照规则写接口就行了。后面将会看到。
package lee.forum.core.entity; import java.util.Date; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document public class User { @Id private String userId; private String userName; private String email; private String sex; private Date birthday; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
package lee.forum.core.dao; import java.util.List; import lee.forum.core.entity.User; import org.springframework.data.mongodb.repository.MongoRepository; public interface UserRepository extends MongoRepository<User, String> { public List<User> findByEmail(String email); public User findOneByUserId(String userId); }这个接口不需要实现类,但是方法的名称必须按照Spring的规则来写。这里只写了查询,更详细的用法请参考文档。 find表示查询,by后面是查询的条件,email是User的属性,这样Spring才知道你要做什么。
package lee.forum.test.dao; import lee.forum.core.dao.UserRepository; import lee.forum.core.entity.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:applicationContext-data.xml" }) public class RespositoryTest { @Autowired private UserRepository userRepository; @Test public void findAll() { for (User user : userRepository.findAll()) { System.out.println(user.getEmail()); } } @Test public void findByEmail() { for (User user : userRepository.findByEmail("libinsbox@137.com")) { System.out.println(user.getUserName()); } } @Test public void findById() { System.out.println(userRepository.findOneByUserId("libinsbox@138.com") .getUserName()); } }