Mongodb integrates with Spring by referring to Spring-data-mongodb's operations on Mongodb in Spring official documents. A simple example is provided to prevent forgetting. Documentation address: docs. spring. iospring-datadata-mongodbdocs1.4.0.RELEASEreferencehtml Software Version: Maven-3.1.1Mongodb-2.4.8Ec
Mongodb integrates with Spring by referring to Spring-data-mongodb's operations on Mongodb in Spring official documents. A simple example is provided to prevent forgetting. Address: http://docs.spring.io/spring-data/data-mongodb/docs/1.4.0.RELEASE/reference/html/-related software version: Maven-3.1.1 Mongodb-2.4.8Ec
Mongodb integration with Spring
Refer to Spring-data-mongodb's operations on Mongodb in the official Spring documents, and provide a simple example to avoid forgetting.
Document address: http://docs.spring.io/spring-data/data-mongodb/docs/1.4.0.RELEASE/reference/html/
Related Software Version: Maven-3.1.1 Mongodb-2.4.8Eclipse in Eclipse to create a Maven project, pom. xml content is as follows:
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
The Spring configuration file is as follows:
Forum is the database name. If it does not exist, it is automatically created. Lee. forum. core. dao is the package name of dao. In this example, dao inherits MongoRepository and only needs to write the interface according to the rule. You will see it later. Entity class:
package lee.forum.core.entity;import java.util.Date;import org.springframework.data.annotation.Id;import org.springframework.data.mongodb.core.mapping.Document;@Documentpublic class User {@Idprivate 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 interface:
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
{public List
findByEmail(String email);public User findOneByUserId(String userId);}
This interface does not need to implement classes, but the method name must be written according to Spring rules. Only query is provided here. For more detailed usage, refer to the documentation. Find indicates the query. by is followed by the query condition and email is the User attribute so that Spring can know what you want to do.
Test class:
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 {@Autowiredprivate UserRepository userRepository;@Testpublic void findAll() {for (User user : userRepository.findAll()) {System.out.println(user.getEmail());}}@Testpublic void findByEmail() {for (User user : userRepository.findByEmail("libinsbox@137.com")) {System.out.println(user.getUserName());}}@Testpublic void findById() {System.out.println(userRepository.findOneByUserId("libinsbox@138.com").getUserName());}}