一:需求
現有以需求就是把某一個文章的全部評論展示出來。
二:分析
關于對文章的評論分為主評論和子評論,主評論就是對文章的直接評論,子評論就是對評論的評論。
三:思路
先擷取某一個文章的全部主評論,遞歸判斷是否有子評論,擷取子評論。
四:編碼
實體類:
1 import java.util.Date;
2 import java.util.List;
3
4 import com.fasterxml.jackson.annotation.JsonFormat;
5
6 import lombok.Data;
7 @Data
8 public class BsChannelPostReply {
9 private long replyId;
10 private String niceName;
11 @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
12 private Date replyDate;
13 private String content;
14 private long directRepliedId;//回複的直接評論的replyId
15 private List<BsChannelPostReply> children;//下面的子評論
16 }
擷取主評論清單,和遞歸全部子評論:
1 @Override
2 @Datasource(value="community")//切換資料源
3 public List<BsChannelPostReply> getMainReply(int postId) {
4 // TODO Auto-generated method stub
5 List<BsChannelPostReply> listMain=dao.getMainReply(postId);//擷取主評論
6 if(listMain.size()>=0){//如果主評論不為空
7 for (BsChannelPostReply bsChannelPostReply : listMain) {
8 bsChannelPostReply.setChildren(getMainReplyChildren(bsChannelPostReply.getReplyId()));//加載子評論
9 }
10 }
11 return listMain;
12 }
13
14 @Override
15 @Datasource(value="community")//切換資料源
16 public List<BsChannelPostReply> getMainReplyChildren(long replyId) {
17 // TODO Auto-generated method stub
18 List<BsChannelPostReply> listChildren=dao.getMainReplyChildren(replyId);//根據目前的replayId擷取目前級子評論清單
19 if(listChildren.size()>=0){
20 for (BsChannelPostReply bsChannelPostReply : listChildren) {
21 bsChannelPostReply.setChildren(getMainReplyChildren(bsChannelPostReply.getReplyId()));//在判斷目前子評論是否還有子評論,遞歸調用,直到沒有子評論
22 }
23 }
24 return listChildren;
25 }
五:效果
根據這樣的遞歸調用就可以實作理論上的擷取無極限的子評論清單。

歡迎大家一起說出自己的想法。