实现核心功能,包括数据库设计,java代码编写。
使用 @用户 评论方法,避免了多重循环嵌套方式。
微博、思否就是用这种评论方式。
效果图:
微博(评论时间倒叙,从下向上看):
思否
数据库设计:
涉及到三张表:
评论表 ↓
CREATE TABLE `pm_comment` ( `id` varchar(30) NOT NULL, `pid` varchar(30) DEFAULT NULL, `pictureid` varchar(30) DEFAULT NULL, `userid` varchar(32) DEFAULT NULL COMMENT '评论用户id', `beicommentuserid` varchar(32) DEFAULT NULL COMMENT '被评论用户id,如果添加根节点评论,则为空', `commenttime` datetime DEFAULT NULL, `isrelease` int(11) DEFAULT '1', `isdel` int(11) DEFAULT '0', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
评论内容表 ↓
CREATE TABLE `pm_commentcontent` ( `id` varchar(32) NOT NULL DEFAULT '', `commentid` varchar(30) NOT NULL, `commentcontent` varchar(500) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
用户表 ↓
CREATE TABLE `pm_user` ( `id` varchar(30) NOT NULL, `account` varchar(20) NOT NULL, `password` varchar(40) NOT NULL, `nickname` varchar(20) DEFAULT NULL, `avatar` varchar(200) DEFAULT NULL, `gender` varchar(10) NOT NULL, `phonenumber` varchar(11) NOT NULL, `email` varchar(50) NOT NULL, `createtime` datetime NOT NULL, `updatetime` datetime DEFAULT NULL, `isdel` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
关联查询出评论记录:
SELECT pc.id, pc.pid, pc.pictureid, pc.userid, pu.account AS username, pc.beicommentuserid, pus.account AS beicommentusername, pc.commenttime, pc.isrelease, pc.isdel, pcc.commentcontent AS commentcontentFROM pm_comment AS pcINNER JOIN pm_commentcontent AS pcc ON pc.id = pcc.commentidINNER JOIN pm_user AS pu ON pc.userid = pu.idLEFT JOIN pm_user AS pus ON pc.beicommentuserid = pus.idWHERE pc.pictureid = '176476335496474624'-- AND pc.pid = '0'ORDER BY pc.commenttime DESC
查询结果 ↓