首页 > 编程 > Java > 正文

Java递归获取评论列表

2019-11-08 01:04:46
字体:
来源:转载
供稿:网友

    递归算法是一种直接或间接地调用自身的算法。在计算机编写程序中,递归算法往往使算法的描述简洁而且易于理解,譬如获取具有明显父子级结构的数据时(部门树、评论列表等等)。

Service层关键代码

public List<CommentTreeVo> getCommentTree(String topicId,String type) {		List<CommentTreeVo> list=null;		try {			list=getTree(topicId,type);		} catch (Exception e) {			e.PRintStackTrace();		}				return  list;	}	public List<CommentTreeVo> getTree(String topicId,String type)  throws Exception{		CommentTreeVo root = new CommentTreeVo(0, "root");		addTree(root,topicId,type);		List<CommentTreeVo> treeList=new ArrayList<CommentTreeVo>();		treeList.add(root);		return treeList;	}	private void addTree(CommentTreeVo parent,String topicId,String type) {		List<CommentHistory> historyList = commentHistoryDao.findCommentByParentId(parent.getId(),topicId,type);		if (historyList != null && historyList.size() > 0) {			if (parent.getChildren() == null) {				parent.setChildren(new ArrayList<CommentTreeVo>());			}			for (CommentHistory temp : historyList) {				CommentTreeVo vo = new CommentTreeVo();				vo.setId(temp.getId());				vo.setText(temp.getWords());				vo.setCreateTime(temp.getCreateTime());				SimpleUser fromUser = temp.getFromUser();				if (fromUser != null) {					vo.setUserId(fromUser.getId());					vo.setFromUserName(fromUser.getUsername());					vo.setFromUserAvatarUrl(fromUser.getHeadImage().getUrl());				}				SimpleUser toUser = temp.getToUser();				if (toUser != null) {					vo.setToUserId(toUser.getId());					vo.setToUserName(toUser.getUsername());					vo.setToUserAvatarUrl(toUser.getHeadImage().getUrl());				}				parent.getChildren().add(vo);				addTree(vo,topicId,type);			}					}	}CommentHistory实体

@Entity@Table(name = "comment_history")public class CommentHistory implements Serializable{	/**	 * 	 */	private static final long serialVersionUID = 1L;	@Id	@GeneratedValue(strategy = GenerationType.IDENTITY)	private long id;		@OneToOne(cascade =  {CascadeType.REFRESH},optional = false)  	@JoinColumn(name = "userId")  	private SimpleUser fromUser;	private String topicId;	private long createTime;	private String type;  // 1视频 2 咨询 3电音节	private long pid;	@OneToOne(cascade =  {CascadeType.REFRESH},optional = false)  	@JoinColumn(name = "toUserId")  	private SimpleUser toUser;	private String words;		public CommentHistory(){		this.createTime=System.currentTimeMillis();	}	public long getId() {		return id;	}	public void setId(long id) {		this.id = id;	}		public String getTopicId() {		return topicId;	}	public void setTopicId(String topicId) {		this.topicId = topicId;	}	public String getType() {		return type;	}	public void setType(String type) {		this.type = type;	}	public long getCreateTime() {		return createTime;	}	public void setCreateTime(long createTime) {		this.createTime = createTime;	}	public long getPid() {		return pid;	}	public void setPid(long pid) {		this.pid = pid;	}	public String getWords() {		return words;	}	public void setWords(String words) {		this.words = words;	}	public SimpleUser getFromUser() {		return fromUser;	}	public void setFromUser(SimpleUser fromUser) {		this.fromUser = fromUser;	}	public SimpleUser getToUser() {		return toUser;	}	public void setToUser(SimpleUser toUser) {		this.toUser = toUser;	}	}

CommentTreeVo

public class CommentTreeVo  {	private long id;	private int userId;	private String fromUserAvatarUrl;	private String fromUserName;	private int toUserId;	private String toUserAvatarUrl;	private String toUserName;	private long createTime;	private String text;	private List<CommentTreeVo> children;	private String topicId;	private String type;    public CommentTreeVo(){			}	public CommentTreeVo(int id, String text) {		this.id = id;		this.text = text;	}	public long getId() {		return id;	}	public void setId(long id) {		this.id = id;	}	public String getText() {		return text;	}	public void setText(String text) {		this.text = text;	}	public List<CommentTreeVo> getChildren() {		return children;	}	public void setChildren(List<CommentTreeVo> children) {		this.children = children;	}	public int getUserId() {		return userId;	}	public void setUserId(int userId) {		this.userId = userId;	}	public int getToUserId() {		return toUserId;	}	public void setToUserId(int toUserId) {		this.toUserId = toUserId;	}	public long getCreateTime() {		return createTime;	}	public void setCreateTime(long createTime) {		this.createTime = createTime;	}	public String getFromUserAvatarUrl() {		return fromUserAvatarUrl;	}	public void setFromUserAvatarUrl(String fromUserAvatarUrl) {		this.fromUserAvatarUrl = fromUserAvatarUrl;	}	public String getFromUserName() {		return fromUserName;	}	public void setFromUserName(String fromUserName) {		this.fromUserName = fromUserName;	}	public String getToUserAvatarUrl() {		return toUserAvatarUrl;	}	public void setToUserAvatarUrl(String toUserAvatarUrl) {		this.toUserAvatarUrl = toUserAvatarUrl;	}	public String getToUserName() {		return toUserName;	}	public void setToUserName(String toUserName) {		this.toUserName = toUserName;	}	public String getTopicId() {		return topicId;	}	public void setTopicId(String topicId) {		this.topicId = topicId;	}	public String getType() {		return type;	}	public void setType(String type) {		this.type = type;	}	}

controller和dao层代码略

comment_history表的结构及数据:

得到的json数据如下:

{  "code": "200",  "msg": "调用成功",  "stat": "ok",  "dataList": [    [      {        "id": 0,        "userId": 0,        "fromUserAvatarUrl": null,        "fromUserName": null,        "toUserId": 0,        "toUserAvatarUrl": null,        "toUserName": null,        "createTime": 0,        "text": "root",        "children": [          {            "id": 1,            "userId": 5,            "fromUserAvatarUrl": "/uploadFile/person/cropperImage/c8d23098-c4dd-4978-a6f2-1584285306f7.thumb.224_0.jpeg",            "fromUserName": "李秀才",            "toUserId": 0,            "toUserAvatarUrl": null,            "toUserName": null,            "createTime": 1487583133008,            "text": "我是一楼。",            "children": [              {                "id": 2,                "userId": 5,                "fromUserAvatarUrl": "/uploadFile/person/cropperImage/c8d23098-c4dd-4978-a6f2-1584285306f7.thumb.224_0.jpeg",                "fromUserName": "李秀才",                "toUserId": 0,                "toUserAvatarUrl": null,                "toUserName": null,                "createTime": 1487583178371,                "text": "我是二楼。",                "children": null,                "topicId": null,                "type": null              },              {                "id": 3,                "userId": 5,                "fromUserAvatarUrl": "/uploadFile/person/cropperImage/c8d23098-c4dd-4978-a6f2-1584285306f7.thumb.224_0.jpeg",                "fromUserName": "李秀才",                "toUserId": 0,                "toUserAvatarUrl": null,                "toUserName": null,                "createTime": 1487583178371,                "text": "我是三楼。",                "children": null,                "topicId": null,                "type": null              }            ],            "topicId": null,            "type": null          },          {            "id": 4,            "userId": 6,            "fromUserAvatarUrl": "/uploadFile/person/cropperImage/2a4e72ee-1701-4380-a43e-3cb88c72f586.jpg",            "fromUserName": "呵呵",            "toUserId": 0,            "toUserAvatarUrl": null,            "toUserName": null,            "createTime": 1487583178371,            "text": "一出手就是十个字。",            "children": null,            "topicId": null,            "type": null          }        ],        "topicId": null,        "type": null      }    ]  ]}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表