今天写数据库大作业的时候发现新添的controller不起作用
代码如下
package com.example.controller.controllers;
import com.example.controller.util.R;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin(origins = "http://localhost:5173")
@RequestMapping("/students")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping
public R getAll(){
return new R(studentService.list());
}
@GetMapping("/size")
public R getSize() {
return new R(studentService.list().size());
}
}
在浏览器中输入地址后返回结果
排查后发现是实体类的命名问题
比如这里的studentNumber,mybatisplus会在表格寻找student_number列而不是studentNumber
这里发生了转换,导致实体类和数据表不匹配。
package com.example.domain;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("students_borrow")
public class Student {
@TableId
// @TableField("studentNumber")
private Integer studentNumber;
// @TableField("name")
private String name;
// @TableField("studentName")
private String studentName;
// @TableField("sex")
private String sex;
// @TableField("institution")
private String institution;
// @TableField("phone")
private String phone;
}
这里我把数据表的列属性改成student_number就成功运行了