很多网站都提供了留言,评论回复功能,而我做的一个分享圈项目同样要实现的功能。
思路:
1.可以只创建一个留言表,用户的留言和回复功能都插进这张表,这样建议在网页显示效果用爬楼形式,因为数据都在一张表,划分留言跟回复情况会有点复杂。
2.创建留言表和回复表,用户的留言和回复插入相应的表,这样在网页中显示可以进行清晰的迭代数据库的数据,进行分层显示。
效果:

实现步骤:
1.创建留言表和回复表:
-- ----------------------------
-- Table structure for `comment`
-- ----------------------------
DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment` (
`c_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '评论表id',
`c_userid` varchar(50) DEFAULT NULL COMMENT '评论者id',
`c_contentid` int(11) DEFAULT NULL COMMENT '内容id(在哪个内容下评论的)',
`c_createtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '插入时间',
`c_content` varchar(50) DEFAULT NULL COMMENT '评论的内容',
`c_otherid` varchar(50) DEFAULT NULL COMMENT '给谁(该内容id的作者)留言',
`c_state` int(11) DEFAULT NULL COMMENT '0-未读,1-已读',
PRIMARY KEY (`c_id`),
KEY `userid` (`c_userid`),
KEY `contentid` (`c_contentid`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `reply`
-- ----------------------------
DROP TABLE IF EXISTS `reply`;
CREATE TABLE `reply` (
`r_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '回复表id',
`r_userid` varchar(50) DEFAULT NULL COMMENT '回复的用户账号',
`r_creatime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '插入时间',
`r_content` varchar(100) DEFAULT NULL COMMENT '回复的内容',
`r_otherid` varchar(50) DEFAULT NULL COMMENT '给谁回复',
`r_words` varchar(100) DEFAULT NULL COMMENT '在哪个留言下的回复',
`r_contentid` int(11) DEFAULT NULL COMMENT '那篇分享下的回复',
`r_state` int(11) DEFAULT NULL COMMENT '0-未读,1-已读',
PRIMARY KEY (`r_id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8;
2.后台代码:
评论:
package net.stxy.one.controller;
import net.stxy.one.model.*;
import net.stxy.one.service.CommentService;
import net.stxy.one.service.ContentService;
import net.stxy.one.service.ReplyService;
import net.stxy.one.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* 评论操作的控制
* Created by ASUS on 2018/5/27
*
* @Authod Grey Wolf
*/
@Controller
@RequestMapping("/user")
public class CommentController {
@Autowired
private ContentService contentService;
@Autowired
private CommentService commentService;
@Autowired
private UserinfoService userinfoService;
@Autowired
private ReplyService replyService;
/**
* 跳到评论页面
* @param contentId
* @param model
* @return
*/
@RequestMapping("/comment")
public String comment(@RequestParam("contentId")int contentId, Model model){
System.out.println("id:"+contentId);
//获取该内容的所有信息
Content content=contentService.selectContent(contentId);
//获取该内容的用户信息
Userinfo userinfo=userinfoService.selectUser(content.getUserid());
//获取在该内容下的所有评论
List<Comment> commentList=commentService.selectById(contentId);
//获取在该内容下的所有回复
List<Reply>replyList=replyService.selectReply(contentId);
model.addAttribute("content",content);
model.addAttribute("user",userinfo);
model.addAttribute("commentList",commentList);
model.addAttribute("replyList",replyList);
return "showContent";
}
/**
* 增加评论
* @param comment
* @return
*/
@RequestMapping("/addcomment")
public String addComment(Comment comment){
System.out.println("增加评论:"+comment.toString());
if(comment.get