JavaAPI操作MongoDB--基本增刪改查
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);
}
}
相關文章
- jquery基本操作增刪改查有哪些?jQuery
- mysql基本增刪改查MySql
- mongodb 基本增刪改查MongoDB
- MongoDB增刪改查操作MongoDB
- PHP操作MongoDB(增刪改查)PHPMongoDB
- 基本 SQL 之增刪改查(二)SQL
- Elasticsearch使用系列-ES增刪查改基本操作+ik分詞Elasticsearch分詞
- mysql資料增刪改查操作MySql
- MySQL基礎操作(增刪改查)MySql
- JS字串操作之增刪改查JS字串
- MongoDB基本增刪改查操作-基於Node.JS驅動MongoDBNode.js
- 基本的資料庫增刪改查資料庫
- MySQL的基本語法(增,刪,改,查)MySql
- 資料庫操作增刪改查模糊查資料庫
- MyBatis框架搭建及增刪改查操作MyBatis框架
- linux-MySQL基本指令-增刪改查LinuxMySql
- mysql 資料增刪改查基本語句MySql
- 增刪改查
- 用thinkphp進行增刪改查的操作PHP
- 使用mybatis開發的增刪改查操作MyBatis
- iOS操作屬性列表plist(增刪改查)iOS
- indexedDB 增刪改查Index
- SQL增刪改查SQL
- mysql增刪改查MySql
- Mongoose查增改刪Go
- FMDB增刪改查
- mysql增查刪改MySql
- Java實現簡單的增刪改查操作Java
- YII1 增、刪、改、查資料庫操作資料庫
- imutable.js常用增刪改查操作說明JS
- 安卓開發SQLite增刪改查操作例項安卓SQLite
- 表的建立修改及增刪改查-DML操作
- Python操作SQLServer資料庫增刪改查PythonSQLServer資料庫
- SQLAlchemy - 模組檔案以及增刪改查(CURD操作)SQL
- layui的增刪改查UI
- sql指令,增,刪,查,改SQL
- EFCore之增刪改查
- 列表的增刪改查