递归评论

一、表结构设计

//社区评论表
CREATE TABLE `g_community_comment` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `statement` varchar(5000) DEFAULT NULL COMMENT '评论',
  `user_id` bigint(11) DEFAULT NULL COMMENT '用户ID',
  `nick_name` varchar(64) COLLATE utf8_bin DEFAULT NULL COMMENT '昵称',
  `avatar` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '头像地址',
  `message_id` bigint(11) DEFAULT NULL COMMENT '发帖ID(冗余),方便查询',
  `be_id` bigint(11) NOT NULL COMMENT '跟帖ID或者发帖中朋友圈类型ID',
  `circle_type` tinyint(2) DEFAULT '0' COMMENT '1:基本面,2:技术面,3:朋友圈',
  `super_id` bigint(11) NOT NULL DEFAULT '0' COMMENT '上级评论ID',
  `level` bigint(11) NOT NULL DEFAULT '0' COMMENT '第几级评论,0,1,2..',
  `all_super_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '所有上级ID',
  `is_read` tinyint(2) DEFAULT '0' COMMENT '是否已读(0 未读,1 已读)',
  `status` tinyint(2) DEFAULT '0' COMMENT '0正常,1删除',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '修改时间',
  `create_date` timestamp NULL DEFAULT NULL COMMENT '创建时间,管理后台使用',
  `update_date` timestamp NULL DEFAULT NULL COMMENT '修改时间,管理后台使用',
  `create_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '创建者,管理后台使用',
  `update_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '修改者,管理后台使用',
  `remarks` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT '' COMMENT '系统备注,管理后台使用 ',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8  COLLATE=utf8_bin COMMENT='社区评论表';

二、递归代码

CommunityCommentDto
package com.general.msg.vo.response;

import java.util.Date;
import java.util.List;

public class CommunityCommentDto {
    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private Long id;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.statement
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private String statement;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.user_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private Long userId;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.nick_name
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private String nickName;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.avatar
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private String avatar;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.message_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private Long messageId;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.be_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private Long beId;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.circle_type
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private Byte circleType;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.super_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private Long superId;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.level
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private Long level;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.all_super_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private String allSuperId;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.is_read
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private Byte isRead;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.status
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private Byte status;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.create_time
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private Date createTime;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.update_time
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private Date updateTime;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.create_date
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private Date createDate;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.update_date
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private Date updateDate;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.create_by
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private String createBy;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.update_by
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private String updateBy;

    /**
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column g_community_comment.remarks
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    private String remarks;


    private List<CommunityCommentDto> sonCommentList;

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.id
     *
     * @return the value of g_community_comment.id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public Long getId() {
        return id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.id
     *
     * @param id the value for g_community_comment.id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.statement
     *
     * @return the value of g_community_comment.statement
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public String getStatement() {
        return statement;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.statement
     *
     * @param statement the value for g_community_comment.statement
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setStatement(String statement) {
        this.statement = statement == null ? null : statement.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.user_id
     *
     * @return the value of g_community_comment.user_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public Long getUserId() {
        return userId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.user_id
     *
     * @param userId the value for g_community_comment.user_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setUserId(Long userId) {
        this.userId = userId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.nick_name
     *
     * @return the value of g_community_comment.nick_name
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public String getNickName() {
        return nickName;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.nick_name
     *
     * @param nickName the value for g_community_comment.nick_name
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setNickName(String nickName) {
        this.nickName = nickName == null ? null : nickName.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.avatar
     *
     * @return the value of g_community_comment.avatar
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public String getAvatar() {
        return avatar;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.avatar
     *
     * @param avatar the value for g_community_comment.avatar
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setAvatar(String avatar) {
        this.avatar = avatar == null ? null : avatar.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.message_id
     *
     * @return the value of g_community_comment.message_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public Long getMessageId() {
        return messageId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.message_id
     *
     * @param messageId the value for g_community_comment.message_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setMessageId(Long messageId) {
        this.messageId = messageId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.be_id
     *
     * @return the value of g_community_comment.be_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public Long getBeId() {
        return beId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.be_id
     *
     * @param beId the value for g_community_comment.be_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setBeId(Long beId) {
        this.beId = beId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.circle_type
     *
     * @return the value of g_community_comment.circle_type
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public Byte getCircleType() {
        return circleType;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.circle_type
     *
     * @param circleType the value for g_community_comment.circle_type
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setCircleType(Byte circleType) {
        this.circleType = circleType;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.super_id
     *
     * @return the value of g_community_comment.super_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public Long getSuperId() {
        return superId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.super_id
     *
     * @param superId the value for g_community_comment.super_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setSuperId(Long superId) {
        this.superId = superId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.level
     *
     * @return the value of g_community_comment.level
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public Long getLevel() {
        return level;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.level
     *
     * @param level the value for g_community_comment.level
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setLevel(Long level) {
        this.level = level;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.all_super_id
     *
     * @return the value of g_community_comment.all_super_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public String getAllSuperId() {
        return allSuperId;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.all_super_id
     *
     * @param allSuperId the value for g_community_comment.all_super_id
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setAllSuperId(String allSuperId) {
        this.allSuperId = allSuperId == null ? null : allSuperId.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.is_read
     *
     * @return the value of g_community_comment.is_read
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public Byte getIsRead() {
        return isRead;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.is_read
     *
     * @param isRead the value for g_community_comment.is_read
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setIsRead(Byte isRead) {
        this.isRead = isRead;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.status
     *
     * @return the value of g_community_comment.status
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public Byte getStatus() {
        return status;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.status
     *
     * @param status the value for g_community_comment.status
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setStatus(Byte status) {
        this.status = status;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.create_time
     *
     * @return the value of g_community_comment.create_time
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.create_time
     *
     * @param createTime the value for g_community_comment.create_time
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.update_time
     *
     * @return the value of g_community_comment.update_time
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public Date getUpdateTime() {
        return updateTime;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.update_time
     *
     * @param updateTime the value for g_community_comment.update_time
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.create_date
     *
     * @return the value of g_community_comment.create_date
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public Date getCreateDate() {
        return createDate;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.create_date
     *
     * @param createDate the value for g_community_comment.create_date
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.update_date
     *
     * @return the value of g_community_comment.update_date
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public Date getUpdateDate() {
        return updateDate;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.update_date
     *
     * @param updateDate the value for g_community_comment.update_date
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.create_by
     *
     * @return the value of g_community_comment.create_by
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public String getCreateBy() {
        return createBy;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.create_by
     *
     * @param createBy the value for g_community_comment.create_by
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setCreateBy(String createBy) {
        this.createBy = createBy == null ? null : createBy.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.update_by
     *
     * @return the value of g_community_comment.update_by
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public String getUpdateBy() {
        return updateBy;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.update_by
     *
     * @param updateBy the value for g_community_comment.update_by
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy == null ? null : updateBy.trim();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column g_community_comment.remarks
     *
     * @return the value of g_community_comment.remarks
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public String getRemarks() {
        return remarks;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column g_community_comment.remarks
     *
     * @param remarks the value for g_community_comment.remarks
     *
     * @mbggenerated Thu Oct 25 17:16:45 CST 2018
     */
    public void setRemarks(String remarks) {
        this.remarks = remarks == null ? null : remarks.trim();
    }

    public List<CommunityCommentDto> getSonCommentList() {
        return sonCommentList;
    }

    public void setSonCommentList(List<CommunityCommentDto> sonCommentList) {
        this.sonCommentList = sonCommentList;
    }
}
View Code

递归方法

 /**
     * 递归评论
     */
    private List<CommunityCommentDto> toRecursionList(CommunityCommentDto dto,List<CommunityCommentDto> commentDtoList,long level){
        if (ListUtil.isNull(commentDtoList)){
            return null;
        }
        if (dto==null){
            //先拿第0级的评论
            List<CommunityCommentDto> fartherList=new ArrayList<>();
            for (CommunityCommentDto communityCommentDto : commentDtoList) {
                if (communityCommentDto.getLevel()==0){
                    fartherList.add(communityCommentDto);
                }
            }
            //递归查询并设置子级别的评论集合
            for (CommunityCommentDto fartherDto : fartherList) {
                List<CommunityCommentDto> sonList = toRecursionList(fartherDto, commentDtoList, level);
                fartherDto.setSonCommentList(sonList);
            }
            return fartherList;
        }else {
            //拿子级别的评论集合
            level++;
            List<CommunityCommentDto> sonList=new ArrayList<>();
            for (CommunityCommentDto communityCommentDto : commentDtoList) {
                if (communityCommentDto.getLevel()==level&&communityCommentDto.getSuperId()==dto.getId()){
                    sonList.add(communityCommentDto);
                }
            }
            //设置子级别的评论集合
            dto.setSonCommentList(sonList);
            //递归查询并设置子级别的评论集合
            for (CommunityCommentDto sonDto : sonList) {
                List<CommunityCommentDto> nextSonList = toRecursionList(sonDto, commentDtoList, level);
                sonDto.setSonCommentList(nextSonList);
            }
            return sonList;
        }
    }

猜你喜欢

转载自www.cnblogs.com/kesimin/p/9894927.html