MongoDB——实战演练

MongoDB实战演练

在上一篇章的学习中,我们已经学会了Mongo的常用指令,这一篇我们结合一个小案例,看看Java中如何使用Mongo

案例描述

我们所需要做的是一个文章评论的后台,具体涉及以下几个功能

  • 评论的增删改查
  • 根据父id分页查找
  • 评论点赞数的累加

引入依赖

spring data框架中整合了mongo的使用,我们可以直接引入springdata的依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

配置文件

类同于MySQL,我们也需要创建配置文件,进行Mongo数据库的相关配置

spring:
  data:
    mongodb:
      host: 121.40.45.37
      database: articledb
      port: 27017
      username: yellowstar
      password: 123321

文章评论实体类的编写

对于Java来说,万物皆对象,在使用MySQL时,我们通常都会创建一个对应数据库表的实体类,Mongo也不例外

@Document("comment")
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Comment implements Serializable {
    @Id
    private String id;//主键
    //该属性对应mongodb的字段的名字,如果一致,则无需该注解
    @Field("content")
    private String content;//吐槽内容
    private Date publishtime;//发布日期
    //添加了一个单字段的索引
    @Indexed
    private String userid;//发布人ID
    private String nickname;//昵称
    private LocalDateTime createdatetime;//评论的日期时间
    private Integer likenum;//点赞数
    private Integer replynum;//回复数
    private String state;//状态
    private String parentid;//上级ID
    private String articleid;
}

实现评论的CRUD功能

编写持久层
public interface CommentRepository extends MongoRepository<Comment,String> {
    
}
编写业务层
@Service
public class CommentService {
    @Autowired
    CommentRepository commentRepository;

    //保存一条评论
    public void saveComment(Comment comment){
        commentRepository.save(comment);
    }

    //更新一条评论
    public void updateComment(Comment comment){
        commentRepository.save(comment);
    }

    //根据id删除评论
    public void deleteCommentById(String id){
        commentRepository.deleteById(id);
    }

    //查询所有评论
    public List<Comment> findCommentList(){
        return commentRepository.findAll();
    }

    //根据id查询评论
    public Comment findCommentById(String id){
        return commentRepository.findById(id).get();
    }
}

根据父ID分页查找

修改持久层
public interface CommentRepository extends MongoRepository<Comment,String> {
    Page<Comment> findCommentByParentid(String parentid, Pageable pageable);
}
业务层
//根据父ID查询分页列表
public Page<Comment> findCommentByParentid(String parentid, int page, int size){
    return commentRepository.findCommentByParentid(parentid, PageRequest.of(page-1,size));
}

评论点赞数累积

评论点赞累计如果使用查找再更新的操作的话,会查找出一些我们实际并不需要的数据,这会导致性能下降,

在常用命令中,inc方法可以实现累加的效果,在springdata中,可以使用MongoTemplate类来实现

修改业务层
@Autowired
MongoTemplate mongoTemplate;

public void updateCommentLikenum(String id){
    Query query = Query.query(Criteria.where("_id").is(id));
    Update update = new Update();
    update.inc("likenum",1);
    mongoTemplate.updateFirst(query,update,Comment.class);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值