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