diff --git a/spring-boot-demo-mongodb/pom.xml b/spring-boot-demo-mongodb/pom.xml
index 3211945..c88b49f 100644
--- a/spring-boot-demo-mongodb/pom.xml
+++ b/spring-boot-demo-mongodb/pom.xml
@@ -28,11 +28,32 @@
spring-boot-starter
+
+ org.springframework.boot
+ spring-boot-starter-data-mongodb
+
+
org.springframework.boot
spring-boot-starter-test
test
+
+
+ cn.hutool
+ hutool-all
+
+
+
+ com.google.guava
+ guava
+
+
+
+ org.projectlombok
+ lombok
+ true
+
diff --git a/spring-boot-demo-mongodb/src/main/java/com/xkcoding/mongodb/SpringBootDemoMongodbApplication.java b/spring-boot-demo-mongodb/src/main/java/com/xkcoding/mongodb/SpringBootDemoMongodbApplication.java
index 7b9d0a8..bf63ed5 100644
--- a/spring-boot-demo-mongodb/src/main/java/com/xkcoding/mongodb/SpringBootDemoMongodbApplication.java
+++ b/spring-boot-demo-mongodb/src/main/java/com/xkcoding/mongodb/SpringBootDemoMongodbApplication.java
@@ -1,8 +1,24 @@
package com.xkcoding.mongodb;
+import cn.hutool.core.lang.Snowflake;
+import cn.hutool.core.util.IdUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+/**
+ *
+ * 启动器
+ *
+ *
+ * @package: com.xkcoding.mongodb
+ * @description: 启动器
+ * @author: yangkai.shen
+ * @date: Created in 2018-12-28 16:14
+ * @copyright: Copyright (c) 2018
+ * @version: V1.0
+ * @modified: yangkai.shen
+ */
@SpringBootApplication
public class SpringBootDemoMongodbApplication {
@@ -10,5 +26,10 @@ public class SpringBootDemoMongodbApplication {
SpringApplication.run(SpringBootDemoMongodbApplication.class, args);
}
+ @Bean
+ public Snowflake snowflake() {
+ return IdUtil.createSnowflake(1, 1);
+ }
+
}
diff --git a/spring-boot-demo-mongodb/src/main/java/com/xkcoding/mongodb/model/Article.java b/spring-boot-demo-mongodb/src/main/java/com/xkcoding/mongodb/model/Article.java
new file mode 100644
index 0000000..0922ce5
--- /dev/null
+++ b/spring-boot-demo-mongodb/src/main/java/com/xkcoding/mongodb/model/Article.java
@@ -0,0 +1,65 @@
+package com.xkcoding.mongodb.model;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.springframework.data.annotation.Id;
+
+import java.util.Date;
+
+/**
+ *
+ * 文章实体类
+ *
+ *
+ * @package: com.xkcoding.mongodb.model
+ * @description: 文章实体类
+ * @author: yangkai.shen
+ * @date: Created in 2018-12-28 16:21
+ * @copyright: Copyright (c) 2018
+ * @version: V1.0
+ * @modified: yangkai.shen
+ */
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class Article {
+ /**
+ * 文章id
+ */
+ @Id
+ private Long id;
+
+ /**
+ * 文章标题
+ */
+ private String title;
+
+ /**
+ * 文章内容
+ */
+ private String content;
+
+ /**
+ * 创建时间
+ */
+ private Date createTime;
+
+ /**
+ * 更新时间
+ */
+ private Date updateTime;
+
+ /**
+ * 点赞数量
+ */
+ private Long thumbUp;
+
+ /**
+ * 访客数量
+ */
+ private Long visits;
+
+}
diff --git a/spring-boot-demo-mongodb/src/main/java/com/xkcoding/mongodb/repository/ArticleRepository.java b/spring-boot-demo-mongodb/src/main/java/com/xkcoding/mongodb/repository/ArticleRepository.java
new file mode 100644
index 0000000..a0ca60c
--- /dev/null
+++ b/spring-boot-demo-mongodb/src/main/java/com/xkcoding/mongodb/repository/ArticleRepository.java
@@ -0,0 +1,29 @@
+package com.xkcoding.mongodb.repository;
+
+import com.xkcoding.mongodb.model.Article;
+import org.springframework.data.mongodb.repository.MongoRepository;
+
+import java.util.List;
+
+/**
+ *
+ * 文章 Dao
+ *
+ *
+ * @package: com.xkcoding.mongodb.repository
+ * @description: 文章 Dao
+ * @author: yangkai.shen
+ * @date: Created in 2018-12-28 16:30
+ * @copyright: Copyright (c) 2018
+ * @version: V1.0
+ * @modified: yangkai.shen
+ */
+public interface ArticleRepository extends MongoRepository {
+ /**
+ * 根据标题模糊查询
+ *
+ * @param title 标题
+ * @return 满足条件的文章列表
+ */
+ List findByTitleLike(String title);
+}
diff --git a/spring-boot-demo-mongodb/src/main/resources/application.yml b/spring-boot-demo-mongodb/src/main/resources/application.yml
new file mode 100644
index 0000000..5e08028
--- /dev/null
+++ b/spring-boot-demo-mongodb/src/main/resources/application.yml
@@ -0,0 +1,9 @@
+spring:
+ data:
+ mongodb:
+ host: localhost
+ port: 27017
+ database: article_db
+logging:
+ level:
+ org.springframework.data.mongodb.core: debug
\ No newline at end of file
diff --git a/spring-boot-demo-mongodb/src/test/java/com/xkcoding/mongodb/repository/ArticleRepositoryTest.java b/spring-boot-demo-mongodb/src/test/java/com/xkcoding/mongodb/repository/ArticleRepositoryTest.java
new file mode 100644
index 0000000..e1c778e
--- /dev/null
+++ b/spring-boot-demo-mongodb/src/test/java/com/xkcoding/mongodb/repository/ArticleRepositoryTest.java
@@ -0,0 +1,156 @@
+package com.xkcoding.mongodb.repository;
+
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.lang.Snowflake;
+import cn.hutool.core.util.RandomUtil;
+import cn.hutool.json.JSONUtil;
+import com.google.common.collect.Lists;
+import com.xkcoding.mongodb.SpringBootDemoMongodbApplicationTests;
+import com.xkcoding.mongodb.model.Article;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.mongodb.core.query.Criteria;
+import org.springframework.data.mongodb.core.query.Query;
+import org.springframework.data.mongodb.core.query.Update;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ *
+ * 测试操作 MongoDb
+ *
+ *
+ * @package: com.xkcoding.mongodb.repository
+ * @description: 测试操作 MongoDb
+ * @author: yangkai.shen
+ * @date: Created in 2018-12-28 16:35
+ * @copyright: Copyright (c) 2018
+ * @version: V1.0
+ * @modified: yangkai.shen
+ */
+@Slf4j
+public class ArticleRepositoryTest extends SpringBootDemoMongodbApplicationTests {
+ @Autowired
+ private ArticleRepository articleRepo;
+
+ @Autowired
+ private MongoTemplate mongoTemplate;
+
+ @Autowired
+ private Snowflake snowflake;
+
+ /**
+ * 测试新增
+ */
+ @Test
+ public void testSave() {
+ Article article = new Article(1L, RandomUtil.randomString(20), RandomUtil.randomString(150), DateUtil.date(), DateUtil
+ .date(), 0L, 0L);
+ articleRepo.save(article);
+ log.info("【article】= {}", JSONUtil.toJsonStr(article));
+ }
+
+ /**
+ * 测试新增列表
+ */
+ @Test
+ public void testSaveList() {
+ List articles = Lists.newArrayList();
+ for (int i = 0; i < 10; i++) {
+ articles.add(new Article(snowflake.nextId(), RandomUtil.randomString(20), RandomUtil.randomString(150), DateUtil
+ .date(), DateUtil.date(), 0L, 0L));
+ }
+ articleRepo.saveAll(articles);
+
+ log.info("【articles】= {}", JSONUtil.toJsonStr(articles.stream()
+ .map(Article::getId)
+ .collect(Collectors.toList())));
+ }
+
+ /**
+ * 测试更新
+ */
+ @Test
+ public void testUpdate() {
+ articleRepo.findById(1L).ifPresent(article -> {
+ article.setTitle(article.getTitle() + "更新之后的标题");
+ article.setUpdateTime(DateUtil.date());
+ articleRepo.save(article);
+ log.info("【article】= {}", JSONUtil.toJsonStr(article));
+ });
+ }
+
+ /**
+ * 测试删除
+ */
+ @Test
+ public void testDelete() {
+ // 根据主键删除
+ articleRepo.deleteById(1L);
+
+ // 全部删除
+ articleRepo.deleteAll();
+ }
+
+ /**
+ * 测试点赞数、访客数,使用save方式更新点赞、访客
+ */
+ @Test
+ public void testThumbUp() {
+ articleRepo.findById(1L).ifPresent(article -> {
+ article.setThumbUp(article.getThumbUp() + 1);
+ article.setVisits(article.getVisits() + 1);
+ articleRepo.save(article);
+ log.info("【标题】= {}【点赞数】= {}【访客数】= {}", article.getTitle(), article.getThumbUp(), article.getVisits());
+ });
+ }
+
+ /**
+ * 测试点赞数、访客数,使用更优雅/高效的方式更新点赞、访客
+ */
+ @Test
+ public void testThumbUp2() {
+ Query query = new Query();
+ query.addCriteria(Criteria.where("_id").is(1L));
+ Update update = new Update();
+ update.inc("thumbUp", 1L);
+ update.inc("visits", 1L);
+ mongoTemplate.updateFirst(query, update, "article");
+
+ articleRepo.findById(1L)
+ .ifPresent(article -> log.info("【标题】= {}【点赞数】= {}【访客数】= {}", article.getTitle(), article.getThumbUp(), article
+ .getVisits()));
+ }
+
+ /**
+ * 测试分页排序查询
+ */
+ @Test
+ public void testQuery() {
+ Sort sort = Sort.by("thumbUp", "updateTime").descending();
+ PageRequest pageRequest = PageRequest.of(0, 5, sort);
+ Page all = articleRepo.findAll(pageRequest);
+ log.info("【总页数】= {}", all.getTotalPages());
+ log.info("【总条数】= {}", all.getTotalElements());
+ log.info("【当前页数据】= {}", JSONUtil.toJsonStr(all.getContent()
+ .stream()
+ .map(article -> "文章标题:" + article.getTitle() + "点赞数:" + article.getThumbUp() + "更新时间:" + article.getUpdateTime())
+ .collect(Collectors.toList())));
+ }
+
+ /**
+ * 测试根据标题模糊查询
+ */
+ @Test
+ public void testFindByTitleLike() {
+ List articles = articleRepo.findByTitleLike("更新");
+ log.info("【articles】= {}", JSONUtil.toJsonStr(articles));
+ }
+
+}
\ No newline at end of file