java 一级和二级评论的实现


前言
每一条评论是一条记录,现在前端要得到下方的结构

[
	{
		content: '一级评论内容',
		userId: '',
		userName: '',
		headIcon: '',
		child: [
			{
				userId: '',
				content: '二级评论内容',
		        userName: '',
		        headIcon: '',
			}
		]
	}
]

后端代码实现如下

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

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Comments {
	@Getter @Setter private Integer id;
	@Getter @Setter private Integer state;	// 状态  0-发布 1-删除(下架)
    @JsonProperty("isTop")
	@Getter @Setter private Integer isTop;	// 是否置顶  0-是 1-否 默认 1-否
    @JsonProperty("fId")
	@Getter @Setter private Integer fId;	// 父级id
    @JsonProperty("aId")
	@Getter @Setter private Integer aId;	// 所属id
    @JsonProperty("userId")
	@Getter @Setter private Integer userId;	// 创建者id
    
    @JsonProperty("comments")
	@Getter @Setter private List<Comments> comments;	// 二级评论

    @JsonProperty("userName")
	@Getter @Setter private String userName;	// 用户名
    @JsonProperty("headIcon")
	@Getter @Setter private String headIcon;	// 用户头像
    @Getter @Setter private String content;	// 内容
    @JsonProperty("appId")
	@Getter @Setter private String appId;	// 应用

    @JsonProperty("createTime")
    @Getter @Setter private Date createTime;	// 创建时间
    @JsonProperty("updateTime")
	@Getter @Setter private Date updateTime;	// 更新时间
}

public PageInfo<Comments> query(Map<String, Object> paramMap) {
    	String pageIndex = paramMap.get("pageIndex").toString();
    	String pageSize = paramMap.get("pageSize").toString();
    	String orderBy = "";
    	if (paramMap.containsKey("orderBy")) {
    		orderBy = paramMap.get("orderBy").toString();
		}
        PageHelper.startPage(Integer.parseInt(pageIndex), Integer.parseInt(pageSize), orderBy);
        
    	PageInfo<Comments> resp = new PageInfo<Comments>(commentsMapper.query(paramMap));
		List<Comments> list = resp.getList();
		if(list.size() != 0){
			for(Comments comments : list){
				UserInfo userInfo = userInfoMapper.queryById(comments.getUserId());
				comments.setUserName(userInfo.getName());
				comments.setHeadIcon(userInfo.getHeadIcon());
				
				// 二级评论
				Map<String, Object> paramChildMap = new HashMap<String, Object>();	// 二级评论参数
				paramChildMap.put("pageIndex", 1);
				paramChildMap.put("pageSize", 2);
				paramChildMap.put("fId", comments.getId());
				paramChildMap.put("orderBy", "create_time DESC");
		    	PageInfo<Comments> respChild = new PageInfo<Comments>(commentsMapper.query(paramChildMap));
		    	List<Comments> childList = respChild.getList();
		    	if(childList.size() !=0){
		    		for(Comments commentsChild : childList){
		    			UserInfo userInfoChild = userInfoMapper.queryById(commentsChild.getUserId());
		    			commentsChild.setUserName(userInfoChild.getName());
		    			commentsChild.setHeadIcon(userInfoChild.getHeadIcon());
		    		}
		    	}
				comments.setComments(childList);
			}
		}
        return resp;
    }
<!-- 获取评论列表 -->
    <select id="query" parameterType="java.util.Map" resultType="com.hi.hailiaowenan.affair.bean.Comments">
    	select id, state, is_top as isTop, f_id as fId, a_id as aId, user_id as userId, 
    	content, app_id as appId,
    	create_time as createTime, update_time as updateTime
    	from
    	<include refid="table_name"></include>
        <trim prefix="WHERE" prefixOverrides="AND | OR">
            <if test="keywords != null and keywords != ''">
		    	<!-- bind写法:预防SQL注入  通过关键词搜索 -->
		    	<bind name="pattern" value="'%'+keywords+'%'" />
		    	content like #{pattern}
            </if>
	    	<!-- 状态 -->        
	        <if test="state != '' and state != null or state == 0">
	    		and state = #{state}
	    	</if>
	    	<!-- 父级id -->        
	        <if test="fId != null and fId != ''">
	    		and f_id = #{fId}
	    	</if>
	    	<!-- 所属id -->        
	        <if test="aId != null and aId != ''">
	    		and a_id = #{aId}
	    	</if>
	    	<!-- 一级评论 -->        
	        <if test="fIdNull != null and fIdNull != ''">
	    		and f_id IS null
	    	</if>
	    	<!-- 用户 -->        
	        <if test="userId != null and userId != ''">
	    		and user_id = #{userId}
	    	</if>
	    	<!-- 应用 -->        
	        <if test="appId != null and appId != ''">
	    		and app_id = #{appId}
	    	</if>
        </trim>
    </select>

Guess you like

Origin blog.csdn.net/weixin_40918145/article/details/119174956