简单的评论功能是指能够在文章底下进行评论,而且能够对评论进行回复,目前实现的功能如下所示,很low,而且这个时候就遇到不会写前端的问题了
我将设计评论功能分为一下几步:
- 首先需要设计表结构
- 然后设计html
- 然后实现相应的功能
1. 评论表的数据库设计
我将评论定义为两个层级:
- 第一个层级是对博客的评论
- 第二个层级是对评论的回复
因此设计了两个表
第一个表叫ArticleComments
CREATE TABLE `ArticleComments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`commentUserId` int(11) NOT NULL,
`articleId` int(11) NOT NULL,
`content` varchar(400) NOT NULL,
`createTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=100000 DEFAULT CHARSET=utf8
这个表依赖的articleId,如果需要显示文章,我们就可以通过articleId得到该博客所有的评论
第二个表叫ReplyComment
CREATE TABLE `ReplyComment` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`articleId` int(11) NOT NULL,
`commentId` int(11) NOT NULL,
`replyUserId` int(11) NOT NULL,
`repliedUserId` int(11) NOT NULL,
`content` varchar(200) NOT NULL,
`createTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=100000 DEFAULT CHARSET=utf8
这个表依赖的是commentId,上面我们可以通过articleId得到博客的所有的评论,得到这些评论我们就可以通过评论的主键id在ReplyComment表中得到所有回复内容
有个问题就是在Comment A下有reply B,还有reply C,但是C是回复B的,这个时候C存储的commentId还是A,这就需要通过其他方式得到C真正回复的是B
2. 评论的Bean类
存在两个bean
import lombok.Data;
import java.util.List;
/**
* Create by Alden He on 2019/5/7
*/
@Data
public class ArticleComment {
private Integer id;//主键id
private int commentUserId;//创建该评论的用户id
private User user;
private int articleId;//评论的文章id
private String content;//评论内容
private String createTime;//评论的日期
private List<ReplyComment> replyCommentList;
public ArticleComment(){
}
public ArticleComment(int commentUserId,int articleId,String content){
this.articleId=articleId;
this.commentUserId=commentUserId;
this.content=content;
}
}
import lombok.Data;
/**
* Create by Alden He on 2019/5/8
* comment
* reply1(@comment)
* reply2(@reply1)--这条回复@了relpy1,但是也要发通知给comment和article
*/
@Data
public class ReplyComment {
private Integer id;
private int articleId;
private int commentId;
//private int commentUserId;//
private int replyUserId;//创建这条回复的id
private User user;//读取评论的时候需要读取这个user信息
private int repliedUserId;//这条回复的@人的id
private String content;
private String createTime;
public ReplyComment(){
}
public ReplyComment(int articleId,int commentId,int replyUserId,int repliedUserId,String content){
this.articleId=articleId;
this.commentId=commentId;
this.replyUserId=replyUserId;
this.repliedUserId=repliedUserId;
this.content=content;
}
}
3. 评论的Service和ServiceImpl设计
这地方就很简单了
import com.nevergetme.nevergetmeweb.bean.ArticleComment;
import java.util.List;
/**
* Create by Alden He on 2019/5/7
*/
public interface ArticleCommentService {
List<ArticleComment> getArticleCommentsByArticleId(int id);
int addArticleComment(ArticleComment articleComment);
}
import com.nevergetme.nevergetmeweb.bean.ReplyComment;
/**
* Create by Alden He on 2019/5/8
*/
public interface ReplyCommentService {
int addReplyCommentMapper(ReplyComment replyComment);
}
然后就是Service类的实现
import com.nevergetme.nevergetmeweb.bean.ArticleComment;
import com.nevergetme.nevergetmeweb.mapper.ArticleCommentMapper;
import com.nevergetme.nevergetmeweb.service.ArticleCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Create by Alden He on 2019/5/7
*/
@Service
public class ArticleCommentServiceImpl implements ArticleCommentService {
@Autowired
private ArticleCommentMapper articleCommentMapper;
@Override
public List<ArticleComment> getArticleCommentsByArticleId(int id) {
return articleCommentMapper.getArticleCommentsByArticleId(id);
}
@Override
public int addArticleComment(ArticleComment articleComment) {
return articleCommentMapper.addArticleComment(articleComment);
}
}
import com.nevergetme.nevergetmeweb.bean.ReplyComment;
import com.nevergetme.nevergetmeweb.mapper.ReplyCommentMapper;
import com.nevergetme.nevergetmeweb.service.ReplyCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Create by Alden He on 2019/5/8
*/
@Service
public class ReplyCommentServiceImpl implements ReplyCommentService {
@Autowired
private ReplyCommentMapper replyCommentMapper;
@Override
public int addReplyCommentMapper(ReplyComment replyComment) {
return replyCommentMapper.addReplyCommentMapper(replyComment);
}
}
4. 评论的Mapper实现
首先是java接口,只是提供简单的接口
import com.nevergetme.nevergetmeweb.bean.ArticleComment;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* Create by Alden He on 2019/5/7
*/
@Mapper
public interface ArticleCommentMapper {
List<ArticleComment> getArticleCommentsByArticleId(int id);
int addArticleComment(ArticleComment articleComment);
}
import com.nevergetme.nevergetmeweb.bean.ReplyComment;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* Create by Alden He on 2019/5/8
*/
@Mapper
public interface ReplyCommentMapper {
int addReplyCommentMapper(ReplyComment replyComment);
List<ReplyComment> getReplyCommentsByCommentId(int id);
}
具体的mapper xml
在ArticleCommentMapper中,定义了一个resultMap,resultMapper中定义了一个association和一个collection
这个association是这个comment的用户信息
collection是这个comment地下的评论,都是通过主键进行查询
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.nevergetme.nevergetmeweb.mapper.ArticleCommentMapper">
<resultMap id="articleCommentsWithUser" type="com.nevergetme.nevergetmeweb.bean.ArticleComment">
<id column="id" property="id"/>
<result column="commentUserId" property="commentUserId"/>
<result column="articleId" property="articleId"/>
<result column="content"