目录
最热文章
接口说明
接口url:/articles/hot
请求方式:POST
请求参数:无
返回数据:
{
"success": true,
"code": 200,
"msg": "success",
"data": [
{
"id": 1,
"title": "springboot介绍以及入门案例",
},
{
"id": 9,
"title": "Vue.js 是什么",
},
{
"id": 10,
"title": "Element相关",
}
]
}
编码实现
配置类application.properties
# spring读取配置文件的数据:@Value("${tag.hot.limit}")
#设置tag最热标签个数
tag.hot.limit=6
#设置Article最热文章个数
article.hotAndNew.limit=6
Controller控制层
ArticleController:
/**
* 最热文章
* @return
*/
@PostMapping("hot")
public Result hotArticle(){
return articleService.hotArticle(limit);
}
Service业务逻辑层
接口ArticleService:
/**
* 查询前limit个最热文章
* @param limit
* @return
*/
Result hotArticle(int limit);
实现类ArticleServiceImpl:
@Override
public Result hotArticle(int limit) {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByDesc(Article::getViewCounts);
queryWrapper.select(Article::getId, Article::getTitle);
queryWrapper.last("limit " + limit);
List<Article> articles = articleMapper.selectList(queryWrapper);
return Result.success(copyList(articles,false,false));
}
前端测试
最新文章
接口说明
接口url:/articles/new
请求方式:POST
请求参数:无
返回数据:
{
"success": true,
"code": 200,
"msg": "success",
"data": [
{
"id": 1,
"title": "springboot介绍以及入门案例",
},
{
"id": 9,
"title": "Vue.js 是什么",
},
{
"id": 10,
"title": "Element相关",
}
]
}
编码实现
Controller控制层
ArticleController
/**
* 最新文章
* @return
*/
@PostMapping("new")
public Result newArticle(){
return articleService.newArticle(limit);
}
Service业务逻辑层
ArticleService接口:
/**
* 最新文章
* @param limit
* @return
*/
Result newArticle(int limit);
ArticleServiceImpl实现类:
@Override
public Result newArticle(int limit) {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(Article::getId,Article::getTitle);
queryWrapper.orderByDesc(Article::getCreateDate);
queryWrapper.last("limit " + limit);
List<Article> articles = articleMapper.selectList(queryWrapper);
return Result.success(copyList(articles,false,false));
}