From 1193841279a820ac8d3bd9f6613b4abc150f89c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Sat, 15 Jul 2023 11:48:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=96=B0=E5=A2=9E=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../itcast/article/dao/CommentRepository.java | 46 ++++++++++++++-- .../article/service/CommentService.java | 17 ++++-- .../article/service/CommentServiceTest.java | 52 +++++++++++++++++-- 3 files changed, 104 insertions(+), 11 deletions(-) diff --git a/src/main/java/cn/itcast/article/dao/CommentRepository.java b/src/main/java/cn/itcast/article/dao/CommentRepository.java index 650d265..0111584 100644 --- a/src/main/java/cn/itcast/article/dao/CommentRepository.java +++ b/src/main/java/cn/itcast/article/dao/CommentRepository.java @@ -4,10 +4,50 @@ import cn.itcast.article.po.Comment; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; -public interface CommentRepository extends MongoRepository { +import java.util.List; +public interface CommentRepository extends MongoRepository { + /** + * findByUserid的Userid一定要和参数一致 + * + * @param userid + * @param pageable + * @return + */ + Page findByUserid(String userid, Pageable pageable); - Page findByParentid(String parentid,Pageable pageable); + /** + * 多字段查询 + * + * @param userid + * @param likenum + * @param pageable + * @return + */ + Page findByUseridAndLikenum(String userid, Integer likenum, Pageable pageable); -} + /** + * 查询userid固定值,并且likenum大于等于的文档 + * + * @param userid + * @param likenum + * @param pageable + * @return + */ + Page findByUseridContainingAndLikenumGreaterThanEqual(String userid, Integer likenum, Pageable pageable); + + + /** + * 正则查询 + * 使用了@Query注解来指定查询语句。在查询语句中, + * 使用了$regex操作符表示name匹配正则表达式,使用了$options选项表示不区分大小写,使用了$gte操作符表示age大于等于指定值。 + * + * @param userid + * @param likenum + * @return + */ + @Query("{ 'userid' : { $regex: ?0, $options: 'i' }, 'likenum' : { $gte: ?1 } }") + List findByUseridAndLikenum(String userid, Integer likenum); +} \ No newline at end of file diff --git a/src/main/java/cn/itcast/article/service/CommentService.java b/src/main/java/cn/itcast/article/service/CommentService.java index d484570..f170bd7 100644 --- a/src/main/java/cn/itcast/article/service/CommentService.java +++ b/src/main/java/cn/itcast/article/service/CommentService.java @@ -15,7 +15,6 @@ import java.util.List; @Service public class CommentService { - @Autowired private MongoTemplate mongoTemplate; @Autowired @@ -74,8 +73,8 @@ public class CommentService { return commentRepository.findById(id).get(); } - public Page findCommentListByParentid(String parentid, int page, int size) { - return commentRepository.findByParentid(parentid, PageRequest.of(page - 1, size)); + public Page findByUserid(String userid, int page, int size) { + return commentRepository.findByUserid(userid, PageRequest.of(page - 1, size)); } public void updateCommentLikenum(String id) { @@ -86,4 +85,16 @@ public class CommentService { update.inc("likenum"); mongoTemplate.updateFirst(query, update, Comment.class); } + + public Page findByUseridAndLikenum(String userid, int likenum, int page, int size) { + return commentRepository.findByUseridAndLikenum(userid, likenum, PageRequest.of(page - 1, size)); + } + + public Page findByUseridContainingAndLikenumGreaterThanEqual(String userid, int likenum, int page, int size) { + return commentRepository.findByUseridContainingAndLikenumGreaterThanEqual(userid, likenum, PageRequest.of(page - 1, size)); + } + + public List findByUseridAndLikenum(String userid, int likenum) { + return commentRepository.findByUseridAndLikenum(userid, likenum); + } } \ No newline at end of file diff --git a/src/test/java/cn/itcast/article/service/CommentServiceTest.java b/src/test/java/cn/itcast/article/service/CommentServiceTest.java index d13dd93..ddcbd35 100644 --- a/src/test/java/cn/itcast/article/service/CommentServiceTest.java +++ b/src/test/java/cn/itcast/article/service/CommentServiceTest.java @@ -29,12 +29,18 @@ public class CommentServiceTest { log.info(JSON.toJSONString(commentList)); } + /** + * 根据id查询 + */ @Test public void testFindCommentById() { Comment commentById = commentService.findCommentById("1"); - System.out.println(commentById); + log.info(JSON.toJSONString(commentById)); } + /** + * 新增评论 + */ @Test public void testSaveComment() { Comment comment = new Comment(); @@ -49,13 +55,49 @@ public class CommentServiceTest { commentService.saveComment(comment); } + /** + * 根据parentid分页查询 + */ + @Test + public void testFindCommentListByUserid() { + Page page = commentService.findByUserid("1003", 1, 10); + log.info(JSON.toJSONString(page.getTotalElements())); + log.info(JSON.toJSONString(page.getContent())); + } + + /** + * 多字段查询 + */ @Test - public void testFindCommentListByParentid() { - Page page = commentService.findCommentListByParentid("3", 1, 2); - System.out.println(page.getTotalElements()); - System.out.println(page.getContent()); + public void testFindCommentListByUseridAndLikenum() { + Page page = commentService.findByUseridAndLikenum("1003", 3000, 1, 2); + log.info(JSON.toJSONString(page.getTotalElements())); + log.info(JSON.toJSONString(page.getContent())); } + /** + * 分页多字段查询,并且 + * 查询userid固定值,并且likenum大于等于的文档 + */ + @Test + public void testFindByUseridContainingAndLikenumGreaterThanEqual() { + Page page = commentService.findByUseridContainingAndLikenumGreaterThanEqual("1003", 100, 1, 2); + log.info(JSON.toJSONString(page.getTotalElements())); + log.info(JSON.toJSONString(page.getContent())); + } + + /** + * 正则查询 + */ + @Test + public void testFindByUseridAndLikenum() { + List list = commentService.findByUseridAndLikenum("1003", 100); + log.info(JSON.toJSONString(list)); + } + + /** + * 修改点赞数加1 + */ @Test public void testUpdateCommentLikenum() { commentService.updateCommentLikenum("1"); -- GitLab