欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关于递归调用,实现树形菜单的样式

程序员文章站 2023-04-06 22:15:56
一:需求 现有以需求就是把某一个帖子的全部评论展示出来。 二:分析 关于对帖子的评论分为主评论和子评论,主评论就是对帖子的直接评论,子评论就是对评论的评论。 三:思路 先获取某一个帖子的全部主评论,递归判断是否有子评论,获取子评论。 四:编码 实体类: 获取主评论列表,和递归全部子评论: 五:效果 ......

一:需求

  现有以需求就是把某一个帖子的全部评论展示出来。

二:分析

  关于对帖子的评论分为主评论和子评论,主评论就是对帖子的直接评论,子评论就是对评论的评论。

三:思路

  先获取某一个帖子的全部主评论,递归判断是否有子评论,获取子评论。

四:编码

  实体类:

 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     }

 

五:效果

  根据这样的递归调用就可以实现理论上的获取无极限的子评论列表。

关于递归调用,实现树形菜单的样式