码神之路博客优化之评论放入mongdb数据库

**

码神之路博客优化之评论放入mongdb数据库

**

先上配置文件,这里设置了mongdb数据库,但是插入了时候自动插入到了test数据库中。

server.port=8888
spring.application.name=ahu_blog
# datasource
spring.datasource.url=jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring:
  data:
    mongodb:
        host: localhost
        port: 27017
        database: blog


#mybatis-plus
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.global-config.db-config.table-prefix=ms_
#??mapper xml?????
mybatis-plus.mapper-locations=classpath:com/ahu/blog/dao/mapper/xml/*.xml
spring.redis.host=localhost
spring.redis.port=6379

自己加了一个commentDb实体类

package com.ahu.blog.dao.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.SuperBuilder;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.MongoId;

import java.io.Serializable;
import java.lang.annotation.Documented;

@Document("commentDb")
@SuperBuilder(toBuilder = true)
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Data
public class CommentDb implements Serializable {


    private Long cid;

    private String content;

    private Long createDate;

    private Long articleId;

    private Long authorId;

    private Long parentId;

    private Long toUid;

    private Integer level;
}

mongdb接口

package com.ahu.blog.repository;

import com.ahu.blog.dao.pojo.CommentDb;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

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

service层代码,主要是在这里对mongdb进行操作

package com.ahu.blog.service.impl;
import com.ahu.blog.config.UserThreadLocal;
import com.ahu.blog.dao.mapper.CommentMapper;
import com.ahu.blog.dao.pojo.Comment;
import com.ahu.blog.dao.pojo.CommentDb;
import com.ahu.blog.dao.pojo.SysUser;
import com.ahu.blog.repository.CommentRepository;
import com.ahu.blog.service.CommentService;
import com.ahu.blog.service.SysUserService;
import com.ahu.blog.utils.SnowflakeUtil;
import com.ahu.blog.vo.CommentVo;
import com.ahu.blog.vo.UserVo;
import com.ahu.blog.vo.params.CommentParams;
import com.ahu.blog.vo.params.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public Result findCommentsById(Long id){
        Query query =new Query(Criteria.where("articleId").is(id).and("level").is(1));
        List<CommentDb> commentDbList = mongoTemplate.find(query, CommentDb.class);
        List<CommentVo> commentVos = copyListDb(commentDbList);
        return Result.success(commentVos);

    }

    private List<CommentVo> copyListDb(List<CommentDb> commentDbList) {
        List<CommentVo> commentVoList = new ArrayList<>();
        for (CommentDb comment:commentDbList){
            commentVoList.add(copyDb(comment));
        }
        return commentVoList;
    }

    private CommentVo copyDb(CommentDb comment) {
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment,commentVo);
        commentVo.setId(comment.getCid().toString());
        commentVo.setCreateDate(new DateTime(comment.getCreateDate()).toString("yyyy-MM-dd HH:mm"));
        UserVo userVo = sysUserService.findUserById(comment.getAuthorId());
        commentVo.setAuthor(userVo);
        Integer level = comment.getLevel();
        if (level==1){
            Long id = comment.getCid();
            List<CommentVo> commentVoList = findCommentsByParentIdDb(id);
            commentVo.setChildrens(commentVoList);
        }
        if (level>1){
            Long toUid = comment.getToUid();
            UserVo user = sysUserService.findUserById(toUid);
            commentVo.setToUser(user);
        }
        return commentVo;
    }

    @Override
    public Result comment(CommentParams commentParams) {
        SysUser sysUser = UserThreadLocal.get();
        CommentDb comment = new CommentDb();

        comment.setCid(SnowflakeUtil.getId());
        comment.setContent(commentParams.getContent());
        comment.setArticleId(commentParams.getArticleId());
        comment.setCreateDate(System.currentTimeMillis());
        comment.setAuthorId(sysUser.getId());
        Long parent = commentParams.getParent();
        if (parent==null || parent==0){
            comment.setLevel(1);
        }else {
            comment.setLevel(2);
        }
        comment.setParentId(parent==null? 0:parent);
        Long touid = commentParams.getToUserId();
        comment.setToUid(touid==null? 0:touid);
        commentRepository.insert(comment);
        return Result.success(comment);

    }


    private List<CommentVo> findCommentsByParentIdDb(Long id) {
        Query query =new Query(Criteria.where("parentId").is(id).and("level").is(2));
        List<CommentDb> commentDbList = mongoTemplate.find(query, CommentDb.class);





}

插入结果如图,并且页面展示也是正常的

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值