============= 后端_文章详情 ============= 用户点击首页文章列表中的文章标题,即显示文章详情。 _____________________________________________________________________ | | | | | 阅读数 | 评论数 | | |________|________|___________________________________________________| | | | | | | | | | 文章内容 | | | | | | | |_____________________________________________________________________| | | | 标签 | |_____________________________________________________________________| | | | 分类 | |_____________________________________________________________________| | | | 评论列表 | |_____________________________________________________________________| 1 数据库表 1.1 内容表 CREATE TABLE `ysdblogdb`.`t_content` ( `id` bigint(0) NOT NULL AUTO_INCREMENT, `content` longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL, `content_html` longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL, `article_id` bigint(0) NOT NULL, PRIMARY KEY (`id`) USING BTREE, INDEX `article_id`(`article_id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 38 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; 1.2 分类表 CREATE TABLE `ysdblogdb`.`t_category` ( `id` bigint(0) NOT NULL AUTO_INCREMENT, `avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, `category_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; 2 实体类 2.1 内容实体类 /ysdblog-api/src/main/java/com/weihome/ysdblog/dao/pojo/Content.java: @Data public class Content { private Long id; private String content; private String contentHtml; private Long articleId; } 2.2 分类实体类 /ysdblog-api/src/main/java/com/weihome/ysdblog/dao/pojo/Category.java: @Data public class Category { private Long id; private String avatar; private String categoryName; private String description; } 3 数据访问接口 3.1 内容数据访问接口 /ysdblog-api/src/main/java/com/weihome/ysdblog/dao/mapper/ContentMapper.java: public interface ContentMapper extends BaseMapper { } 3.2 分类数据访问接口 /ysdblog-api/src/main/java/com/weihome/ysdblog/dao/mapper/CategoryMapper.java: public interface CategoryMapper extends BaseMapper { } 4 视图对象 4.1 内容视图对象 /ysdblog-api/src/main/java/com/weihome/ysdblog/vo/ContentVo.java: @Data public class ContentVo { private String content; } 4.2 分类视图对象 /ysdblog-api/src/main/java/com/weihome/ysdblog/vo/CategoryVo.java: @Data public class CategoryVo { private Long id; private String avatar; private String categoryName; } 4.3 文章视图对象 /ysdblog/src/main/java/com/weihome/ysdblog/vo/ArticleVo.java: ... public class ArticleVo { ... private ContentVo content; private CategoryVo category; ... } 5 内容服务 5.1 内容服务接口 /ysdblog-api/src/main/java/com/weihome/ysdblog/service/ContentService.java: public interface ContentService { /** * 根据内容ID查询内容 */ public Content findContentById(Long id); } 5.2 内容服务实现 /ysdblog-api/src/main/java/com/weihome/ysdblog/service/impl/ContentServiceImpl.java: @Service public class ContentServiceImpl implements ContentService { @Autowired private ContentMapper contentMapper; @Override public Content findContentById(Long id) { return contentMapper.selectById(id); } } 6 分类服务 6.1 分类服务接口 /ysdblog-api/src/main/java/com/weihome/ysdblog/service/CategoryService.java: public interface CategoryService { /** * 根据分类ID查询分类 */ public Category findCategoryById(Long id); } 6.2 分类服务实现 /ysdblog-api/src/main/java/com/weihome/ysdblog/service/impl/CategoryServiceImpl.java: @Service public class CategoryServiceImpl implements CategoryService { @Autowired private CategoryMapper categoryMapper; @Override public Category findCategoryById(Long id) { return categoryMapper.selectById(id); } } 7 文章服务 7.1 文章服务接口 /ysdblog-api/src/main/java/com/weihome/ysdblog/service/ArticleService.java: public interface ArticleService { ... /** * 查询文章详情 */ public Result details(Long articleId); ... } 7.2 文章服务实现 /ysdblog-api/src/main/java/com/weihome/ysdblog/service/impl/ArticleServiceImpl.java: ... public class ArticleServiceImpl implements ArticleService { ... @Autowired private ContentService contentService; @Autowired private CategoryService categoryService; ... @Override public Result details(Long articleId) { Article article = articleMapper.selectById(articleId); ArticleVo articleVo = copy(article, true, true, true, true); return Result.success(articleVo); } ... private ArticleVo copy( Article article, boolean author, boolean content, boolean category, boolean tags) { ... if (content) { ContentVo contentVo = new ContentVo(); BeanUtils.copyProperties( contentService.findContentById( article.getContentId()), contentVo); articleVo.setContent(contentVo); } if (category) { CategoryVo categoryVo = new CategoryVo(); BeanUtils.copyProperties( categoryService.findCategoryById( article.getCategoryId()), categoryVo); articleVo.setCategory(categoryVo); } ... } ... } 8 文章控制器 /ysdblog-api/src/main/java/com/weihome/ysdblog/controller/ArticleController.java: ... public class ArticleController { ... /** * 查询文章详情 */ @GetMapping("details/{id}") public Result details(@PathVariable("id") Long articleId) { return articleService.details(articleId); } ... } 9 运行测试 Postman GET localhost:8888/article/details/1511577146642972674 ---------------------------------------------------------- { "success": true, "code": 200, "msg": "success", "data": { "id": "1511577146642972674", "commentCount": 0, "createDate": "2022-04-06 13:30:50", "summary": "summary_a", "title": "title_a", "viewCount": 0, "weight": 0, "author": "nickname_f", "content": { "content": "content_a" }, "category": { "id": 1, "avatar": "category_a.png", "categoryName": "category_a" }, "tags": [ { "id": 1, "tagName": "tag_a" }, { "id": 2, "tagName": "tag_b" } ] } }