============= 后端_最热文章 ============= 每篇文章都有一个阅读数,以之表示文章的热度。 1 文章服务 1.1 文章服务接口 /ysdblog-api/src/main/java/com/weihome/ysdblog/service/ArticleService.java: public interface ArticleService { ... /** * 查询最热文章 */ public Result hottest(int limit); ... } 1.2 文章服务实现 SQL> select id, title from t_article order by view_count desc limit 5 /ysdblog-api/src/main/java/com/weihome/ysdblog/service/impl/ArticleServiceImpl.java: ... public class ArticleServiceImpl implements ArticleService { ... @Override public Result hottest(int limit) { LambdaQueryWrapper
queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.orderByDesc(Article::getViewCount); queryWrapper.select(Article::getId, Article::getTitle) .last("limit " + limit); List
articles = articleMapper.selectList(queryWrapper); return Result.success(copyList(articles, false, false)); } ... } 2 文章控制器 /ysdblog-api/src/main/java/com/weihome/ysdblog/controller/ArticleController.java: ... public class ArticleController { ... /** * 查询最热文章 */ @GetMapping("hottest") public Result hottest() { return articleService.hottest(5); } ... } 3 运行测试 Postman GET localhost:8888/article/hottest -------------------------------------- { "success": true, "code": 200, "msg": "success", "data": [ { "id": "1511577146642972674", "commentCount": null, "createDate": "2022-04-06 13:39:53", "summary": null, "title": "title_a", "viewCount": null, "weight": null, "author": null, "content": null, "category": null, "tags": null }, { "id": "1511577630850203650", "commentCount": null, "createDate": "2022-04-06 13:39:53", "summary": null, "title": "title_b", "viewCount": null, "weight": null, "author": null, "content": null, "category": null, "tags": null } ] }