Skip to content

Commit b7c9819

Browse files
committed
✨ spring-boot-demo-mongodb 完成
1 parent d364ee0 commit b7c9819

File tree

6 files changed

+301
-0
lines changed

6 files changed

+301
-0
lines changed

spring-boot-demo-mongodb/pom.xml

+21
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,32 @@
2828
<artifactId>spring-boot-starter</artifactId>
2929
</dependency>
3030

31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-data-mongodb</artifactId>
34+
</dependency>
35+
3136
<dependency>
3237
<groupId>org.springframework.boot</groupId>
3338
<artifactId>spring-boot-starter-test</artifactId>
3439
<scope>test</scope>
3540
</dependency>
41+
42+
<dependency>
43+
<groupId>cn.hutool</groupId>
44+
<artifactId>hutool-all</artifactId>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>com.google.guava</groupId>
49+
<artifactId>guava</artifactId>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>org.projectlombok</groupId>
54+
<artifactId>lombok</artifactId>
55+
<optional>true</optional>
56+
</dependency>
3657
</dependencies>
3758

3859
<build>
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
11
package com.xkcoding.mongodb;
22

3+
import cn.hutool.core.lang.Snowflake;
4+
import cn.hutool.core.util.IdUtil;
35
import org.springframework.boot.SpringApplication;
46
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
58

9+
/**
10+
* <p>
11+
* 启动器
12+
* </p>
13+
*
14+
* @package: com.xkcoding.mongodb
15+
* @description: 启动器
16+
* @author: yangkai.shen
17+
* @date: Created in 2018-12-28 16:14
18+
* @copyright: Copyright (c) 2018
19+
* @version: V1.0
20+
* @modified: yangkai.shen
21+
*/
622
@SpringBootApplication
723
public class SpringBootDemoMongodbApplication {
824

925
public static void main(String[] args) {
1026
SpringApplication.run(SpringBootDemoMongodbApplication.class, args);
1127
}
1228

29+
@Bean
30+
public Snowflake snowflake() {
31+
return IdUtil.createSnowflake(1, 1);
32+
}
33+
1334
}
1435

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.xkcoding.mongodb.model;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
import org.springframework.data.annotation.Id;
8+
9+
import java.util.Date;
10+
11+
/**
12+
* <p>
13+
* 文章实体类
14+
* </p>
15+
*
16+
* @package: com.xkcoding.mongodb.model
17+
* @description: 文章实体类
18+
* @author: yangkai.shen
19+
* @date: Created in 2018-12-28 16:21
20+
* @copyright: Copyright (c) 2018
21+
* @version: V1.0
22+
* @modified: yangkai.shen
23+
*/
24+
@Data
25+
@Builder
26+
@NoArgsConstructor
27+
@AllArgsConstructor
28+
public class Article {
29+
/**
30+
* 文章id
31+
*/
32+
@Id
33+
private Long id;
34+
35+
/**
36+
* 文章标题
37+
*/
38+
private String title;
39+
40+
/**
41+
* 文章内容
42+
*/
43+
private String content;
44+
45+
/**
46+
* 创建时间
47+
*/
48+
private Date createTime;
49+
50+
/**
51+
* 更新时间
52+
*/
53+
private Date updateTime;
54+
55+
/**
56+
* 点赞数量
57+
*/
58+
private Long thumbUp;
59+
60+
/**
61+
* 访客数量
62+
*/
63+
private Long visits;
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.xkcoding.mongodb.repository;
2+
3+
import com.xkcoding.mongodb.model.Article;
4+
import org.springframework.data.mongodb.repository.MongoRepository;
5+
6+
import java.util.List;
7+
8+
/**
9+
* <p>
10+
* 文章 Dao
11+
* </p>
12+
*
13+
* @package: com.xkcoding.mongodb.repository
14+
* @description: 文章 Dao
15+
* @author: yangkai.shen
16+
* @date: Created in 2018-12-28 16:30
17+
* @copyright: Copyright (c) 2018
18+
* @version: V1.0
19+
* @modified: yangkai.shen
20+
*/
21+
public interface ArticleRepository extends MongoRepository<Article, Long> {
22+
/**
23+
* 根据标题模糊查询
24+
*
25+
* @param title 标题
26+
* @return 满足条件的文章列表
27+
*/
28+
List<Article> findByTitleLike(String title);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring:
2+
data:
3+
mongodb:
4+
host: localhost
5+
port: 27017
6+
database: article_db
7+
logging:
8+
level:
9+
org.springframework.data.mongodb.core: debug
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package com.xkcoding.mongodb.repository;
2+
3+
import cn.hutool.core.date.DateUtil;
4+
import cn.hutool.core.lang.Snowflake;
5+
import cn.hutool.core.util.RandomUtil;
6+
import cn.hutool.json.JSONUtil;
7+
import com.google.common.collect.Lists;
8+
import com.xkcoding.mongodb.SpringBootDemoMongodbApplicationTests;
9+
import com.xkcoding.mongodb.model.Article;
10+
import lombok.extern.slf4j.Slf4j;
11+
import org.junit.Test;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.data.domain.Page;
14+
import org.springframework.data.domain.PageRequest;
15+
import org.springframework.data.domain.Sort;
16+
import org.springframework.data.mongodb.core.MongoTemplate;
17+
import org.springframework.data.mongodb.core.query.Criteria;
18+
import org.springframework.data.mongodb.core.query.Query;
19+
import org.springframework.data.mongodb.core.query.Update;
20+
21+
import java.util.List;
22+
import java.util.stream.Collectors;
23+
24+
/**
25+
* <p>
26+
* 测试操作 MongoDb
27+
* </p>
28+
*
29+
* @package: com.xkcoding.mongodb.repository
30+
* @description: 测试操作 MongoDb
31+
* @author: yangkai.shen
32+
* @date: Created in 2018-12-28 16:35
33+
* @copyright: Copyright (c) 2018
34+
* @version: V1.0
35+
* @modified: yangkai.shen
36+
*/
37+
@Slf4j
38+
public class ArticleRepositoryTest extends SpringBootDemoMongodbApplicationTests {
39+
@Autowired
40+
private ArticleRepository articleRepo;
41+
42+
@Autowired
43+
private MongoTemplate mongoTemplate;
44+
45+
@Autowired
46+
private Snowflake snowflake;
47+
48+
/**
49+
* 测试新增
50+
*/
51+
@Test
52+
public void testSave() {
53+
Article article = new Article(1L, RandomUtil.randomString(20), RandomUtil.randomString(150), DateUtil.date(), DateUtil
54+
.date(), 0L, 0L);
55+
articleRepo.save(article);
56+
log.info("【article】= {}", JSONUtil.toJsonStr(article));
57+
}
58+
59+
/**
60+
* 测试新增列表
61+
*/
62+
@Test
63+
public void testSaveList() {
64+
List<Article> articles = Lists.newArrayList();
65+
for (int i = 0; i < 10; i++) {
66+
articles.add(new Article(snowflake.nextId(), RandomUtil.randomString(20), RandomUtil.randomString(150), DateUtil
67+
.date(), DateUtil.date(), 0L, 0L));
68+
}
69+
articleRepo.saveAll(articles);
70+
71+
log.info("【articles】= {}", JSONUtil.toJsonStr(articles.stream()
72+
.map(Article::getId)
73+
.collect(Collectors.toList())));
74+
}
75+
76+
/**
77+
* 测试更新
78+
*/
79+
@Test
80+
public void testUpdate() {
81+
articleRepo.findById(1L).ifPresent(article -> {
82+
article.setTitle(article.getTitle() + "更新之后的标题");
83+
article.setUpdateTime(DateUtil.date());
84+
articleRepo.save(article);
85+
log.info("【article】= {}", JSONUtil.toJsonStr(article));
86+
});
87+
}
88+
89+
/**
90+
* 测试删除
91+
*/
92+
@Test
93+
public void testDelete() {
94+
// 根据主键删除
95+
articleRepo.deleteById(1L);
96+
97+
// 全部删除
98+
articleRepo.deleteAll();
99+
}
100+
101+
/**
102+
* 测试点赞数、访客数,使用save方式更新点赞、访客
103+
*/
104+
@Test
105+
public void testThumbUp() {
106+
articleRepo.findById(1L).ifPresent(article -> {
107+
article.setThumbUp(article.getThumbUp() + 1);
108+
article.setVisits(article.getVisits() + 1);
109+
articleRepo.save(article);
110+
log.info("【标题】= {}【点赞数】= {}【访客数】= {}", article.getTitle(), article.getThumbUp(), article.getVisits());
111+
});
112+
}
113+
114+
/**
115+
* 测试点赞数、访客数,使用更优雅/高效的方式更新点赞、访客
116+
*/
117+
@Test
118+
public void testThumbUp2() {
119+
Query query = new Query();
120+
query.addCriteria(Criteria.where("_id").is(1L));
121+
Update update = new Update();
122+
update.inc("thumbUp", 1L);
123+
update.inc("visits", 1L);
124+
mongoTemplate.updateFirst(query, update, "article");
125+
126+
articleRepo.findById(1L)
127+
.ifPresent(article -> log.info("【标题】= {}【点赞数】= {}【访客数】= {}", article.getTitle(), article.getThumbUp(), article
128+
.getVisits()));
129+
}
130+
131+
/**
132+
* 测试分页排序查询
133+
*/
134+
@Test
135+
public void testQuery() {
136+
Sort sort = Sort.by("thumbUp", "updateTime").descending();
137+
PageRequest pageRequest = PageRequest.of(0, 5, sort);
138+
Page<Article> all = articleRepo.findAll(pageRequest);
139+
log.info("【总页数】= {}", all.getTotalPages());
140+
log.info("【总条数】= {}", all.getTotalElements());
141+
log.info("【当前页数据】= {}", JSONUtil.toJsonStr(all.getContent()
142+
.stream()
143+
.map(article -> "文章标题:" + article.getTitle() + "点赞数:" + article.getThumbUp() + "更新时间:" + article.getUpdateTime())
144+
.collect(Collectors.toList())));
145+
}
146+
147+
/**
148+
* 测试根据标题模糊查询
149+
*/
150+
@Test
151+
public void testFindByTitleLike() {
152+
List<Article> articles = articleRepo.findByTitleLike("更新");
153+
log.info("【articles】= {}", JSONUtil.toJsonStr(articles));
154+
}
155+
156+
}

0 commit comments

Comments
 (0)