Skip to content

Commit dec1b80

Browse files
committed
使用NoSQL数据库(二):MongoDB
1 parent 4fa3d8b commit dec1b80

File tree

6 files changed

+181
-0
lines changed

6 files changed

+181
-0
lines changed

Chapter3-2-6/pom.xml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>demo</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>demo</name>
12+
<description>Spring Boot project</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.2.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
41+
</dependency>
42+
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-maven-plugin</artifactId>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
54+
55+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.didispace.domain;
2+
3+
import org.springframework.data.annotation.Id;
4+
5+
/**
6+
* @author 程序猿DD
7+
* @version 1.0.0
8+
* @date 16/4/27 下午10:04.
9+
* @blog http://blog.didispace.com
10+
*/
11+
public class User {
12+
13+
@Id
14+
private Long id;
15+
16+
private String username;
17+
private Integer age;
18+
19+
public User(Long id, String username, Integer age) {
20+
this.id = id;
21+
this.username = username;
22+
this.age = age;
23+
}
24+
25+
public Long getId() {
26+
return id;
27+
}
28+
29+
public void setId(Long id) {
30+
this.id = id;
31+
}
32+
33+
public String getUsername() {
34+
return username;
35+
}
36+
37+
public void setUsername(String username) {
38+
this.username = username;
39+
}
40+
41+
public Integer getAge() {
42+
return age;
43+
}
44+
45+
public void setAge(Integer age) {
46+
this.age = age;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace.domain;
2+
3+
import org.springframework.data.mongodb.repository.MongoRepository;
4+
5+
/**
6+
* @author 程序猿DD
7+
* @version 1.0.0
8+
* @date 16/4/27 下午10:16.
9+
* @blog http://blog.didispace.com
10+
*/
11+
public interface UserRepository extends MongoRepository<User, Long> {
12+
13+
User findByUsername(String username);
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/test
2+
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.didispace;
2+
3+
import com.didispace.domain.User;
4+
import com.didispace.domain.UserRepository;
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.SpringApplicationConfiguration;
11+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12+
13+
14+
@RunWith(SpringJUnit4ClassRunner.class)
15+
@SpringApplicationConfiguration(Application.class)
16+
public class ApplicationTests {
17+
18+
@Autowired
19+
private UserRepository userRepository;
20+
21+
@Before
22+
public void setUp() {
23+
userRepository.deleteAll();
24+
}
25+
26+
@Test
27+
public void test() throws Exception {
28+
29+
// 创建三个User,并验证User总数
30+
userRepository.save(new User(1L, "didi", 30));
31+
userRepository.save(new User(2L, "mama", 40));
32+
userRepository.save(new User(3L, "kaka", 50));
33+
Assert.assertEquals(3, userRepository.findAll().size());
34+
35+
// 删除一个User,再验证User总数
36+
User u = userRepository.findOne(1L);
37+
userRepository.delete(u);
38+
Assert.assertEquals(2, userRepository.findAll().size());
39+
40+
// 删除一个User,再验证User总数
41+
u = userRepository.findByUsername("mama");
42+
userRepository.delete(u);
43+
Assert.assertEquals(1, userRepository.findAll().size());
44+
45+
}
46+
47+
}

0 commit comments

Comments
 (0)