Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • 工程准备
  • 文章
    • 热门文章
    • 分类文章列表
    • 根据目录分页文章列表
    • 文章详情
  • 登录校验
  • 评论
  • 个人信息
  • 文章浏览次数
  • 个人博客
Nreal
2023-12-18
目录

文章

# 热门文章

需求:按浏览量排序,展示文章标题和浏览量,只展示正式文章

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

接口:

@GetMapping("/hotArticleList")
@ApiOperation(value = "热门文章")
public ResponseResult hotArticleList(){
    ResponseResult result = articleService.hotArticleList();
    return result;
}
1
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

# 分类文章列表

VO:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CategoryVo {
    private Long id;
    private String name;
}
1
2
3
4
5
6
7

接口:

@ApiOperation(value = "文章分类列表")
@GetMapping("/getCategoryList")
public ResponseResult getCategoryList() {
    return categoryService.getCategoryList();
}
1
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

# 根据目录分页文章列表

在实体类Artilce中添加不存在的数据库字段categoryName;

@Accessors(chain = true)

@TableField(exist = false)
private String categoryName;
1
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
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageVo {
    private List rows;
    private Long total;
}
1
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
@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

# 文章详情

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

接口:

@GetMapping("/{id}")
@ApiOperation(value = "文章详情")
public ResponseResult getArticleDetail(@PathVariable("id") Long id){
    return articleService.getArticleDetail(id);
}
1
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
工程准备
登录校验

← 工程准备 登录校验→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式