模仿qq空间评论以及回复
来源:互联网 发布:淘宝怎么看我的退货率 编辑:程序博客网 时间:2024/04/30 05:04
先看需求是怎么样的:
再看评论表结构的设计:
create table t_comment( id varchar(32) primary key, #主键Id customer_id varchar(32) not null, #用户Id parent_comment_id varchar(32) not null, #父评论Id content_id varchar(32) not null, #评论对象的Id type int(11), #评论对象的类型 content varchar(140) null, #评论内容 comment_date varchar(10) null, #评论日期 comment_time varchar(12) null, #评论时间 state int(11) null #评论的状态 0显示或者1不显示);
在看实体的设计:
private String id;// idprivate String customerId;// 用户idprivate String parentCommentId;// 父评论idprivate String contentId;// 内容idprivate String content;// 评论内容private String commetDate;// 评论日期private String commetTime;// 评论时间private Integer state;// 评论的状态private Integer type;// 评论类型//=================================private List<Comment> replyComment; // 评论回复信息private Customer customer;// 评论者信息private Customer replyCustomer; // 回复评论的人
分析问题的解决方案,我先说说我解决问题的方案:
首先根据我们要评论的对象得Id,查询出评论该对象的所有父评论;
然后根据父评论的Id去查询子评论的信息。
分析完毕后:我们开始写sql:
下面的t_item 为评论对象中的一种类型, 他的类型码为1 所有tco.type = 1
SELECT tco.* FROM t_comment tco LEFT JOIN t_item tit ON tco.content_id = tit.idWHERE tco.parent_comment_id IS NULLAND tco.type = 1AND comment.state = 0AND tit.id = #{itemId}ORDER BY tco.comment_date DESC, tco.comment_time DESC
以上为查询所有的父评论信息;
然后我们再根据父评论的Id查询子评论的信息:
SELECT tco.* FROM t_comment tco WHERE tco.parent_comment_id = #{parentCommentId} AND comment.state = 0
以上是查询子评论的信息
通过上面的sql,我们只是简单的查询了语句:
我们在看看java中怎么去构建评论的集合
dao的编写:就是把以上的两个sql语句变成两个方法。
根据评论对象的Id查询所有父评论:List<Comment> findParentCommentByItemId(String itemId, int offset, int limit);
根据父评论的Id查询所有子评论:List<Comment> findReplyCommentByCommentId(String parentCommentId, int offset, int limit);
然后我们看service怎么实现:
根据评论对象的Id查询所有评论:List<Comment> findCommentByItemId(String itemId, int offset, int limit);
具体的实现:
public List<Comment> findCommentByItemId(String itemId, int offset, int limit) {List<Comment> comments = commentDao.findParentCommentByItemId(itemId, offset, limit);for(Comment comment : comments){List<Comment> replys = new ArrayList<Comment>(); // 实例化回复的集合comment.setReplyComment(replys); // 设置评论的回复集合String customerId = comment.getCustomerId(); // 获取评论的人的IdCustomer customer = customerService.getCustomerByCustomerId(customerId); // 通过评论人的Id获取评论人的信息if(customer != null) customer.setPassword(null); // 把评论的人的密码设置为空comment.setCustomer(customer); // 设置评论的人的信息buildReplyComment(comment, replys, offset, limit); // 构建评论与回复信息}return comments;}
/** * 构建评论与回复评论的关系 * @param comment * @param offset * @param limit */private void buildReplyComment(Comment comment, List<Comment> replys, int offset, int limit){List<Comment> replyComments = commentDao.findReplyCommentByCommentId(comment.getId(), offset, limit); // 获取评论的所有回复replys.addAll(replyComments); // 把所有的回复添加到评论实例化的回复集合中for(Comment c : replyComments){ // 遍历回复中的回复String customerId = c.getCustomerId(); // 获取回复人的idCustomer replyCustomer = customerService.getCustomerByCustomerId(customerId); // 获取回复人信息if(replyCustomer != null) replyCustomer.setPassword(null); // 把回复人的密码设置为空Customer customer = customerService.getCustomerByCustomerId(comment.getCustomerId()); // 获取评论人的信息c.setCustomer(customer); // 设置评论人的信息c.setReplyCustomer(replyCustomer); // 设置回复人的信息buildReplyComment(c, replys, offset, limit); // 递归调用}}
最后看页面的展示:
<c:forEach var="comment" items="${itemComments}"><c:out value="${comment.customer.nickName}"/>:<span class="ellipsis">${comment.content}</span><span class="ellipsis">(<c:out value="${comment.commetDate}" />)</span><c:if test="${!empty comment.replyComment}"><c:forEach var="reply" items="${comment.replyComment}">${reply.replyCustomer.nickName}回复了${reply.customer.nickName}<span class="ellipsis">${reply.content}</span><span class="ellipsis">(<c:out value="${comment.commetDate}" />)</span></c:forEach></c:if></c:forEach>
样式方面需要调整,答题的功能就这个样子了,
还有不足请指出来,谢谢
2 0
- 模仿qq空间评论以及回复
- 仿微信朋友圈/qq空间评论 下载刷新下拉加载回复、评论等
- 设置QQ空间评论回复权限,包括日志、相册、说说、留言板的评论回复和留言权限,限制名单成员无评论回复权限
- qq空间评论bug?
- QQ空间秒赞秒评论
- TP模仿qq发表状态回复功能
- html+css qq空间模仿
- qq空间评论的功能
- 小黄鸡QQ空间自动回复机器人
- Android QQ空间说说回复功能简介
- Android QQ空间说说回复功能简介
- 3.模仿qq空间背景下拉动画
- QQ空间触屏版说说模拟评论
- 实现QQ空间评论列表样式
- 仿QQ空间文章列表+评论查询
- Android仿QQ空间二级评论列表
- Android评论工具类,实现QQ空间文字评论效果
- 实现类似微信朋友圈或者QQ空间,评论回复,九宫格布局。处理键盘弹出后定位到当前点击的被评论人处。另:滑动时候FPS在57-60之间,纵享丝滑
- 1035. 插入与归并
- qgy 的ios 一些小经验,小技巧
- C++单例设计模式
- 屏幕模式
- 【图解】Linux下C程序进程地址空间布局
- 模仿qq空间评论以及回复
- uinty
- Linux常用命令
- java面试题
- nyoj-171 聪明的kk
- 【剑指Offer面试编程题】题目1388:跳台阶--九度OJ
- 53 java.io.IOException: open failed: EACCES (Permission denied)问题解决
- 如何使用service
- 解决方案、项目、程序集、命名空间