不管是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; //对加了@Controller注解的方法进行拦截处理 AOP的实现 @ControllerAdvice public class AllExceptionHandler { //进行异常处理,处理Exception.class的异常 @ExceptionHandler(Exception.class) @ResponseBody //返回json数据 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 queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.select(Article::getId,Article::getTitle); queryWrapper.orderByDesc(Article::getViewCounts); queryWrapper.last("limit " + limit); //select id,title from article order by view_counts desc limit 5 List 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 queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.orderByDesc(Article::getCreateDate); queryWrapper.select(Article::getId,Article::getTitle); queryWrapper.last("limit "+limit); //select id,title from article order by create_date desc limit 5 List 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 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 { List listArchives(); }
持久层对应的xml文件
4.4 测试
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)