Skip to content

Commit bee6c50

Browse files
committed
Spring Boot 中使用 MongoDB 增删改查
1 parent 54853b1 commit bee6c50

9 files changed

Lines changed: 358 additions & 0 deletions

File tree

‎spring-boot-mongodb/.gitignore‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/

‎spring-boot-mongodb/pom.xml‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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>io.ymq.example</groupId>
7+
<artifactId>spring-boot-mongodb</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-mongodb</name>
12+
<description>Spring Boot 中使用 MongoDB 增删改查</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.10.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-web</artifactId>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-test</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>com.alibaba</groupId>
52+
<artifactId>fastjson</artifactId>
53+
<version>1.2.4</version>
54+
</dependency>
55+
56+
</dependencies>
57+
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-maven-plugin</artifactId>
63+
</plugin>
64+
<plugin>
65+
<groupId>org.apache.maven.plugins</groupId>
66+
<artifactId>maven-surefire-plugin</artifactId>
67+
<configuration>
68+
<skip>true</skip>
69+
<testFailureIgnore>true</testFailureIgnore>
70+
</configuration>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
75+
76+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.ymq.example.mongodb;
2+
3+
/**
4+
* 描述: Demo DAO
5+
*
6+
* @author yanpenglei
7+
* @create 2018-02-03 16:56
8+
**/
9+
public interface DemoDao {
10+
11+
void saveDemo(DemoEntity demoEntity);
12+
13+
void removeDemo(Long id);
14+
15+
void updateDemo(DemoEntity demoEntity);
16+
17+
DemoEntity findDemoById(Long id);
18+
19+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.ymq.example.mongodb;
2+
3+
import org.springframework.data.mongodb.core.MongoTemplate;
4+
import org.springframework.data.mongodb.core.query.Criteria;
5+
import org.springframework.data.mongodb.core.query.Query;
6+
import org.springframework.data.mongodb.core.query.Update;
7+
import org.springframework.stereotype.Component;
8+
9+
import javax.annotation.Resource;
10+
11+
/**
12+
* 描述: Demo DAO 实现
13+
*
14+
* @author yanpenglei
15+
* @create 2018-02-03 16:57
16+
**/
17+
@Component
18+
public class DemoDaoImpl implements DemoDao {
19+
20+
@Resource
21+
private MongoTemplate mongoTemplate;
22+
23+
@Override
24+
public void saveDemo(DemoEntity demoEntity) {
25+
mongoTemplate.save(demoEntity);
26+
}
27+
28+
@Override
29+
public void removeDemo(Long id) {
30+
mongoTemplate.remove(id);
31+
}
32+
33+
@Override
34+
public void updateDemo(DemoEntity demoEntity) {
35+
Query query = new Query(Criteria.where("id").is(demoEntity.getId()));
36+
37+
Update update = new Update();
38+
update.set("title", demoEntity.getTitle());
39+
update.set("description", demoEntity.getDescription());
40+
update.set("by", demoEntity.getBy());
41+
update.set("url", demoEntity.getUrl());
42+
43+
mongoTemplate.updateFirst(query, update, DemoEntity.class);
44+
}
45+
46+
@Override
47+
public DemoEntity findDemoById(Long id) {
48+
Query query = new Query(Criteria.where("id").is(id));
49+
DemoEntity demoEntity = mongoTemplate.findOne(query, DemoEntity.class);
50+
return demoEntity;
51+
}
52+
53+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.ymq.example.mongodb;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.mongodb.core.mapping.Document;
5+
6+
import java.io.Serializable;
7+
8+
/**
9+
* 描述: DemoEntity
10+
*
11+
* @author yanpenglei
12+
* @create 2018-02-03 16:52
13+
**/
14+
@Document(collection = "demo_collection")
15+
public class DemoEntity implements Serializable {
16+
17+
@Id
18+
private Long id;
19+
20+
private String title;
21+
22+
private String description;
23+
24+
private String by;
25+
26+
private String url;
27+
28+
public Long getId() {
29+
return id;
30+
}
31+
32+
public void setId(Long id) {
33+
this.id = id;
34+
}
35+
36+
public String getTitle() {
37+
return title;
38+
}
39+
40+
public void setTitle(String title) {
41+
this.title = title;
42+
}
43+
44+
public String getDescription() {
45+
return description;
46+
}
47+
48+
public void setDescription(String description) {
49+
this.description = description;
50+
}
51+
52+
public String getBy() {
53+
return by;
54+
}
55+
56+
public void setBy(String by) {
57+
this.by = by;
58+
}
59+
60+
public String getUrl() {
61+
return url;
62+
}
63+
64+
public void setUrl(String url) {
65+
this.url = url;
66+
}
67+
68+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.ymq.example.mongodb;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* 描述: 启动服务
8+
*
9+
* @author: yanpenglei
10+
* @create: 2018/2/5 13:52
11+
*/
12+
@SpringBootApplication
13+
public class SpringBootMongodbApplication {
14+
15+
public static void main(String[] args) {
16+
SpringApplication.run(SpringBootMongodbApplication.class, args);
17+
}
18+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.application.name=spring-boot-mongodb
2+
# mongodb
3+
spring.data.mongodb.uri=mongodb://192.168.252.121:20000,192.168.252.122:20000,192.168.252.12:20000/demo
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.ymq.example.mongodb;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
10+
/**
11+
* 描述: 单元测试
12+
*
13+
* @author: yanpenglei
14+
* @create: 2018/2/5 13:52
15+
*/
16+
@RunWith(SpringRunner.class)
17+
@SpringBootTest(classes = SpringBootMongodbApplication.class)
18+
public class SpringBootMongodbApplicationTests {
19+
20+
@Autowired
21+
private DemoDao demoDao;
22+
23+
@Test
24+
public void saveDemoTest() {
25+
26+
DemoEntity demoEntity = new DemoEntity();
27+
demoEntity.setId(1L);
28+
demoEntity.setTitle("Spring Boot 中使用 MongoDB");
29+
demoEntity.setDescription("关注公众号,搜云库,专注于开发技术的研究与知识分享");
30+
demoEntity.setBy("souyunku");
31+
demoEntity.setUrl("http://www.souyunku.com");
32+
33+
demoDao.saveDemo(demoEntity);
34+
35+
demoEntity = new DemoEntity();
36+
demoEntity.setId(2L);
37+
demoEntity.setTitle("Spring Boot 中使用 MongoDB");
38+
demoEntity.setDescription("关注公众号,搜云库,专注于开发技术的研究与知识分享");
39+
demoEntity.setBy("souyunku");
40+
demoEntity.setUrl("http://www.souyunku.com");
41+
42+
demoDao.saveDemo(demoEntity);
43+
}
44+
45+
@Test
46+
public void removeDemoTest() {
47+
demoDao.removeDemo(2L);
48+
}
49+
50+
@Test
51+
public void updateDemoTest() {
52+
53+
DemoEntity demoEntity = new DemoEntity();
54+
demoEntity.setId(1L);
55+
demoEntity.setTitle("Spring Boot 中使用 MongoDB 更新数据");
56+
demoEntity.setDescription("关注公众号,搜云库,专注于开发技术的研究与知识分享");
57+
demoEntity.setBy("souyunku");
58+
demoEntity.setUrl("http://www.souyunku.com");
59+
60+
demoDao.updateDemo(demoEntity);
61+
}
62+
63+
@Test
64+
public void findDemoByIdTest() {
65+
66+
DemoEntity demoEntity = demoDao.findDemoById(1L);
67+
68+
System.out.println(JSONObject.toJSONString(demoEntity));
69+
}
70+
71+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.ymq.example.rocketmq;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/**
8+
* 描述:
9+
*
10+
* @author yanpenglei
11+
* @create 2018-02-02 16:50
12+
**/
13+
public class test {
14+
15+
public static void main(String[] args) {
16+
17+
List<String> names = Arrays.asList("1", "3", "2", "4");
18+
19+
Collections.sort(names,(a,b) -> b.compareTo(a));
20+
21+
System.out.println(names);
22+
23+
24+
25+
}
26+
}

0 commit comments

Comments
 (0)