安装
拉取镜像
dokcer pull mogo
创建容器
docker run -di --name mongo-service --restart=always -p 27017:27017 -v ~/data/mongodata:/data mongo
项目集成
导入依赖
| |
| org.springframework.boot |
| spring-boot-starter-data-mongodb |
| |
修改application
| server: |
| port: 9998 |
| spring: |
| data: |
| mongodb: |
| host: 192.168.200.130 |
| port: 27017 |
| database: leadnews-history |
| |
创建实体类
| import lombok.Data; |
| import org.springframework.data.mongodb.core.mapping.Document; |
| |
| import java.io.Serializable; |
| import java.util.Date; |
| |
| |
| |
| |
| |
| |
| |
| @Data |
| @Document("ap_associate_words") |
| public class ApAssociateWords implements Serializable { |
| |
| private static final long serialVersionUID = 1L; |
| |
| private String id; |
| |
| |
| |
| |
| private String associateWords; |
| |
| |
| |
| |
| private Date createdTime; |
| |
| } |
测试类
| import com.itheima.mongo.MongoApplication; |
| import com.itheima.mongo.pojo.ApAssociateWords; |
| 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.data.domain.Sort; |
| 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.test.context.junit4.SpringRunner; |
| |
| import java.util.Date; |
| import java.util.List; |
| |
| @SpringBootTest(classes = MongoApplication.class) |
| @RunWith(SpringRunner.class) |
| public class MongoTest { |
| |
| |
| @Autowired |
| private MongoTemplate mongoTemplate; |
| |
| |
| @Test |
| public void saveTest(){ |
| |
| |
| |
| |
| |
| |
| ApAssociateWords apAssociateWords = new ApAssociateWords(); |
| apAssociateWords.setAssociateWords("黑马直播"); |
| apAssociateWords.setCreatedTime(new Date()); |
| mongoTemplate.save(apAssociateWords); |
| |
| } |
| |
| |
| @Test |
| public void saveFindOne(){ |
| ApAssociateWords apAssociateWords = mongoTemplate.findById("60bd973eb0c1d430a71a7928", ApAssociateWords.class); |
| System.out.println(apAssociateWords); |
| } |
| |
| |
| @Test |
| public void testQuery(){ |
| Query query = Query.query(Criteria.where("associateWords").is("黑马头条")) |
| .with(Sort.by(Sort.Direction.DESC,"createdTime")); |
| List apAssociateWordsList = mongoTemplate.find(query, ApAssociateWords.class); |
| System.out.println(apAssociateWordsList); |
| } |
| |
| @Test |
| public void testDel(){ |
| mongoTemplate.remove(Query.query(Criteria.where("associateWords").is("黑马头条")),ApAssociateWords.class); |
| } |
| } |
saveTest
方法运行之后,
会根据spring.data.mongodb.database
: leadnews-history 建立数据库
根据@Document("ap_associate_words")
作为表名
以及实体类中的id,associateWords,createdTime
作为表的列

核心方法
保存或者修改
mongoTemplate.save(apAssociateWords);
查询一个对象
=
ApAssociateWords apAssociateWords = mongoTemplate.findById("60bd973eb0c1d430a71a7928", ApAssociateWords.class);
多条件查询
| Query query = Query |
| .query(Criteria.where("associateWords") |
| .is("黑马头条")) |
| .with(Sort |
| .by(Sort.Direction.DESC,"createdTime")); |
| List apAssociateWordsList = mongoTemplate.find(query, ApAssociateWords.class); |
删除
| mongoTemplate |
| .remove(Query.query(Criteria.where("associateWords") |
| .is("黑马头条")) |
| ,ApAssociateWords.class); |
MongoRepository
创建评论dto
| import java.io.Serializable; |
| import java.time.LocalDateTime; |
| import java.util.Date; |
| |
| |
| |
| |
| |
| |
| |
| |
| @Document(collection="comment") |
| |
| @CompoundIndex( def = "{'userid': 1, 'nickname': -1}") |
| @Data |
| @AllArgsConstructor |
| @NoArgsConstructor |
| @Accessors(chain = true) |
| public class Comment implements Serializable { |
| |
| @Id |
| private String id; |
| |
| @Field("content") |
| private String content; |
| private Date 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; |
| |
| } |
创建Repository
| import com.example.mongodb2.po.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); |
| } |
调用CommentRepository方法
| import com.example.mongodb2.dto.CommentRepository; |
| import com.example.mongodb2.po.Comment; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.data.domain.Page; |
| import org.springframework.data.domain.PageRequest; |
| import org.springframework.data.domain.Pageable; |
| 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; |
| |
| |
| |
| |
| |
| public void saveComment(Comment comment){ |
| |
| |
| |
| commentRepository.save(comment); |
| } |
| |
| |
| |
| |
| |
| public void updateComment(Comment comment){ |
| |
| commentRepository.save(comment); |
| } |
| |
| |
| |
| |
| |
| public void deleteCommentById(String id){ |
| |
| commentRepository.deleteById(id); |
| } |
| |
| |
| |
| |
| |
| public List findCommentList(){ |
| |
| return commentRepository.findAll(); |
| } |
| |
| |
| |
| |
| |
| |
| public Comment findCommentById(String id){ |
| |
| return commentRepository.findById(id).get(); |
| } |
| |
| |
| public Page findByParentId(String parentId,int page ,int size){ |
| return commentRepository.findByParentid(parentId,PageRequest.of(page-1,size)); |
| } |
| |
| @Autowired |
| private MongoTemplate mongoTemplate; |
| |
| 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); |
| } |
| |
| } |
方法测试
| import com.example.mongodb2.po.Comment; |
| import com.example.mongodb2.service.CommentService; |
| import org.junit.jupiter.api.Test; |
| import org.junit.runner.RunWith; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.boot.test.context.SpringBootTest; |
| import org.springframework.data.domain.Page; |
| import org.springframework.data.mongodb.core.MongoTemplate; |
| import org.springframework.test.context.junit4.SpringRunner; |
| |
| import java.util.List; |
| |
| @RunWith(SpringRunner.class) |
| @SpringBootTest |
| class Mongodb2ApplicationTests { |
| |
| @Autowired |
| private CommentService commentService; |
| |
| @Autowired |
| private MongoTemplate mongoTemplate; |
| |
| @Test |
| void contextLoads() { |
| List commentList = commentService.findCommentList(); |
| System.out.println(commentList); |
| } |
| |
| @Test |
| void getOne() { |
| Comment commentById = commentService.findCommentById("1"); |
| System.out.println(commentById); |
| } |
| |
| @Test |
| void page() { |
| Page page = commentService.findByParentId("3", 1, 2); |
| System.out.println(page.getTotalPages()); |
| System.out.println(page.getContent()); |
| } |
| |
| @Test |
| void updateCommentLikenum() { |
| commentService.updateCommentLikenum("1"); |
| |
| } |
| } |
查询评论列表
| @Test |
| void contextLoads() { |
| List commentList = commentService.findCommentList(); |
| System.out.println(commentList); |
| } |

根据id查询评论信息
| |
| @Test |
| void getOne() { |
| Comment commentById = commentService.findCommentById("1"); |
| System.out.println(commentById); |
| } |

查询分页信息
| @Test |
| void page() { |
| Page page = commentService.findByParentId("3", 1, 2); |
| System.out.println(page.getTotalPages()); |
| System.out.println(page.getContent()); |
| } |

为指定id的评论添加点赞
| @Test |
| void updateCommentLikenum() { |
| commentService.updateCommentLikenum("1"); |
| } |
方法执行前

方法执行后
