当前位置:   article > 正文

Mongodb---java篇

Mongodb---java篇

一、导入依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-mongodb</artifactId>
  4. </dependency>

二、编写配置文件连接Mongodb

我的认证数据库是admin,你们可能不一样

  1. spring:
  2. data:
  3. mongodb:
  4. host: 192.168.110.223
  5. port: 27017
  6. username: root
  7. #password: 123456
  8. # 将纯数字密码加字符串
  9. password: smart
  10. database: articledb
  11. authentication-database: admin

三、编写实体

  1. @Data
  2. //复合索引
  3. @CompoundIndex( def = "{'userid': 1, 'nickname': -1}")
  4. @Document("comment")
  5. public class Comment implements Serializable {
  6. @Id
  7. private String id;
  8. //吐槽内容
  9. private String content;
  10. //发布日期
  11. private Date publishtime;
  12. @Indexed
  13. private String userid;
  14. //昵称
  15. private String nickname;
  16. //评论的日期时间
  17. private LocalDateTime createdatetime;
  18. //点赞数
  19. private Integer likenum;
  20. //回复数
  21. private Integer replynum;
  22. //状态
  23. private String state;
  24. //上级ID
  25. private String parentid;
  26. private String articleid;

四、操作数据

        可以是spring的模板方法进行crud操作,也可以自己设置Repository层进行操作,方法名和JPA有点相似。Repository默认的方法比价少,需要自己的写方法,用模板方法就要多一些

  1. @Repository
  2. public interface CommentRepository extends MongoRepository<Comment,String> {
  3. Page<Comment> findByParentid(String parentid, PageRequest of);
  4. }
  1. package net.royal.student.service;
  2. import net.royal.student.dao.CommentRepository;
  3. import net.royal.student.entity.Comment;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.domain.Page;
  6. import org.springframework.data.domain.PageRequest;
  7. import org.springframework.data.mongodb.core.MongoTemplate;
  8. import org.springframework.data.mongodb.core.query.Criteria;
  9. import org.springframework.data.mongodb.core.query.Query;
  10. import org.springframework.data.mongodb.core.query.Update;
  11. import org.springframework.stereotype.Service;
  12. import java.util.List;
  13. /**
  14. * @author Royal
  15. */
  16. @Service
  17. public class CommentService {
  18. @Autowired
  19. private CommentRepository commentRepository;
  20. //注入MongoTemplate
  21. @Autowired
  22. private MongoTemplate mongoTemplate;
  23. /**
  24. * 保存一个评论
  25. */
  26. public void saveComment(Comment comment) {
  27. commentRepository.save(comment);
  28. }
  29. /*
  30. 更新一个评论
  31. */
  32. public void updateComment(Comment comment) {
  33. commentRepository.save(comment);
  34. }
  35. /**
  36. * 根据id删除评论
  37. */
  38. public void deleteCommentById(String id) {
  39. commentRepository.deleteById(id);
  40. }
  41. /**
  42. * 查询所有评论
  43. *
  44. * @return
  45. */
  46. public List<Comment> findCommentList() {
  47. //调用dao
  48. return commentRepository.findAll();
  49. }
  50. /**
  51. * 根据id查询评论
  52. *
  53. * @param id
  54. * @return
  55. */
  56. public Comment findCommentById(String id) {
  57. //调用dao
  58. return commentRepository.findById(id).get();
  59. }
  60. /**
  61. * 根据父id查询分页列表
  62. * @param parentid
  63. * @param page
  64. * @param size
  65. * @return
  66. */
  67. public Page<Comment> findCommentListPageParentId(String parentid, int page ,int size){
  68. return commentRepository.findByParentid(parentid, PageRequest.of(page-1,size));
  69. }
  70. /**
  71. * 点赞-效率低
  72. */
  73. public void updateCommentThumbupToIncrementingOld(String id) {
  74. Comment comment = commentRepository.findById(id).get();
  75. comment.setLikenum(comment.getLikenum()+1);
  76. commentRepository.save(comment);
  77. }
  78. /**
  79. * 点赞数+1
  80. */
  81. public void updateCommentLikenum(String id){
  82. //查询对象
  83. Query query = Query.query(Criteria.where("_id").is(id));
  84. //更新对象
  85. Update update = new Update();
  86. update.inc("likenum");
  87. mongoTemplate.updateFirst(query, update, "comment");
  88. }
  89. }

测试我这里就不演示了,都比较简单。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/725269
推荐阅读
相关标签
  

闽ICP备14008679号