博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
说说,博客,贴吧评论设计问题
阅读量:4230 次
发布时间:2019-05-26

本文共 2620 字,大约阅读时间需要 8 分钟。

说说,博客,贴吧的评论都是可以无限评论的,也就是说,你根本不知道一共有多少层,如果要设计这么一个结构,怎么保存数据,查询显示数据呢?

– – 创建说说表

– create table saysay(
– id int unsigned auto_increment primary key,
– owner varchar(20) not null,
– sendtime datetime,
– content text
– );
评论表(comments)
id:主键、无意义(id 为0 代表一级评论)
owner:该评论的发出者
toname: 该评论是回复哪个人的
sendtime:评论的时间
content:评论的内容
ssid:是哪条说说的下面的评论
pid:一级评论的id

我们可以这样考虑,先按ssid,从数据库中查出该条说说的所有相关评论,然后在对这些说说进行分层,得到每条评论具体是评论哪个内容的,就是在说说里面放一个评论的list(对应直接评论),在每个评论里面又放一个list(回复该评论的评论),如果list为空,那么就结束了。

下面我写了一个demo,简化版的,但是原理类似:

public class Main {    public static void main(String[] args) {        // *表示一级评论 *-*表示二级评论 *-*-*表示三级评论 *-*-*-*表示四级评论        List
list = new ArrayList<>(Arrays.asList( new Context("", "1"),new Context("", "2"), new Context("", "3"), new Context("1", "1-1.1"), new Context("1", "1-1.3"), new Context("1", "1-1.4"), new Context("1-1.1", "1-1.1-1.1.1"), new Context("1-1.1", "1-1.1-1.1.5"), new Context("2", "2-2.1"), new Context("2-2.1", "2-2.1-2.1.2"), new Context("3", "3-3.1"), new Context("3", "3-3.2"), new Context("3-3.2", "3-3.2-3.2.2"), new Context("3-3.2-3.2.2", "3-3.2-3.2.2-3.2.2.2"))); List
resultList = getContextList(list); resultList.forEach(System.out::println); } private static List
getContextList(List
list) { List
resultList = new LinkedList<>(); list.forEach(x -> { if (x.getBefore().equals("")) { resultList.add(new Context(x, list)); } }); return resultList; }}class Context { private String before;//上一级 private String value;//当前 private List
list = new LinkedList<>(); Context(String before, String value) { this.before = before; this.value = value; } Context(Context context, List
contextList) { this.before = context.getBefore(); this.value = context.getValue(); contextList.forEach(x -> { if (x.getBefore().equals(value)) { list.add(new Context(x, contextList)); } }); } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public List
getList() { return list; } public void setList(List
list) { this.list = list; } public String getBefore() { return before; } public void setBefore(String before) { this.before = before; } @Override public String toString() { return value + (list.size() == 0 ? "" : ":" + list); }}

结果:

1:[1-1.1:[1-1.1-1.1.1, 1-1.1-1.1.5], 1-1.3, 1-1.4]2:[2-2.1:[2-2.1-2.1.1, 2-2.1-2.1.2]]3:[3-3.1, 3-3.2:[3-3.2-3.2.2:[3-3.2-3.2.2-3.2.2.2]]]

可以发现,已经达到了预期的效果。

转载地址:http://pqjqi.baihongyu.com/

你可能感兴趣的文章
Database Programming with Visual Basic® .NET and ADO.NET: Tips, Tutorials, and Code
查看>>
Visual Basic 2005 Express: Now Playing
查看>>
Jakarta Struts Cookbook
查看>>
AspectJ Cookbook
查看>>
IntelliJ IDEA in Action
查看>>
HTML Professional Projects
查看>>
Python Cookbook, Second Edition
查看>>
Java Extreme Programming Cookbook
查看>>
XSLT Cookbook
查看>>
Java Programming with Oracle JDBC
查看>>
Hack Proofing Your Network (Second Edition)
查看>>
XML Programming (Core Reference)
查看>>
Visual Studio .NET: The .NET Framework Black Book
查看>>
Best Kept Secrets in .NET
查看>>
SQL: The Complete Reference
查看>>
Wireless Network Hacks & Mods For Dummies
查看>>
Programming INDIGO
查看>>
.NET Development Security Solutions
查看>>
3ds Max 8 Bible
查看>>
Hack Proofing Your Web Applications
查看>>