fix:新增数据

上级 2917f230
......@@ -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<Comment,String > {
import java.util.List;
public interface CommentRepository extends MongoRepository<Comment, String> {
/**
* findByUserid的Userid一定要和参数一致
*
* @param userid
* @param pageable
* @return
*/
Page<Comment> findByUserid(String userid, Pageable pageable);
Page<Comment> findByParentid(String parentid,Pageable pageable);
/**
* 多字段查询
*
* @param userid
* @param likenum
* @param pageable
* @return
*/
Page<Comment> findByUseridAndLikenum(String userid, Integer likenum, Pageable pageable);
}
/**
* 查询userid固定值,并且likenum大于等于的文档
*
* @param userid
* @param likenum
* @param pageable
* @return
*/
Page<Comment> 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<Comment> findByUseridAndLikenum(String userid, Integer likenum);
}
\ No newline at end of file
......@@ -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<Comment> findCommentListByParentid(String parentid, int page, int size) {
return commentRepository.findByParentid(parentid, PageRequest.of(page - 1, size));
public Page<Comment> 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<Comment> findByUseridAndLikenum(String userid, int likenum, int page, int size) {
return commentRepository.findByUseridAndLikenum(userid, likenum, PageRequest.of(page - 1, size));
}
public Page<Comment> findByUseridContainingAndLikenumGreaterThanEqual(String userid, int likenum, int page, int size) {
return commentRepository.findByUseridContainingAndLikenumGreaterThanEqual(userid, likenum, PageRequest.of(page - 1, size));
}
public List<Comment> findByUseridAndLikenum(String userid, int likenum) {
return commentRepository.findByUseridAndLikenum(userid, likenum);
}
}
\ No newline at end of file
......@@ -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<Comment> page = commentService.findByUserid("1003", 1, 10);
log.info(JSON.toJSONString(page.getTotalElements()));
log.info(JSON.toJSONString(page.getContent()));
}
/**
* 多字段查询
*/
@Test
public void testFindCommentListByParentid() {
Page<Comment> page = commentService.findCommentListByParentid("3", 1, 2);
System.out.println(page.getTotalElements());
System.out.println(page.getContent());
public void testFindCommentListByUseridAndLikenum() {
Page<Comment> 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<Comment> page = commentService.findByUseridContainingAndLikenumGreaterThanEqual("1003", 100, 1, 2);
log.info(JSON.toJSONString(page.getTotalElements()));
log.info(JSON.toJSONString(page.getContent()));
}
/**
* 正则查询
*/
@Test
public void testFindByUseridAndLikenum() {
List<Comment> list = commentService.findByUseridAndLikenum("1003", 100);
log.info(JSON.toJSONString(list));
}
/**
* 修改点赞数加1
*/
@Test
public void testUpdateCommentLikenum() {
commentService.updateCommentLikenum("1");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册