当前位置:   article > 正文

springboot的controller无法访问_springboot的controller层无法访问

springboot的controller层无法访问

今天写数据库大作业的时候发现新添的controller不起作用

 

代码如下

  1. package com.example.controller.controllers;
  2. import com.example.controller.util.R;
  3. import com.example.service.StudentService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.CrossOrigin;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. @RestController
  10. @CrossOrigin(origins = "http://localhost:5173")
  11. @RequestMapping("/students")
  12. public class StudentController {
  13. @Autowired
  14. private StudentService studentService;
  15. @GetMapping
  16. public R getAll(){
  17. return new R(studentService.list());
  18. }
  19. @GetMapping("/size")
  20. public R getSize() {
  21. return new R(studentService.list().size());
  22. }
  23. }

在浏览器中输入地址后返回结果

 排查后发现是实体类的命名问题

比如这里的studentNumber,mybatisplus会在表格寻找student_number列而不是studentNumber

这里发生了转换,导致实体类和数据表不匹配。

  1. package com.example.domain;
  2. import com.baomidou.mybatisplus.annotation.TableField;
  3. import com.baomidou.mybatisplus.annotation.TableId;
  4. import com.baomidou.mybatisplus.annotation.TableName;
  5. import lombok.Data;
  6. @Data
  7. @TableName("students_borrow")
  8. public class Student {
  9. @TableId
  10. // @TableField("studentNumber")
  11. private Integer studentNumber;
  12. // @TableField("name")
  13. private String name;
  14. // @TableField("studentName")
  15. private String studentName;
  16. // @TableField("sex")
  17. private String sex;
  18. // @TableField("institution")
  19. private String institution;
  20. // @TableField("phone")
  21. private String phone;
  22. }

这里我把数据表的列属性改成student_number就成功运行了

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/946701
推荐阅读
相关标签
  

闽ICP备14008679号