前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >7.19 SpringBoot项目实战【学生详情】:学生信息 + 申请记录

7.19 SpringBoot项目实战【学生详情】:学生信息 + 申请记录

作者头像
天罡gg
发布2023-10-22 09:07:43
1950
发布2023-10-22 09:07:43
举报
文章被收录于专栏:天罡gg天罡gg

前言

通过上文 我们实现了从学生列表执行:学生入驻审核,但通常审核都不是盲审,都需要打开学生详情页,查看学生基本信息,以及申请记录,再来审核!

另外还有一个点,学生列表可还包括审核通过的学生,也需要学生详情页,还包括像学生借阅记录等等,所以后端通常提供多个细粒度的API,方便前端的页面实现。

所以,本文我们实战两个接口:查看学生信息,以及查看学生申请记录,分别实现服务层、数据访问层、控制器层,,并把我们学习过的知识点串连应用起来,知识点包括:参数校验Validation管理员权限校验API接口定义Mybatis查询拷贝工具类等。

一、编写服务层

StudentService方法定义 (其它方法省略了):

代码语言:javascript
复制
public interface StudentService {
    /**
     * 获取学生信息(根据学生id)
     * @param studentId 学生id
     * **/
    StudentBO getStudentById(Integer studentId);

    /**
     * 获取学生申请借阅证记录
     * @param studentId 学生id
     * **/
    List<QualificationBO> getQualificationList(Integer studentId);
}

看注释足够清楚了~

1. 获取学生信息(根据学生id)

代码如下,通过selectByPrimaryKey 获取学生信息,最后通过CopyUtils.copy拷贝到StudentBO

代码语言:javascript
复制
@Override
public StudentBO getStudentById(Integer studentId) {
    Student po = studentMapper.selectByPrimaryKey(studentId);
    return CopyUtils.copy(po, StudentBO::new);
}

2. 获取学生申请借阅证记录

代码如下,我这里使用selectByExample 查询所有字段,当然你也可以使用XML方式,最后通过CopyUtils.copyList拷贝到QualificationBO

代码语言:javascript
复制
@Override
public List<QualificationBO> getQualificationList(Integer studentId) {
    QualificationExample example = new QualificationExample();
    example.createCriteria().andStudentIdEqualTo(studentId);
    List<Qualification> qualificationList = qualificationMapper.selectByExample(example);
    return CopyUtils.copyList(qualificationList, QualificationBO::new);
}

QualificationBO 定义如下:

代码语言:javascript
复制
@Data
public class QualificationBO implements Serializable {
    private Integer id;
    private Integer studentId;
    private Integer status;
    private Date verifyTime;
    private Integer verifyUserId;
    private String rejectReason;
    private Date gmtCreate;
    private Date gmtModified;
}

二、编写控制器

StudentController接口定义 (其它方法省略了):

代码语言:javascript
复制
@Role
@GetMapping("/id")
public TgResult<StudentBO> getStudentById(@Min(value = 1, message = "id必须大于0") @RequestParam("studentId") Integer studentId) {
    StudentBO studentBO = studentService.getStudentById(studentId);
    return TgResult.ok(studentBO);
}

@Role
@GetMapping("/qualification/list")
public TgResult getStudentQualificationList(@Min(value = 1, message = "id必须大于0") @RequestParam("studentId") Integer studentId) {
    List<QualificationBO> qualificationList = studentService.getQualificationList(studentId);
    return TgResult.ok(qualificationList);
}

说明如下:

  • @Role注解

是我们自定义的注解,用于管理员权限校验,当不是管理员调用该API时,会返回403 无权限。 共通过两种方式实现:

  1. 通过拦截器Interceptor的实现:7.5 SpringBoot 拦截器Interceptor实战 统一角色权限校验
  2. 通过@Aspect AOP实现:7.6 SpringBoot AOP实战 统一角色权限校验
  3. @GetMapping(“/id”)

GET请求的标准方式,加上Controller上的路径,完整接口路径为:/student/id

  • @GetMapping(“/qualification/list”)

GET请求的标准方式,加上Controller上的路径,完整接口路径为:/student/qualification/list

  • @Min(value = 1, message = “id必须大于0”)

Spring参数校验Validation,@Min最小值:7.13 在SpringBoot中 正确使用Validation实现参数效验

  • TgResult

封装的统一返回结果 详见:2-2. SpringBoot API开发详解 --SpringMVC注解+封装结果+支持跨域+打包

三、PostMan测试

  1. getStudentById 根据学生id查询学生信息 /student/id?studentId=2
  1. getStudentQualificationList 查询学生的申请记录 /student/qualification/list?studentId=2

最后

看到这,觉得有帮助的,刷波666,感谢大家的支持~

想要看更多实战好文章,还是给大家推荐我的实战专栏–>《基于SpringBoot+SpringCloud+Vue前后端分离项目实战》,由我和 前端狗哥 合力打造的一款专栏,可以让你从0到1快速拥有企业级规范的项目实战经验!

具体的优势、规划、技术选型都可以在《开篇》试读!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-10-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 一、编写服务层
    • 1. 获取学生信息(根据学生id)
      • 2. 获取学生申请借阅证记录
      • 二、编写控制器
      • 三、PostMan测试
      • 最后
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档