第四章节 SpringBoot整合MongoDB

第四章节 SpringBoot整合MongoDB,第1张

第四章节 SpringBoot整合MongoDB

关于整合MongoDB的入门文档

前言1.引入pom文件2.配置连接地址3.编写对应的实体类4.编写dao类5.编写service类总结

前言

为了在项目中使用MongoDB,本文记录了整合MongoDB的过程。

1.引入pom文件
		
			org.springframework.boot
			spring-boot-starter-data-mongodb
		
2.配置连接地址
spring:
  data:
    mongodb:
      uri: mongodb://localhost:27017/articledb
3.编写对应的实体类
package com.worsoft.worsoft.db.entity;

import lombok.Data;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;


//把一个java类声明为mongodb的文档,可以通过collection参数指定这个类对应的文档。
//@document(collection="mongodb 对应 collection 名")
// 若未加 @document ,该 bean save 到 mongo 的 comment collection
// 若添加 @document ,则 save 到 comment collection
@document(collection="comment")//可以省略,如果省略,则默认使用类名小写映射集合
//复合索引
@CompoundIndex( def = "{'userid': 1, 'nickname': -1}")
@Data
public class Comment implements Serializable {
    //主键标识,该属性的值会自动对应mongodb的主键字段"_id",如果该属性名就叫“id”,则该注解可以省略,否则必须写
//    @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;

}
4.编写dao类
package com.worsoft.worsoft.db.mapper;

import com.worsoft.worsoft.db.entity.Comment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface CommentRepository extends MongoRepository {


    Page findByParentid(String parentid,Pageable pageable);

}
5.编写service类
package com.worsoft.worsoft.db.service;

import com.worsoft.worsoft.db.entity.Comment;
import com.worsoft.worsoft.db.mapper.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {

    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private MongoTemplate mongoTemplate;

    
    public void saveComment(Comment comment){
        //如果需要自定义主键,可以在这里指定主键;如果不指定主键,MongoDB会自动生成主键
        //设置一些默认初始值。。。
        //调用dao
        commentRepository.save(comment);
    }

    
    public void updateComment(Comment comment){
        //调用dao
        commentRepository.save(comment);
    }

    
    public void deleteCommentById(String id){
        //调用dao
        commentRepository.deleteById(id);
    }

    
    public List findCommentList(){
        //调用dao
        return commentRepository.findAll();
    }

    
    public Comment findCommentById(String id){
        //调用dao
        return commentRepository.findById(id).get();
    }


    public Page findCommentListByParentid(String parentid,int page,int size) {
        return commentRepository.findByParentid(parentid,PageRequest.of(page-1,size));
    }

    public void updateCommentLikenum(String id){
        //  查询条件
        Query query = Query.query(Criteria.where("_id").is(id));
        //  更新条件
        Update update = new Update();
        update.inc("likenum");
        mongoTemplate.updateFirst(query,update,Comment.class);
    }

}
总结

本文记录了SpringBoot整合MongoDB后的使用方法,一个愉快的周末,又学到了一个知识点。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5709811.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存