最近在学习Spring Boot,顺便做一个简单的问答系统来实践下,用来巩固加深知识的理解。
需求确定
参考知乎分析一下我们简单问答系统需要的功能:
- 用户基本信息需要
- 系统或者好友发送/接收的消息需要
- 提问功能需要
- 评论功能需要
- 搜索功能需要
- 内容的话后期考虑爬虫爬取
建立数据库:
根据我们的需求建立相关的数据库,
- 用户表
- 站内信
- 问题
- 评论
搜索功能准备用全文搜索服务器来做,就不建表了
用户(User)
属性 | 类型 | 含义 |
---|---|---|
id | int | id号 |
name | varchar | 用户姓名 |
password | varchar | 用户密码 |
salt | varchar | 密码盐值 |
head_url | varchar | 用户头像 |
站内信(Message)
属性 | 类型 | 含义 |
---|---|---|
id | int | id号 |
fromid | int | 发送者id |
toid | int | 接受者id |
content | text | 站内信内容 |
conversation_id | int | 对话的id号 |
created_date | datetime | 创建时间 |
hasRead | int | 是否已读 |
问题(Question)
属性 | 类型 | 含义 |
---|---|---|
id | int | id号 |
title | varchar | 问题标题 |
content | text | 问题内容 |
user_id | int | 提问用户id |
created_date | datetime | 创建时间 |
comment_count | int | 评论数量 |
评论(Comment)
属性 | 类型 | 含义 |
---|---|---|
id | id号 | |
content | text | 评论内容 |
user_id | int | 评论者id |
created_date | datetime | 评论时间 |
entity_id | int | 实体id |
entity_type | int | 实体类型 |
status | int | 评论状态 |
DROP TABLE IF EXISTS `question`;
CREATE TABLE `question` (
`id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`content` TEXT NULL,
`user_id` INT NOT NULL,
`created_date` DATETIME NOT NULL,
`comment_count` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `date_index` (`created_date` ASC));
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL DEFAULT '',
`password` varchar(128) NOT NULL DEFAULT '',
`salt` varchar(32) NOT NULL DEFAULT '',
`head_url` varchar(256) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment` (
`id` INT NOT NULL AUTO_INCREMENT,
`content` TEXT NOT NULL,
`user_id` INT NOT NULL,
`entity_id` INT NOT NULL,
`entity_type` INT NOT NULL,
`created_date` DATETIME NOT NULL,
`status` INT NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
INDEX `entity_index` (`entity_id` ASC, `entity_type` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `message`;
CREATE TABLE `message` (
`id` INT NOT NULL AUTO_INCREMENT,
`from_id` INT NULL,
`to_id` INT NULL,
`content` TEXT NULL,
`created_date` DATETIME NULL,
`has_read` INT NULL,
`conversation_id` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
INDEX `conversation_index` (`conversation_id` ASC),
INDEX `created_date` (`created_date` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;