============= 后端_文章列表 ============= 1 用户服务 1.1 用户服务接口 /ysdblog-api/src/main/java/com/weihome/ysdblog/service/UserService.java: public interface UserService { /** * 根据用户ID查询用户 */ public User findUserById(Long id); } 1.2 用户服务实现 SQL> select * from t_user where id = 1 /ysdblog-api/src/main/java/com/weihome/ysdblog/service/impl/UserServiceImpl.java: @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User findUserById(Long id) { return userMapper.selectById(id); } } 2 标签数据访问 2.1 标签数据访问接口 /ysdblog-api/src/main/java/com/weihome/ysdblog/dao/mapper/TagMapper.java: public interface TagMapper extends BaseMapper { /** * 根据文章ID查询标签列表 */ public List findTagsByArticleId(Long articleId); } 2.2 标签数据访问实现 SQL> select id, tag_name from t_tag where id in (select tag_id from t_article_tag where article_id = 1) /ysdblog-api/src/main/resources/com/weihome/ysdblog/dao/mapper/TagMapper.xml: 3 标签服务 3.1 标签服务接口 /src/main/java/com/weihome/ysdblog/service/TagService.java: public interface TagService { /** * 根据文章ID查询标签列表 */ public List findTagsByArticleId(Long articleId); } 3.2 标签服务实现 /src/main/java/com/weihome/ysdblog/service/impl/TagServiceImpl.java: @Service public class TagServiceImpl implements TagService { @Autowired private TagMapper tagMapper; @Override public List findTagsByArticleId(Long articleId) { List tags = tagMapper.findTagsByArticleId(articleId); return copyList(tags); } private List copyList(List tags) { List tagVos = new ArrayList<>(); for (Tag tag: tags) tagVos.add(copy(tag)); return tagVos; } private TagVo copy(Tag tag) { TagVo tagVo = new TagVo(); BeanUtils.copyProperties(tag, tagVo); return tagVo; } } 4 文章服务 4.1 文章服务接口 /ysdblog-api/src/main/java/com/weihome/ysdblog/service/ArticleService.java: public interface ArticleService { /** * 分页查询文章列表 */ public Result list(PageParam pageParam); } 4.2 文章服务实现 SQL> select * from t_article order by weight desc, create_date desc /ysdblog-api/src/main/java/com/weihome/ysdblog/service/impl/ArticleServiceImpl.java: @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleMapper articleMapper; @Autowired private UserService userService; @Autowired private TagService tagService; @Override public Result list(PageParam pageParam) { Page
page = new Page<>(pageParam.getPage(), pageParam.getPageSize()); LambdaQueryWrapper
queryWrapper = new LambdaQueryWrapper<>(); // 按权重降序,权重相同按创建时间降序 queryWrapper.orderByDesc( Article::getWeight, Article::getCreateDate); page = articleMapper.selectPage(page, queryWrapper); List
articles = page.getRecords(); return Result.success(copyList(articles, true, true)); } private List copyList( List
articles, boolean author, boolean tags) { List articleVos = new ArrayList<>(); for (Article article: articles) articleVos.add(copy(article, author, tags)); return articleVos; } private ArticleVo copy( Article article, boolean author, boolean tags) { ArticleVo articleVo = new ArticleVo(); BeanUtils.copyProperties(article, articleVo); articleVo.setCreateDate(new DateTime(article.getCreateDate()) .toString("yyyy-MM-dd HH:mm:ss")); if (author) articleVo.setAuthor(userService.findUserById( article.getAuthorId()).getNickname()); if (tags) articleVo.setTags(tagService.findTagsByArticleId( article.getId())); return articleVo; } } 5 文章控制器 /ysdblog-api/src/main/java/com/weihome/ysdblog/controller/ArticleController.java: @RestController @RequestMapping("article") public class ArticleController { @Autowired private ArticleService articleService; /** * 分页查询文章列表 */ @PostMapping("list") public Result list(@RequestBody PageParam pageParam) { return articleService.list(pageParam); } } 6 运行测试 Postman POST localhost:8888/article/list Body { "page": "1", "pageSize": "10" } ------------------------------------------------ { "success": true, "code": 200, "msg": "success", "data": [ { "id": "1511577630850203650", "commentCount": 0, "createDate": "2022-04-06 13:32:45", "summary": "summary_b", "title": "title_b", "viewCount": 0, "weight": 0, "author": "nickname_a", "content": null, "category": null, "tags": [ { "id": 1, "tagName": "tag_a" }, { "id": 2, "tagName": "tag_b" } ] }, { "id": "1511577146642972674", "commentCount": 0, "createDate": "2022-04-06 13:30:50", "summary": "summary_a", "title": "title_a", "viewCount": 1, "weight": 0, "author": "nickname_a", "content": null, "category": null, "tags": [ { "id": 1, "tagName": "tag_a" }, { "id": 2, "tagName": "tag_b" } ] } ] }