============= 后端_最新文章 ============= 每篇文章都有一个创建时间,以之表示文章的新鲜度。 1 文章服务 1.1 文章服务接口 /ysdblog-api/src/main/java/com/weihome/ysdblog/service/ArticleService.java: public interface ArticleService { ... /** * 查询最新文章 */ public Result newest(int limit); ... } 1.2 文章服务实现 SQL> select id, title from t_article order by create_date desc limit 5 /ysdblog-api/src/main/java/com/weihome/ysdblog/service/impl/ArticleServiceImpl.java: ... public class ArticleServiceImpl implements ArticleService { ... @Override public Result newest(int limit) { LambdaQueryWrapper
queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.orderByDesc(Article::getCreateDate); 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("newest") public Result newest() { return articleService.newest(5); } ... } 3 运行测试 Postman GET localhost:8888/article/newest ------------------------------------- { "success": true, "code": 200, "msg": "success", "data": [ { "id": "1511577630850203650", "commentCount": null, "createDate": "2022-04-06 13:40:34", "summary": null, "title": "title_b", "viewCount": null, "weight": null, "author": null, "content": null, "category": null, "tags": null }, { "id": "1511577146642972674", "commentCount": null, "createDate": "2022-04-06 13:40:34", "summary": null, "title": "title_a", "viewCount": null, "weight": null, "author": null, "content": null, "category": null, "tags": null } ] }