赞
踩
1、需求分析(一问一答模式)
这种设计简单、直接,也满足了用户评论、回复的基本要求,对于没有大量用户评论的APP需求足够。
2、数据库字段设计
表字段 | 字段说明 |
id | 主键 |
show_id | 主题id (音乐或MV) |
user_id | 评论用户userId |
content | 评论内容(长度限制为140个中文汉字) |
give_like | 点赞总量 |
create_time | 评论时间 |
state | 评论状态(正常默认 1,已删除 0) |
reply_id | 回复评论id |
因为我数据库没有冗余评论用户头像和昵称,需要根据user_id去查询,返回到一个评论中间类中
reply_id 字段默认为null,该字段不为null时,表示该条评论是回复别人的评论,用于设置回复某条评论的主键id
state 字段是会用到的,主要场景如下图
回复评论作者已经删除评论的话,不显示原评论的所有信息
3、返回评论列表
返回评论的数据结构这里,我用到了三个类(评论类、评论中间类、最终返回的评论类)
评论类
- package com.shinemi.realiart.model;
-
- import java.util.Date;
- // 评论类
- public class ArtShowComments {
- private Long id;
-
- private Long showId;
-
- private Long userId;
-
- private String content;
-
- private Long giveLike;
-
- private Date createTime;
-
- private Integer state;
-
- private Long replyId;
-
- public Long getId() {
- return id;
- }
-
- public void setId(Long id) {
- this.id = id;
- }
-
- public Long getShowId() {
- return showId;
- }
-
- public void setShowId(Long showId) {
- this.showId = showId;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public void setUserId(Long userId) {
- this.userId = userId;
- }
-
- public String getContent() {
- return content;
- }
-
- public void setContent(String content) {
- this.content = content == null ? null : content.trim();
- }
-
- public Long getGiveLike() {
- return giveLike;
- }
-
- public void setGiveLike(Long giveLike) {
- this.giveLike = giveLike;
- }
-
- public Date getCreateTime() {
- return createTime;
- }
-
- public void setCreateTime(Date createTime) {
- this.createTime = createTime;
- }
-
- public Integer getState() {
- return state;
- }
-
- public void setState(Integer state) {
- this.state = state;
- }
-
- public Long getReplyId() {
- return replyId;
- }
-
- public void setReplyId(Long replyId) {
- this.replyId = replyId;
- }
- }
- package com.shinemi.realiart.bean;
-
- import com.shinemi.realiart.model.ArtShowComments;
-
- // 评论中间类
- public class ArtShowCommentsBean extends ArtShowComments {
- // 评论用户头像
- private String userHeadPortrait;
- // 评论用户昵称
- private String userNickName;
-
- public void setArtShowComments(ArtShowComments artShowComments) {
- this.setId(artShowComments.getId());
- this.setShowId(artShowComments.getShowId());
- this.setUserId(artShowComments.getUserId());
- this.setContent(artShowComments.getContent());
- this.setGiveLike(artShowComments.getGiveLike());
- this.setCreateTime(artShowComments.getCreateTime());
- this.setState(artShowComments.getState());
- this.setReplyId(artShowComments.getReplyId());
- }
-
- public String getUserHeadPortrait() {
- return userHeadPortrait;
- }
-
- public void setUserHeadPortrait(String userHeadPortrait) {
- this.userHeadPortrait = userHeadPortrait;
- }
-
- public String getUserNickName() {
- return userNickName;
- }
-
- public void setUserNickName(String userNickName) {
- this.userNickName = userNickName;
- }
-
- }
评论返回类(该类中有三个变量,那就是上面的评论中间类和评论拥有者的状态了,评论拥有者主要给予前端判断该评论是否能让当前用户删除)
- package com.shinemi.realiart.bean;
-
- // 评论返回类
- public class ArtShowCommentsBO {
- // 原评论
- private ArtShowCommentsBean originalComments;
- // 回复评论
- private ArtShowCommentsBean replyComments;
- // 评论拥有者
- private boolean realyReviewers;
-
- public ArtShowCommentsBean getOriginalComments() {
- return originalComments;
- }
-
- public void setOriginalComments(ArtShowCommentsBean originalComments) {
- this.originalComments = originalComments;
- }
-
- public ArtShowCommentsBean getReplyComments() {
- return replyComments;
- }
-
- public void setReplyComments(ArtShowCommentsBean replyComments) {
- this.replyComments = replyComments;
- }
-
- public boolean isRealyReviewers() {
- return realyReviewers;
- }
-
- public void setRealyReviewers(boolean realyReviewers) {
- this.realyReviewers = realyReviewers;
- }
- }
思路流程是这样的:
根据主题id 分页查询 评论表,将获取回来的评论进行遍历,判断reply_id是否为null,是的话new 中间类对象,将评论对象设置进去,再查出评论者头像and昵称设置,完成后创建评论返回类、将刚刚的评论中间类set 入 评论返回类的 originalComments就ok了
reply_id 不为null、就需要根据reply_id去查询评论类、再同上的操作、就是最后set 入 评论返回类的 replyComments字段中就ok
以上是个人观点设计,若有不足的地方或更好的设计,欢迎提出。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。