评论回复功能的数据库设计(JAVA+MYSQl) 您所在的位置:网站首页 怎么回复博客的信息 评论回复功能的数据库设计(JAVA+MYSQl)

评论回复功能的数据库设计(JAVA+MYSQl)

2024-03-18 16:59| 来源: 网络整理| 查看: 265

 1.概述

评论回复功能是社交网站最基本的功能,本文主要讲解评论留言的设计及实现。

需求:

用户评论日记,回复评论显示所有评论 2.数据库设计

日记表:diary

用户表:user

回复表:reply

字段设计

private int id ; //回复id (主键id) private Integer diaryId; //日记id (关联日记表) private String text; //回复内容 private Integer userId; //用户id (关联用户表) private Integer lastId; //上一条回复id (用于关联回复表,维护好每条评论的关系)

外键关系

讲解:

通过last_id我们就能找到这条回复是回复的哪条评论或日记。

3.java实现思路 存储我们使用链表,这样可以一步一步找到最后一条回复,应为一条评论下可能有多人回复,所以存储下一个对象我们使用List来存储(对象数组也行)先查询last_id为null的数据,last_id为null则说明这是日记的第一层评论。查询list_id不为null的数据,last_id不为null则说明这是回复。通过last_id找到对应的评论,回复。添加到链表中。打印 4.实现

示例数据库数据

示例显示(在web中显示)

dao层使用的jpa.

实体类

1.Reply(评论实体类)

package com.ss.entity; import lombok.Data; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Data @Table(name = "reply") public class Reply { @Id @GeneratedValue private int id ; //回复id private Integer diaryId; //日记id private String text; //回复内容 private Integer userId; //用户id private Integer lastId; //上一条回复id }

2.Node(链表结构)

package com.ss.entity; import lombok.Data; import java.util.ArrayList; import java.util.List; @Data public class Node { public Node(){ } /** * 构造函数 * @param reply */ public Node(Reply reply){ this.id = reply.getId(); this.diaryId = reply.getDiaryId(); this.lastId = reply.getLastId(); this.text = reply.getText(); this.userId = reply.getUserId(); } private int id ; private Integer diaryId; private String text; private Integer userId; private Integer lastId; //下一条回复 private List nextNodes = new ArrayList(); /** * 将单个node添加到链表中 * @param list * @param node * @return */ public static boolean addNode(List list,Node node){ for (Node node1 : list) { //循环添加 if (node1.getId()==node.getLastId()){ //判断留言的上一段是都是这条留言 node1.getNextNodes().add(node); //是,添加,返回true; System.out.println("添加了一个"); return true; }else{ //否则递归继续判断 if (node1.getNextNodes().size()!=0) if (Node.addNode(node1.getNextNodes(),node)){ return true; } } } return false; } /** * 将查出来的lastId不为null的回复都添加到第一层Node集合中 * @param firstList * @param thenList * @return */ public static List addAllNode(List firstList,List thenList){ while (thenList.size()!=0){ int size = thenList.size(); for (int i = 0;i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有