文章
# 热门文章
需求:按浏览量排序,展示文章标题和浏览量,只展示正式文章
VO:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class HotArticleVo {
private Long id;
//标题
private String title;
//访问量
private Long viewCount;
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
接口:
@GetMapping("/hotArticleList")
@ApiOperation(value = "热门文章")
public ResponseResult hotArticleList(){
ResponseResult result = articleService.hotArticleList();
return result;
}
1
2
3
4
5
6
2
3
4
5
6
@Override
public ResponseResult hotArticleList() {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL)
.orderByDesc(Article::getViewCount);
Page<Article> page = new Page(1, 10);
page(page,queryWrapper);
List<Article> articles = page.getRecords();
List<HotArticleVo> hotArticleVos = BeanCopyUtils.copyBeanList(articles, HotArticleVo.class);
return ResponseResult.okResult(hotArticleVos);
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 分类文章列表
VO:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CategoryVo {
private Long id;
private String name;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
接口:
@ApiOperation(value = "文章分类列表")
@GetMapping("/getCategoryList")
public ResponseResult getCategoryList() {
return categoryService.getCategoryList();
}
1
2
3
4
5
2
3
4
5
Article中有categoryId属性;
@Override
public ResponseResult getCategoryList() {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
List<Article> articleList = articleService.list(queryWrapper);
Set<Long> CategoryIds = articleList.stream().map(article -> article.getCategoryId()).collect(Collectors.toSet());
List<Category> categories = listByIds(CategoryIds);
categories = categories.stream()
.filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
.collect(Collectors.toList());
List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
return ResponseResult.okResult(categoryVos);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 根据目录分页文章列表
在实体类Artilce中添加不存在的数据库字段categoryName;
@Accessors(chain = true)
@TableField(exist = false)
private String categoryName;
1
2
3
4
2
3
4
VO:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ArticleListVo {
private Long id;
//标题
private String title;
//文章摘要
private String summary;
//所属分类名
private String categoryName;
//缩略图
private String thumbnail;
//访问量
private Long viewCount;
private Date createTime;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageVo {
private List rows;
private Long total;
}
1
2
3
4
5
6
7
2
3
4
5
6
7
接口:
@GetMapping("/articleList/{pageNum}/{pageSize}/{categoryId}")
@ApiOperation(value = "文章分页展示")
public ResponseResult articleList(@PathVariable Integer pageNum,@PathVariable Integer pageSize,@PathVariable("categoryId") Long categoryId){
return articleService.articleList(pageNum,pageSize,categoryId);
}
1
2
3
4
5
2
3
4
5
@Autowired
private CategoryService categoryService;
@Override
public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Objects.nonNull(categoryId) && categoryId>0,Article::getCategoryId,categoryId)
.eq(Article::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL)
.orderByDesc(Article::getIsTop);
Page<Article> page = new Page<>(pageNum, pageSize);
page(page,queryWrapper);
List<Article> articles = page.getRecords();
// artilce表中没有categoryName字段
articles.stream().map(article->article.setCategoryName(categoryService.getById(article.getCategoryId()).getName()))
.collect(Collectors.toList());
// 封装VO
List<ArticleListVo> articleListVos = BeanCopyUtils.copyBeanList(articles,ArticleListVo.class);
PageVo pageVo = new PageVo(articleListVos, page.getTotal());
return ResponseResult.okResult(pageVo);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 文章详情
VO:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ArticleDetailVo {
private Long id;
private String title;
private String summary;
private Long categoryId;
private String categoryName;
private String thumbnail;
private Long viewCount;
private Date createTime;
private String content;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
接口:
@GetMapping("/{id}")
@ApiOperation(value = "文章详情")
public ResponseResult getArticleDetail(@PathVariable("id") Long id){
return articleService.getArticleDetail(id);
}
1
2
3
4
5
2
3
4
5
@Override
public ResponseResult getArticleDetail(Long id) {
Article article = getById(id);
ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(article, ArticleDetailVo.class);
//添加 categroyName
Long categoryId = articleDetailVo.getCategoryId();
Category category = categoryService.getById(categoryId);
if(category!=null){
articleDetailVo.setCategoryName(category.getName());
}
return ResponseResult.okResult(articleDetailVo);
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12