目录
一、数据库表准备
1.文章表
sql
CREATE TABLE `yan_article` (
`id` bigint(20) NOT NULL,
`summary` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '简介',
`title` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '标题',
`view_counts` int(11) DEFAULT NULL COMMENT '浏览数量',
`status` int(11) DEFAULT NULL COMMENT '文章审核',
`weight` int(11) NOT NULL COMMENT '是否置顶',
`author_id` bigint(20) DEFAULT NULL COMMENT '作者id',
`body_id` bigint(20) DEFAULT NULL COMMENT '内容id',
`activity_id` bigint(20) DEFAULT NULL COMMENT '活动ID',
`category_id` int(11) DEFAULT NULL COMMENT '类别id',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
2.用户表
sql
CREATE TABLE `yan_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`detail_id` bigint(20) DEFAULT NULL COMMENT '用户细节ID',
`username` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名',
`password` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '密码',
`email` varchar(100) DEFAULT '' COMMENT '用户邮箱',
`login_date` datetime DEFAULT NULL COMMENT '最后登录时间',
`status` int(1) DEFAULT '0' COMMENT '帐号状态(0正常 1停用)',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`),
KEY `idx_id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=108 DEFAULT CHARSET=utf8;
3.用户详细信息表
sql
CREATE TABLE `yan_details` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '个人信息ID',
`full_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '真实姓名',
`gender` enum('男','女') CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '性别',
`birth_date` date DEFAULT NULL COMMENT '出生日期',
`phone` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '手机号码',
`address_id` bigint(20) DEFAULT NULL COMMENT '居住地址',
`avatar` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '头像',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=106 DEFAULT CHARSET=utf8;
4.用户点赞表
sql
CREATE TABLE `yan_like` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) DEFAULT NULL COMMENT '用户ID',
`article_id` bigint(20) DEFAULT NULL COMMENT '文章ID',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
5.用户评论表
sql
CREATE TABLE `yan_comment` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`content` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`create_date` datetime DEFAULT NULL,
`article_id` bigint(20) NOT NULL,
`author_id` bigint(20) NOT NULL,
`parent_id` bigint(20) NOT NULL,
`to_uid` bigint(20) NOT NULL,
`level` varchar(1) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`remark` varchar(500) DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`id`) USING BTREE,
KEY `article_id` (`article_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1405564731300831234 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
二、需求描述
将五张表联合起来,得到文章对应的用户信息,以及文章对应的点赞总数和评论总数。
三、返回实体类
java
package com.minster.yanapi.response;
import lombok.Data;
import java.util.Date;
@Data
public class ArticleResp {
private Long id;
private String username;
private String avatar;
private Date createTime;
private String title;
private String summary;
private Long likeCount;
private Long commentCount;
}