評論回覆實體:
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