springboot hibernate从sql server上的现有表读取数据

b5buobof  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(342)

我在sql server中有一个现有表。

CREATE TABLE [dbo].[STUDENTS](
    [ID] [int] NOT NULL,
    [FIRSTNAME] [varchar](50) NOT NULL,
    [LASTNAME] [varchar](50) NOT NULL,
    [EMAIL] [varchar](100) NOT NULL,
    [AGE] [int] NOT NULL,
    [ADDRESS] [varchar](3000) NOT NULL,
    PRIMARY KEY (ID)
);

我用java创建了这个实体,以便能够使用hibernate提取数据。

package com.My_task_be.spring.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "STUDENTS")
public class Student {

    @Id
    @Column(name = "ID")
    public int id;
    @Column(name = "FISRTNAME")
    public String firstName;
    @Column(name = "LASTNAME")
    public String lastName;
    @Column(name = "EMAIL")
    public String email;
    @Column(name = "AGE")
    public int Age;
    @Column(name = "ADDRESS")
    public String Address;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public int getAge() {
        return Age;
    }
    public void setAge(int age) {
        Age = age;
    }
    public String getAddress() {
        return Address;
    }
    public void setAddress(String address) {
        Address = address;
    }

}

这是存储库

@Repository
public interface StudentRepository extends CrudRepository<Student, Integer>{
    List<Student> findAll();

}

这是控制器

@RestController
public class AppController {

    @Autowired
    private StudentRepository studentRpository;

    @GetMapping("/getAllStudents")
    public List<Student> getAllStudents(){
        return studentRpository.findAll();
    }

    @GetMapping("/helloWorld")
    public String helloWorld(){
        return "Hello World!";
    }

}

当我跑的时候 http://localhost:8080/getAllStudents 我明白了 Invalid column name 'fisrtname'. 我不希望hibernate创建一个新表,我只希望它从一个现有的表中读取。我已经指定了列名,但它仍然不起作用。
有什么想法吗?

3wabscal

3wabscal1#

你的名字打错了。这是名字,更改如下

@Column(name = "FIRSTNAME")
public String firstName;

相关问题