SpringBoot-MongoDB整合版V1.0.0
- 项目整合流程为:mongodb->dao->service->单元测试Test
1.创建springboot项目
1.1创建项目如下图
1.2引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.douglas</groupId>
<artifactId>springboot_mongodb</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
</project>
1.3创建application.yml
spring:
data:
mongodb:
host: 101.43.89.105
database: douglas
port: 27017
1.4创建启动类
package com.douglas.mymongo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyMongoApplication {
public static void main(String[] args) {
SpringApplication.run(MyMongoApplication.class,args);
}
}
1.5启动项目
- 启动项目检查控制台是否有错,能否正常启动
- 检查mongodb是否打开连接成功
2.文章评论案例实现CRUD
2.1创建评论实体类
package com.douglas.mymongo.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 java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
@Document(collection = "comment")
public class Comment implements Serializable {
@Id
private String id;
@Field("content")
private String content;
@Indexed
private String userid;
private Date publishtime;
private String nickname;
private LocalDateTime createdatatime;
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 String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public Date getPublishtime() {
return publishtime;
}
public void setPublishtime(Date publishtime) {
this.publishtime = publishtime;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public LocalDateTime getCreatedatatime() {
return createdatatime;
}
public void setCreatedatatime(LocalDateTime createdatatime) {
this.createdatatime = createdatatime;
}
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;
}
}
2.2创建评论DAO
package com.douglas.mymongo.dao;
import com.douglas.mymongo.po.Comment;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface CommentRepository extends MongoRepository<Comment,String> {
}
- 【注意】
- java的Dao必须继承MongoRepository接口
- 设置泛型Comment就是java对应mongodb的实体对象
- 设置泛型String就是mongodb中document的主键id类型
- 基本的CRUD不用自己重写,直接调用就可以
2.3创建评论Service
package com.douglas.mymongo.service;
import com.douglas.mymongo.po.Comment;
import java.util.List;
public interface CommentService {
void saveComment(Comment comment);
void updateComment(Comment comment);
void deleteComment(String id);
List<Comment> findComment();
Comment findCommentById(String id);
}
package com.douglas.mymongo.service.impl;
import com.douglas.mymongo.dao.CommentRepository;
import com.douglas.mymongo.po.Comment;
import com.douglas.mymongo.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class CommentServiceImpl implements CommentService {
@Autowired
private CommentRepository commentRepository;
@Override
public void saveComment(Comment comment){
commentRepository.save(comment);
}
@Override
public void updateComment(Comment comment) {
commentRepository.save(comment);
}
@Override
public void deleteComment(String id) {
commentRepository.deleteById(id);
}
@Override
public List<Comment> findComment() {
return commentRepository.findAll();
}
@Override
public Comment findCommentById(String id) {
Optional<Comment> comment = commentRepository.findById(id);
return comment.get();
}
}
2.4创建单元测试
package com.douglas.mymongo.service;
import com.douglas.mymongo.po.Comment;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CommentServiceTest {
@Autowired
private CommentService commentService;
@Test
public void saveComment(){
commentService.saveComment(Comment.builder().articleid("1001")
.content("你是最棒的,加油少年")
.createdatatime(LocalDateTime.now())
.id("1")
.likenum(11)
.nickname("LS")
.parentid("1")
.publishtime(new Date())
.replynum(12)
.state("有效")
.userid("dgc").build()
);
}
@Test
public void updateComment(){
commentService.updateComment(Comment.builder()
.articleid("1002")
.content("你是最棒的,加油少年")
.createdatatime(LocalDateTime.now())
.id("1")
.likenum(88)
.nickname("LS")
.parentid("1")
.publishtime(new Date())
.replynum(99)
.state("有效")
.userid("516300284106").build());
}
@Test
public void deleteComment(){
commentService.deleteComment("1");
}
@Test
public void findComment(){
List<Comment> comment = commentService.findComment();
comment.forEach(s->{
System.out.println(s.toString());
});
}
@Test
public void findCommentById(){
System.out.println(commentService.findCommentById("1"));
}
}
3.整合项目框架