JavaAPI操作MongoDB--基本增刪改查

小劉新鮮事兒發表於2020-11-12

在這裡插入圖片描述

1.文章評論實體類

Comment.java

package csdn.xiaoliuxxs.article.po;

import org.springframework.data.annotation.Id;
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 javax.xml.crypto.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
 * 文章評論實體類
 */
@Document(collection = "comment")//可以省略,省略則預設使用類名小寫對映集合
public class Comment implements Serializable {
    @Id
    private String id;
    @Field("content")
    private String content;
    private Data publishtime;
    @Indexed
    private String userid;
    private String nickname;
    private LocalDateTime createdatetime;
    private Integer likenum;
    private Integer replynum;
    private String state;
    private String parentid;
    private String articleid;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Data getPublishtime() {
        return publishtime;
    }

    public void setPublishtime(Data publishtime) {
        this.publishtime = publishtime;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public LocalDateTime getCreatedatetime() {
        return createdatetime;
    }

    public void setCreatedatetime(LocalDateTime createdatetime) {
        this.createdatetime = createdatetime;
    }

    public Integer getLikenum() {
        return likenum;
    }

    public void setLikenum(Integer likenum) {
        this.likenum = likenum;
    }

    public Integer getReplynum() {
        return replynum;
    }

    public void setReplynum(Integer replynum) {
        this.replynum = replynum;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getParentid() {
        return parentid;
    }

    public void setParentid(String parentid) {
        this.parentid = parentid;
    }

    public String getArticleid() {
        return articleid;
    }

    public void setArticleid(String articleid) {
        this.articleid = articleid;
    }

    @Override
    public String toString() {
        return "Comment{" +
                "id='" + id + '\'' +
                ", content='" + content + '\'' +
                ", publishtime=" + publishtime +
                ", userid='" + userid + '\'' +
                ", nickname='" + nickname + '\'' +
                ", createdatetime=" + createdatetime +
                ", likenum=" + likenum +
                ", replynum=" + replynum +
                ", state='" + state + '\'' +
                ", parentid='" + parentid + '\'' +
                ", articleid='" + articleid + '\'' +
                '}';
    }
}

2.建立資料訪問介面

CommentRepository.java

package csdn.xiaoliuxxs.article.dao;

import csdn.xiaoliuxxs.article.po.Comment;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface CommentRepository extends MongoRepository<Comment,String> {
}

3.建立業務邏輯類

CommentService.java

package csdn.xiaoliuxxs.article.service;

import csdn.xiaoliuxxs.article.dao.CommentRepository;
import csdn.xiaoliuxxs.article.po.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {

    //注入dao
    @Autowired
    private 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();
    }
}

4.建立測試類
CommentServiceTest.java

package csdn.xiaoliuxxs.article.service;


import csdn.xiaoliuxxs.article.po.Comment;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDateTime;
import java.util.List;


@RunWith(SpringRunner.class)
@SpringBootTest
public class CommentServiceTest {

    @Autowired
    private CommentService commentService;


    //查詢所有comment記錄
    @Test
    public void testFindCommentList(){
        List<Comment> commentList = commentService.findCommentList();
        for (Comment comment : commentList) {
            System.out.println(comment);
        }
    }

在這裡插入圖片描述

    //查詢id為1的記錄
    @Test
    public void testFindCommentById(){
        Comment commentById = commentService.findCommentById("1");
        System.out.println(commentById);
    }

在這裡插入圖片描述

    //插入一條評論
    //如果不指定id,會自動生成一個ObjectID
    @Test
    public void testSaveComment(){
        Comment comment = new Comment();
        comment.setArticleid("100000");
        comment.setContent("你若三冬");
        comment.setCreatedatetime(LocalDateTime.now());
        comment.setUserid("1003");
        comment.setNickname("阿悠悠");
        comment.setState("1");
        comment.setLikenum(0);
        comment.setReplynum(0);
        commentService.saveComment(comment);
    }
}

在這裡插入圖片描述

相關文章