Mongodb整合Spring示例
程序员文章站
2022-03-20 11:06:43
...
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
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.8 Eclipse
在Eclipse中创建一个Maven项目,pom.xml内容如下:
4.0.0 lee.forum Forum1.0.1 0.0.1-SNAPSHOT war UTF-8 junit junit 4.8.2 test org.springframework.data spring-data-mongodb 1.3.4.RELEASE org.springframework spring-core 3.2.8.RELEASE org.springframework spring-beans 3.2.8.RELEASE org.springframework spring-tx 3.2.8.RELEASE org.springframework spring-context 3.2.8.RELEASE org.springframework spring-expression 3.2.8.RELEASE org.springframework spring-test 3.2.8.RELEASE org.mongodb mongo-java-driver 2.11.4 org.slf4j slf4j-log4j12 1.7.1 spring-milestone Spring Maven MILESTONE Repository http://repo.spring.io/libs-milestone
Spring配置文件安如下:
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; } }
Dao接口:
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这个接口不需要实现类,但是方法的名称必须按照Spring的规则来写。这里只写了查询,更详细的用法请参考文档。 find表示查询,by后面是查询的条件,email是User的属性,这样Spring才知道你要做什么。{ public List findByEmail(String email); public User findOneByUserId(String userId); }
测试类:
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()); } }