开发环境
java version "1.8.0_171"
Apache Maven 3.5.4
mongodb-win32-x86_64-2008plus-ssl-4.0.2-signed
在IDEA中创建新项目,直接一路next到下图,选择这一项之后继续直接一路next然后finish
项目结构如下:
创建Model类
package com.example.mongodb.model;
import org.springframework.data.annotation.Id;
public class User {
@Id
private Long id;
private String username;
private Integer age;
public User(Long id, String username, Integer age) {
this.id = id;
this.username = username;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
创建仓储类
package com.example.mongodb.repository;
import com.example.mongodb.model.User;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface UserRepository extends MongoRepository<User, Long> {
User findByUsername(String username);
}
测试类:
package com.example.mongodb;
import com.example.mongodb.model.User;
import com.example.mongodb.repository.UserRepository;
import org.junit.Assert;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MongodbApplicationTests {
@Autowired
private UserRepository userRepository;
@Test
public void contextLoads() {
}
@Test
public void test() throws Exception {
// 创建三个User,并验证User总数
userRepository.save(new User(1L, "xiaoming", 30));
userRepository.save(new User(2L, "xiaohong", 40));
userRepository.save(new User(3L, "xiaolan", 50));
System.out.println(userRepository.findAll().size());
}
}
配置文件application.properties
spring.data.mongodb.uri=mongodb://localhost:27017/test
运行测试类,观察测试结果
附录 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>mongodb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mongodb</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>