评论回复加载渲染

评论回复实体:
public class Comment {

private static final long serialVersionUID = 1L;
 private String id;
private Answers ansid;		// 说说id
private String context;		// 评价内容
private User conmmenter;		// 评论
private String callid;		// 被评论id
private List<Comment> callList; //回复列表
private User respondent;		//被评论人
}

//链表结构类
public class NodeComment {

public NodeComment(){

}

/**
 * 构造函数
 * @param comment
 */
public NodeComment(Comment comment){
    this.id = comment.getId();
    this.ansid = comment.getAnsid();
    this.callid = comment.getCallid();
    this.context = comment.getContext();
    this.conmmenter = comment.getConmmenter();
    this.respondent = comment.getRespondent();
}

private String id;
private Answers ansid;		// 说说id
private String context;		// 评价内容
private User conmmenter;		// 评论
private String callid;		// 被评论id
private List<Comment> callList; //回复列表
private User respondent;		//被评论人
//下一条回复
private List<NodeComment> nextNodes = new ArrayList<NodeComment>();

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public Answers getAnsid() {
    return ansid;
}
public void setAnsid(Answers ansid) {
    this.ansid = ansid;
}
public String getContext() {
    return context;
}
public void setContext(String context) {
    this.context = context;
}
public User getConmmenter() {
    return conmmenter;
}

public void setConmmenter(User conmmenter) {
    this.conmmenter = conmmenter;
}

public String getCallid() {
    return callid;
}

public void setCallid(String callid) {
    this.callid = callid;
}
public float getGrade() {
    return grade;
}

public void setGrade(float grade) {
    this.grade = grade;
}
public User getRespondent(){
    return respondent;
}
public void setRespondent(User respondent){
    this.respondent =respondent;
}

public List<NodeComment> getNextNodes() {
    return nextNodes;
}
public void setNextNodes(List<NodeComment> nextNodes) {
    this.nextNodes = nextNodes;
}

/**
 * 将单个node添加到链表中
 * @param list
 * @param node
 * @return
 */
public static boolean addNode(List<NodeComment> list,NodeComment node){
    for (NodeComment node1 : list) {   //循环添加
        if (node1.getId().equals(node.getCallid())){   //判断留言的上一段是都是这条留言
            node1.getNextNodes().add(node);   //返回true;
            return true;
        }else{     //否则递归继续判断
            if (node1.getNextNodes().size()!=0)
                if (NodeComment.addNode(node1.getNextNodes(),node)){
                    return true;
                }
        }
    }
    return false;
}


/**
 * 将查出来的lastId不为null的回复都添加到第一层Node集合中
 * @param firstList
 * @param thenList
 * @return
 */
public static List addAllNode(List<NodeComment> firstList,List<Comment> thenList){
    if (thenList.size()!=0){
        int size = thenList.size();
        for (int i = 0;i<size;i++){
            if (NodeComment.addNode(firstList,new NodeComment(thenList.get(i)))){
                thenList.remove(i);
                i--;
                size--;
            }
        }
        return firstList;
    }
    return firstList;
}

//打印
public static void show(List<NodeComment> list){
    for (NodeComment node : list) {
        System.out.println(node.getConmmenter().getName()+" 用户回复了ni"+node.getContext());
        System.out.println(node.getNextNodes().size());
        if (node.getNextNodes().size()!=0){
            NodeComment.show(node.getNextNodes());
        }

    }
}

}

调用的部分代码:

//查询id为ansid.getId()且callid为null的评论
List firstList =commentMapper.findAllByDiaryIdAndLastIdNull(answers.getId());
//查询id为ansid.getId()且callid不为null的评论
List thenList = commentMapper.findAllByDiaryIdAndLastIdNotNull(answers.getId());
//新建一个Node集合。
ArrayList nodes = new ArrayList<>();
//将第一层评论都添加都Node集合中
for (Object o : firstList) {
nodes.add(new NodeComment((Comment) o));
}
List list =NodeComment.addAllNode(nodes, thenList);

渲染效果图如下:
效果图
这个demo 使用了递归插入链表,递归查询链表 但在页面渲染数据处理是比较麻烦,建议在填写评论时callid使用第一条评论的id

原创文章 5 获赞 0 访问量 842

猜你喜欢

转载自blog.csdn.net/shmilyhq/article/details/97939346