environment
jdk1.8
maven
tool
idea
Architecture
springboot
(1) Install MongoDB
You can refer to the tutorial I wrote: http://chuichuige.iteye.com/admin/blogs/2401146
(2) Use idea to build a springboot project
You can refer to http://blog.csdn.net/u013248535/article/details/55100979
(3) Modify the pom.xml file
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
(4) Add the MongoDB data configuration in the application.properties file (this place must be configured with MongoDB to add users)
Note: After the MongoDB database configuration is completed, the user root password root must be added
The method of adding may be different due to the different versions downloaded.
For details, please refer to
http://chuichuige.iteye.com/blog/2401151
spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.database=admin spring.data.mongodb.username=root spring.data.mongodb.password=root
(5) Write database Dao layer code
Note: do not write findUserByUsername as findUserByUserName, if it is written in this way, it will violate the specification of jpa, and an error will be reported
Reference: http://blog.csdn.net/u022812849/article/details/44277355
package mongodb.dao; import mongodb.pojo.User; import org.springframework.data.mongodb.repository.MongoRepository; /** * Created by pactera on 2017/11/26. */ public interface UserRepository extends MongoRepository<User,Long> { User findUserByUsername(String username); }
(6) Use springboot unit test
package mongodb; import mongodb.dao.UserRepository; import mongodb.pojo.User; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; /* Created by pactera on 2017/11/26. */ @RunWith(SpringRunner.class) @SpringBootTest public class AppTest { @Autowired private UserRepository userRepository; // delete all data /* @Before public void setUp(){ System.out.println(" --- delete all data start ----- "); userRepository.deleteAll(); System.out.println(" --- delete all data end ----- "); }*/ // add user test /* @Test public void test(){ // Add user userRepository.save(new User(1L,"lyf","123456")); userRepository.save(new User(2L,"lyf2","1234567")); userRepository.save(new User(3L,"lyf3","12345678")); //delete users //userRepository.delete(1L); System.out.println(userRepository.findAll().size()); } */ /** * Query user test *//* @Test public void testQuery(){ List<User> users = userRepository.findAll(); for (User user: users) { System.out.println("-----"); System.out.println(user); } } */ /** * Query user name based on user name * */ @Test public void testQueryfindUserByUsername(){ User user = userRepository.findUserByUsername("lyf"); System.out.println(user); } }
The final test results are as follows (due to the time relationship, after I added users after the test, I tested querying users based on their user names),
The test results are as follows
Code cloud git source code: https://gitee.com/chui0704/springboot
Reference: http://blog.didispace.com/springbootmongodb/ (Because it is a little different from what the author wrote, I wrote one myself)