1. 统一异常处理
不管是controller层还是service,dao层,都有可能报异常,如果是预料中的异常,可以直接捕获处理,如果是意料之外的异常,需要统一进行处理,进行记录,并给用户提示相对比较友好的信息。
package com.mszlu.blog.handler;
import com.mszlu.blog.vo.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class AllExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
public Result doException(Exception ex){
ex.printStackTrace();
return Result.fail(-999,"系统异常");
}
}
2. 首页-最热文章
思路:根据观看浏览量多少排序
2.1 接口说明
接口url:/articles/hot
请求方式:POST
请求参数:
返回数据:
{
"success": true,
"code": 200,
"msg": "success",
"data": [
{
"id": 1,
"title": "springboot介绍以及入门案例",
},
{
"id": 9,
"title": "Vue.js 是什么",
},
{
"id": 10,
"title": "Element相关",
}
]
}
2.2 Controller
首页展示最热文章控制层编写
@PostMapping("/hot")
public result hostArticle(){
int limit = 5;
return articleService.hostArticle(limit);
}
2.3 Service
业务层接口于接口实现编写
Result hotArticle(int limit);
@Override
public result hostArticle(int limit) {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(Article::getId,Article::getTitle);
queryWrapper.orderByDesc(Article::getViewCounts);
queryWrapper.last("limit " + limit);
List<Article> articles = articleMapper.selectList(queryWrapper);
return result.success(copyList(articles,false,false));
}
2.4 测试
3. 首页-最新文章
思路:根据发表时间先后来排序
3.1 接口说明
接口url:/articles/new
请求方式:POST
请求参数:
返回数据:
{
"success": true,
"code": 200,
"msg": "success",
"data": [
{
"id": 1,
"title": "springboot介绍以及入门案例",
},
{
"id": 9,
"title": "Vue.js 是什么",
},
{
"id": 10,
"title": "Element相关",
}
]
}
3.1 Controller
控制层代码编写
@PostMapping("new")
public Result newArticles(){
int limit = 5;
return articleService.newArticles(limit);
}
3.2 Service
业务层接口和接口实现编写
Result newArticles(int limit);
@Override
public Result newArticles(int limit) {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByDesc(Article::getCreateDate);
queryWrapper.select(Article::getId,Article::getTitle);
queryWrapper.last("limit "+limit);
List<Article> articles = articleMapper.selectList(queryWrapper);
return Result.success(copyList(articles,false,false));
}
3.4 测试
4. 首页-文章归档
思路:文章归档即按照年份和月份来划分统计作者发表博客的篇数
4.1接口说明
接口url:/articles/listArchives
请求方式:POST
请求参数:
返回数据:
{
"success": true,
"code": 200,
"msg": "success",
"data": [
{
"year": "2021",
"month": "6",
"count": 2
}
]
}
?
4.1 Controller
控制层代码编写
@PostMapping("listArchives")
public Result listArchives(){
return articleService.listArchives();
}
在dao包下创建一个dos包创建Archives类用来服务于业务层充当泛型
package com.mszlu.blog.dao.dos;
import lombok.Data;
@Data
public class Archives {
private Integer year;
private Integer month;
private Integer count;
}
4.2 Service
业务层的接口和接口实现编写
Result listArchives();
@Override
public Result listArchives() {
List<Archives> archivesList = articleMapper.listArchives();
return Result.success(archivesList);
}
4.3 Dao
持久层接口代码编写
package com.mszlu.blog.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mszlu.blog.dao.pojo.Article;
import java.util.List;
import java.util.Map;
public interface ArticleMapper extends BaseMapper<Article> {
List<Archives> listArchives();
}
持久层对应的xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mszlu.blog.dao.mapper.ArticleMapper">
<select id="listArchives" resultType="com.mszlu.blog.dao.dos.Archives">
select year(create_date) as year,month(create_date) as month,count(*) as count from ms_article group by year,month
</select>
</mapper>
4.4 测试
|