springboot的controller无法访问

作者在编写SpringBoot应用时遇到新添加的控制器方法失效,经排查发现是由于实体类中的字段命名与数据库表列名不符,MyBatisPlus自动转换导致的。通过调整实体类字段名与数据库列名一致,解决了问题。
摘要由CSDN通过智能技术生成

今天写数据库大作业的时候发现新添的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就成功运行了

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值