Mongodb---java篇

发布于:2024-06-11 ⋅ 阅读:(34) ⋅ 点赞:(0)

一、导入依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

二、编写配置文件连接Mongodb

我的认证数据库是admin,你们可能不一样

spring:
  data:
    mongodb:
      host: 192.168.110.223
      port: 27017
      username: root
      #password: 123456
      # 将纯数字密码加字符串
      password: smart
      database: articledb
      authentication-database: admin

三、编写实体

@Data
//复合索引
@CompoundIndex( def = "{'userid': 1, 'nickname': -1}")
@Document("comment")
public class Comment implements Serializable {
    @Id
    private String id;

    //吐槽内容
    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;
    //上级ID
    private String parentid;
    private String articleid;

四、操作数据

        可以是spring的模板方法进行crud操作,也可以自己设置Repository层进行操作,方法名和JPA有点相似。Repository默认的方法比价少,需要自己的写方法,用模板方法就要多一些

@Repository
public interface CommentRepository  extends MongoRepository<Comment,String> {

    Page<Comment> findByParentid(String parentid, PageRequest of);
    
}
package net.royal.student.service;

import net.royal.student.dao.CommentRepository;
import net.royal.student.entity.Comment;
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;

/**
 * @author Royal
 */
@Service
public class CommentService {

    @Autowired
    private CommentRepository commentRepository;


    //注入MongoTemplate
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存一个评论
     */
    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);
    }

    /**
     * 查询所有评论
     *
     * @return
     */
    public List<Comment> findCommentList() {
//调用dao
        return commentRepository.findAll();
    }

    /**
     * 根据id查询评论
     *
     * @param id
     * @return
     */
    public Comment findCommentById(String id) {
//调用dao
        return commentRepository.findById(id).get();
    }
    /**
     * 根据父id查询分页列表
     * @param parentid
     * @param page
     * @param size
     * @return
     */
    public Page<Comment> findCommentListPageParentId(String parentid, int page ,int size){
        return commentRepository.findByParentid(parentid, PageRequest.of(page-1,size));
    }
    /**
     * 点赞-效率低
     */
    public void updateCommentThumbupToIncrementingOld(String id) {
        Comment comment = commentRepository.findById(id).get();
        comment.setLikenum(comment.getLikenum()+1);
        commentRepository.save(comment);
    }

    /**
     * 点赞数+1
     */
    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");
    }
}

测试我这里就不演示了,都比较简单。


网站公告

今日签到

点亮在社区的每一天
去签到