1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
@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<Article> 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(); }
@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<Article> 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<Article> articles = articleRepo.findByTitleLike("更新"); log.info("【articles】= {}", JSONUtil.toJsonStr(articles)); } }
|