6_1_评论中心
分类:
IT文章
•
2024-05-24 20:32:48
一 需求描述
1. 当分享发布一条头条消息后,当前用户可在指定消息内评论头条;
2. 评论完成后能够在当前页面显示以及评论数的改变
效果描述:

二 具体实现
1. DB
DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment`(
`id` int NOT NULL AUTO_INCREMENT,
`content` TEXT NOT NULL comment '评论内容',
`entity_id` INT NOT NULL comment '实体id',
`entity_type` INT NOT NULL comment '实体类型',
`created_date` DATETIME NOT NULL comment '创建日期',
`user_id` INT NOT NULL comment '评论用户id',
`status` INT NOT NULL DEFAULT 0 comment '评论会话状态',
PRIMARY KEY(`id`),
INDEX `entity_index `(`entity_id` ASC ,`entity_type` ASC )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='评论中心';
View Code
2. Model
package com.nowcoder.model;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* Created by Administrator on 2017/4/29.
*/
@Component
public class Comment {
private int id;
private String content;
private int entityId;
private int entityType;
private Date createdDate;
private int userId;
private int status;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getEntityId() {
return entityId;
}
public void setEntityId(int entityId) {
this.entityId = entityId;
}
public int getEntityType() {
return entityType;
}
public void setEntityType(int entityType) {
this.entityType = entityType;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
View Code
3. Dao:
int addCommnet(Comment comment); 添加评论
List<Comment> selectByEntity(@Param("entityId") int entityId, @Param("entityType") int entityType); : 根据entityId查询所有评论;
int getCommentCount(@Param("entityId") int entityId, @Param("entityType") int entityType); : 获取所有评论数量
void updateStatus(@Param("entityId") int entityId,
@Param("entityType") int entityType,
@Param("status") int status); 删除评论
有两个点要说一下: entityId 和entityType 一起 代表一个咨询。这里type即为news,而entityId 即为newsId.假如还有news,topics等等别的可以直接套用。
第二个就是删除评论,并不是直接删除,而是将status设置为1.
package com.nowcoder.dao;
import com.nowcoder.model.Comment;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
* Created by Administrator on 2017/4/29.
*/
@Mapper
public interface CommentDao {
String TABLE_NAME = "comment";
String INSERT_FIELDS = "content, entity_id, entity_type, created_date, user_id, status";
String SELECT_FIELDS = "id," + INSERT_FIELDS;
@Insert({"insert into", TABLE_NAME, "(", INSERT_FIELDS,
") values(#{content}, #{entityId}, #{entityType}, #{createdDate}, #{userId}, #{status})" })
int addCommnet(Comment comment);
@Select({"select", SELECT_FIELDS, "from", TABLE_NAME,
"where entity_id = #{entityId} and entity_type=#{entityType} order by id desc"})
List<Comment> selectByEntity(@Param("entityId") int entityId, @Param("entityType") int entityType);
@Select({"select count(id) from", TABLE_NAME,
"where entity_id = #{entityId} and entity_type=#{entityType}"})
int getCommentCount(@Param("entityId") int entityId, @Param("entityType") int entityType);
@Update({"update", TABLE_NAME, "set status=#{status} where entity_id = #{entityId} and entity_type=#{entityType}"})
void updateStatus(@Param("entityId") int entityId,
@Param("entityType") int entityType,
@Param("status") int status);
}